blob: 857c705f932f28bd3a5dfa7c3081434d0d51bfb7 [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 Vishniakou370039c2021-02-04 22:09:01 +0000829 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
830 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800831
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000832 const size_t filledSize = std::min(mEvents.size(), bufferSize);
833 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
834
835 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700836 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000837 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800838 }
839
Chris Yea52ade12020-08-27 16:49:20 -0700840 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600841 auto it = mVideoFrames.find(deviceId);
842 if (it != mVideoFrames.end()) {
843 std::vector<TouchVideoFrame> frames = std::move(it->second);
844 mVideoFrames.erase(deviceId);
845 return frames;
846 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800847 return {};
848 }
849
Chris Yea52ade12020-08-27 16:49:20 -0700850 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800851 Device* device = getDevice(deviceId);
852 if (device) {
853 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
854 if (index >= 0) {
855 return device->scanCodeStates.valueAt(index);
856 }
857 }
858 return AKEY_STATE_UNKNOWN;
859 }
860
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000861 InputDeviceCountryCode getCountryCode(int32_t deviceId) const override {
862 Device* device = getDevice(deviceId);
863 if (device) {
864 return device->countryCode;
865 }
866 return InputDeviceCountryCode::INVALID;
867 }
868
Chris Yea52ade12020-08-27 16:49:20 -0700869 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800870 Device* device = getDevice(deviceId);
871 if (device) {
872 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
873 if (index >= 0) {
874 return device->keyCodeStates.valueAt(index);
875 }
876 }
877 return AKEY_STATE_UNKNOWN;
878 }
879
Chris Yea52ade12020-08-27 16:49:20 -0700880 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800881 Device* device = getDevice(deviceId);
882 if (device) {
883 ssize_t index = device->switchStates.indexOfKey(sw);
884 if (index >= 0) {
885 return device->switchStates.valueAt(index);
886 }
887 }
888 return AKEY_STATE_UNKNOWN;
889 }
890
Chris Yea52ade12020-08-27 16:49:20 -0700891 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
892 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800893 Device* device = getDevice(deviceId);
894 if (device) {
895 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
896 if (index >= 0) {
897 *outValue = device->absoluteAxisValue.valueAt(index);
898 return OK;
899 }
900 }
901 *outValue = 0;
902 return -1;
903 }
904
Philip Junker4af3b3d2021-12-14 10:36:55 +0100905 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
906 Device* device = getDevice(deviceId);
907 if (!device) {
908 return AKEYCODE_UNKNOWN;
909 }
910 auto it = device->keyCodeMapping.find(locationKeyCode);
911 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
912 }
913
Chris Yea52ade12020-08-27 16:49:20 -0700914 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700915 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700916 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800917 bool result = false;
918 Device* device = getDevice(deviceId);
919 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700920 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700921 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800922 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
923 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
924 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800925 }
926 }
927 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
928 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
929 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800930 }
931 }
932 }
933 }
934 return result;
935 }
936
Chris Yea52ade12020-08-27 16:49:20 -0700937 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800938 Device* device = getDevice(deviceId);
939 if (device) {
940 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
941 return index >= 0;
942 }
943 return false;
944 }
945
Arthur Hungcb40a002021-08-03 14:31:01 +0000946 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
947 Device* device = getDevice(deviceId);
948 if (!device) {
949 return false;
950 }
951 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
952 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
953 return true;
954 }
955 }
956 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
957 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
958 return true;
959 }
960 }
961 return false;
962 }
963
Chris Yea52ade12020-08-27 16:49:20 -0700964 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965 Device* device = getDevice(deviceId);
966 return device && device->leds.indexOfKey(led) >= 0;
967 }
968
Chris Yea52ade12020-08-27 16:49:20 -0700969 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800970 Device* device = getDevice(deviceId);
971 if (device) {
972 ssize_t index = device->leds.indexOfKey(led);
973 if (index >= 0) {
974 device->leds.replaceValueAt(led, on);
975 } else {
976 ADD_FAILURE()
977 << "Attempted to set the state of an LED that the EventHub declared "
978 "was not present. led=" << led;
979 }
980 }
981 }
982
Chris Yea52ade12020-08-27 16:49:20 -0700983 void getVirtualKeyDefinitions(
984 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800985 outVirtualKeys.clear();
986
987 Device* device = getDevice(deviceId);
988 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800989 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800990 }
991 }
992
Chris Yea52ade12020-08-27 16:49:20 -0700993 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700994 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800995 }
996
Chris Yea52ade12020-08-27 16:49:20 -0700997 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800998 return false;
999 }
1000
Chris Yea52ade12020-08-27 16:49:20 -07001001 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001002
Chris Yea52ade12020-08-27 16:49:20 -07001003 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001004
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001005 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override { return mVibrators; };
Chris Ye87143712020-11-10 05:05:58 +00001006
Chris Yee2b1e5c2021-03-10 22:45:12 -08001007 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
1008 return BATTERY_CAPACITY;
1009 }
Kim Low03ea0352020-11-06 12:45:07 -08001010
Chris Yee2b1e5c2021-03-10 22:45:12 -08001011 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
1012 return BATTERY_STATUS;
1013 }
1014
Andy Chenf9f1a022022-08-29 20:07:10 -04001015 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override {
1016 return {DEFAULT_BATTERY};
1017 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001018
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001019 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
1020 int32_t batteryId) const override {
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001021 if (batteryId != DEFAULT_BATTERY) return {};
1022 static const auto BATTERY_INFO = RawBatteryInfo{.id = DEFAULT_BATTERY,
1023 .name = "default battery",
1024 .flags = InputBatteryClass::CAPACITY,
1025 .path = BATTERY_DEVPATH};
1026 return BATTERY_INFO;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001027 }
Kim Low03ea0352020-11-06 12:45:07 -08001028
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001029 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001030 std::vector<int32_t> ids;
1031 for (const auto& [rawId, info] : mRawLightInfos) {
1032 ids.push_back(rawId);
1033 }
1034 return ids;
1035 }
1036
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001037 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001038 auto it = mRawLightInfos.find(lightId);
1039 if (it == mRawLightInfos.end()) {
1040 return std::nullopt;
1041 }
1042 return it->second;
1043 }
1044
1045 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1046 mLightBrightness.emplace(lightId, brightness);
1047 }
1048
1049 void setLightIntensities(int32_t deviceId, int32_t lightId,
1050 std::unordered_map<LightColor, int32_t> intensities) override {
1051 mLightIntensities.emplace(lightId, intensities);
1052 };
1053
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001054 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001055 auto lightIt = mLightBrightness.find(lightId);
1056 if (lightIt == mLightBrightness.end()) {
1057 return std::nullopt;
1058 }
1059 return lightIt->second;
1060 }
1061
1062 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001063 int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001064 auto lightIt = mLightIntensities.find(lightId);
1065 if (lightIt == mLightIntensities.end()) {
1066 return std::nullopt;
1067 }
1068 return lightIt->second;
1069 };
1070
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001071 void dump(std::string&) const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001072
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001073 void monitor() const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001074
Chris Yea52ade12020-08-27 16:49:20 -07001075 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001076
Chris Yea52ade12020-08-27 16:49:20 -07001077 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001078};
1079
Michael Wrightd02c5b62014-02-10 15:10:22 -08001080// --- FakeInputMapper ---
1081
1082class FakeInputMapper : public InputMapper {
1083 uint32_t mSources;
1084 int32_t mKeyboardType;
1085 int32_t mMetaState;
1086 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1087 KeyedVector<int32_t, int32_t> mScanCodeStates;
1088 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001089 // fake mapping which would normally come from keyCharacterMap
1090 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001091 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001092
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001093 std::mutex mLock;
1094 std::condition_variable mStateChangedCondition;
1095 bool mConfigureWasCalled GUARDED_BY(mLock);
1096 bool mResetWasCalled GUARDED_BY(mLock);
1097 bool mProcessWasCalled GUARDED_BY(mLock);
1098 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001099
Arthur Hungc23540e2018-11-29 20:42:11 +08001100 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001101public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001102 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1103 : InputMapper(deviceContext),
1104 mSources(sources),
1105 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001106 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001107 mConfigureWasCalled(false),
1108 mResetWasCalled(false),
1109 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001110
Chris Yea52ade12020-08-27 16:49:20 -07001111 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001112
1113 void setKeyboardType(int32_t keyboardType) {
1114 mKeyboardType = keyboardType;
1115 }
1116
1117 void setMetaState(int32_t metaState) {
1118 mMetaState = metaState;
1119 }
1120
1121 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001122 std::unique_lock<std::mutex> lock(mLock);
1123 base::ScopedLockAssertion assumeLocked(mLock);
1124 const bool configureCalled =
1125 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1126 return mConfigureWasCalled;
1127 });
1128 if (!configureCalled) {
1129 FAIL() << "Expected configure() to have been called.";
1130 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001131 mConfigureWasCalled = false;
1132 }
1133
1134 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001135 std::unique_lock<std::mutex> lock(mLock);
1136 base::ScopedLockAssertion assumeLocked(mLock);
1137 const bool resetCalled =
1138 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1139 return mResetWasCalled;
1140 });
1141 if (!resetCalled) {
1142 FAIL() << "Expected reset() to have been called.";
1143 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001144 mResetWasCalled = false;
1145 }
1146
Yi Kong9b14ac62018-07-17 13:48:38 -07001147 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001148 std::unique_lock<std::mutex> lock(mLock);
1149 base::ScopedLockAssertion assumeLocked(mLock);
1150 const bool processCalled =
1151 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1152 return mProcessWasCalled;
1153 });
1154 if (!processCalled) {
1155 FAIL() << "Expected process() to have been called.";
1156 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001157 if (outLastEvent) {
1158 *outLastEvent = mLastEvent;
1159 }
1160 mProcessWasCalled = false;
1161 }
1162
1163 void setKeyCodeState(int32_t keyCode, int32_t state) {
1164 mKeyCodeStates.replaceValueFor(keyCode, state);
1165 }
1166
1167 void setScanCodeState(int32_t scanCode, int32_t state) {
1168 mScanCodeStates.replaceValueFor(scanCode, state);
1169 }
1170
1171 void setSwitchState(int32_t switchCode, int32_t state) {
1172 mSwitchStates.replaceValueFor(switchCode, state);
1173 }
1174
1175 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001176 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001177 }
1178
Philip Junker4af3b3d2021-12-14 10:36:55 +01001179 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1180 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1181 }
1182
Michael Wrightd02c5b62014-02-10 15:10:22 -08001183private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001184 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001185
Chris Yea52ade12020-08-27 16:49:20 -07001186 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001187 InputMapper::populateDeviceInfo(deviceInfo);
1188
1189 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1190 deviceInfo->setKeyboardType(mKeyboardType);
1191 }
1192 }
1193
Chris Yea52ade12020-08-27 16:49:20 -07001194 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001195 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001196 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001197
1198 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001199 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001200 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1201 mViewport = config->getDisplayViewportByPort(*displayPort);
1202 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001203
1204 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001205 }
1206
Chris Yea52ade12020-08-27 16:49:20 -07001207 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001208 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001209 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001210 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001211 }
1212
Chris Yea52ade12020-08-27 16:49:20 -07001213 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001214 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001215 mLastEvent = *rawEvent;
1216 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001217 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001218 }
1219
Chris Yea52ade12020-08-27 16:49:20 -07001220 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001221 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1222 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1223 }
1224
Philip Junker4af3b3d2021-12-14 10:36:55 +01001225 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1226 auto it = mKeyCodeMapping.find(locationKeyCode);
1227 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1228 }
1229
Chris Yea52ade12020-08-27 16:49:20 -07001230 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001231 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1232 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1233 }
1234
Chris Yea52ade12020-08-27 16:49:20 -07001235 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001236 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1237 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1238 }
1239
Chris Yea52ade12020-08-27 16:49:20 -07001240 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001241 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -07001242 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001243 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001244 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1245 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1246 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001247 }
1248 }
1249 }
Chris Yea52ade12020-08-27 16:49:20 -07001250 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001251 return result;
1252 }
1253
1254 virtual int32_t getMetaState() {
1255 return mMetaState;
1256 }
1257
1258 virtual void fadePointer() {
1259 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001260
1261 virtual std::optional<int32_t> getAssociatedDisplay() {
1262 if (mViewport) {
1263 return std::make_optional(mViewport->displayId);
1264 }
1265 return std::nullopt;
1266 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001267};
1268
1269
1270// --- InstrumentedInputReader ---
1271
1272class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001273 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001274
1275public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001276 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1277 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001278 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001279 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001280
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001281 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001282
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001283 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001284
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001285 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001286 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001287 InputDeviceIdentifier identifier;
1288 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001289 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001290 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001291 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001292 }
1293
Prabir Pradhan28efc192019-11-05 01:10:04 +00001294 // Make the protected loopOnce method accessible to tests.
1295 using InputReader::loopOnce;
1296
Michael Wrightd02c5b62014-02-10 15:10:22 -08001297protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001298 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1299 const InputDeviceIdentifier& identifier)
1300 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001301 if (!mNextDevices.empty()) {
1302 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1303 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001304 return device;
1305 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001306 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001307 }
1308
arthurhungdcef2dc2020-08-11 14:47:50 +08001309 // --- FakeInputReaderContext ---
1310 class FakeInputReaderContext : public ContextImpl {
1311 int32_t mGlobalMetaState;
1312 bool mUpdateGlobalMetaStateWasCalled;
1313 int32_t mGeneration;
1314
1315 public:
1316 FakeInputReaderContext(InputReader* reader)
1317 : ContextImpl(reader),
1318 mGlobalMetaState(0),
1319 mUpdateGlobalMetaStateWasCalled(false),
1320 mGeneration(1) {}
1321
1322 virtual ~FakeInputReaderContext() {}
1323
1324 void assertUpdateGlobalMetaStateWasCalled() {
1325 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1326 << "Expected updateGlobalMetaState() to have been called.";
1327 mUpdateGlobalMetaStateWasCalled = false;
1328 }
1329
1330 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1331
1332 uint32_t getGeneration() { return mGeneration; }
1333
1334 void updateGlobalMetaState() override {
1335 mUpdateGlobalMetaStateWasCalled = true;
1336 ContextImpl::updateGlobalMetaState();
1337 }
1338
1339 int32_t getGlobalMetaState() override {
1340 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1341 }
1342
1343 int32_t bumpGeneration() override {
1344 mGeneration = ContextImpl::bumpGeneration();
1345 return mGeneration;
1346 }
1347 } mFakeContext;
1348
Michael Wrightd02c5b62014-02-10 15:10:22 -08001349 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001350
1351public:
1352 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001353};
1354
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001355// --- InputReaderPolicyTest ---
1356class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001357protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001358 sp<FakeInputReaderPolicy> mFakePolicy;
1359
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001360 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -07001361 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001362};
1363
1364/**
1365 * Check that empty set of viewports is an acceptable configuration.
1366 * Also try to get internal viewport two different ways - by type and by uniqueId.
1367 *
1368 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1369 * Such configuration is not currently allowed.
1370 */
1371TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001372 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001373
1374 // We didn't add any viewports yet, so there shouldn't be any.
1375 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001376 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001377 ASSERT_FALSE(internalViewport);
1378
1379 // Add an internal viewport, then clear it
1380 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001381 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001382 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001383
1384 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001385 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001386 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001387 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001388
1389 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001390 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001391 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001392 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001393
1394 mFakePolicy->clearViewports();
1395 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001396 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001397 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001398 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001399 ASSERT_FALSE(internalViewport);
1400}
1401
1402TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1403 const std::string internalUniqueId = "local:0";
1404 const std::string externalUniqueId = "local:1";
1405 const std::string virtualUniqueId1 = "virtual:2";
1406 const std::string virtualUniqueId2 = "virtual:3";
1407 constexpr int32_t virtualDisplayId1 = 2;
1408 constexpr int32_t virtualDisplayId2 = 3;
1409
1410 // Add an internal viewport
1411 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001412 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1413 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001414 // Add an external viewport
1415 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001416 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1417 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001418 // Add an virtual viewport
1419 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001420 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1421 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001422 // Add another virtual viewport
1423 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001424 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1425 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001426
1427 // Check matching by type for internal
1428 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001429 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001430 ASSERT_TRUE(internalViewport);
1431 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1432
1433 // Check matching by type for external
1434 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001435 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001436 ASSERT_TRUE(externalViewport);
1437 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1438
1439 // Check matching by uniqueId for virtual viewport #1
1440 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001441 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001442 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001443 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001444 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1445 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1446
1447 // Check matching by uniqueId for virtual viewport #2
1448 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001449 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001450 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001451 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001452 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1453 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1454}
1455
1456
1457/**
1458 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1459 * that lookup works by checking display id.
1460 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1461 */
1462TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1463 const std::string uniqueId1 = "uniqueId1";
1464 const std::string uniqueId2 = "uniqueId2";
1465 constexpr int32_t displayId1 = 2;
1466 constexpr int32_t displayId2 = 3;
1467
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001468 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1469 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001470 for (const ViewportType& type : types) {
1471 mFakePolicy->clearViewports();
1472 // Add a viewport
1473 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001474 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1475 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001476 // Add another viewport
1477 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001478 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1479 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001480
1481 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001482 std::optional<DisplayViewport> viewport1 =
1483 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001484 ASSERT_TRUE(viewport1);
1485 ASSERT_EQ(displayId1, viewport1->displayId);
1486 ASSERT_EQ(type, viewport1->type);
1487
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001488 std::optional<DisplayViewport> viewport2 =
1489 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001490 ASSERT_TRUE(viewport2);
1491 ASSERT_EQ(displayId2, viewport2->displayId);
1492 ASSERT_EQ(type, viewport2->type);
1493
1494 // When there are multiple viewports of the same kind, and uniqueId is not specified
1495 // in the call to getDisplayViewport, then that situation is not supported.
1496 // The viewports can be stored in any order, so we cannot rely on the order, since that
1497 // is just implementation detail.
1498 // However, we can check that it still returns *a* viewport, we just cannot assert
1499 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001500 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001501 ASSERT_TRUE(someViewport);
1502 }
1503}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001504
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001505/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001506 * When we have multiple internal displays make sure we always return the default display when
1507 * querying by type.
1508 */
1509TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1510 const std::string uniqueId1 = "uniqueId1";
1511 const std::string uniqueId2 = "uniqueId2";
1512 constexpr int32_t nonDefaultDisplayId = 2;
1513 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1514 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1515
1516 // Add the default display first and ensure it gets returned.
1517 mFakePolicy->clearViewports();
1518 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001519 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001520 ViewportType::INTERNAL);
1521 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001522 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001523 ViewportType::INTERNAL);
1524
1525 std::optional<DisplayViewport> viewport =
1526 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1527 ASSERT_TRUE(viewport);
1528 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1529 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1530
1531 // Add the default display second to make sure order doesn't matter.
1532 mFakePolicy->clearViewports();
1533 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001534 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001535 ViewportType::INTERNAL);
1536 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001537 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001538 ViewportType::INTERNAL);
1539
1540 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1541 ASSERT_TRUE(viewport);
1542 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1543 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1544}
1545
1546/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001547 * Check getDisplayViewportByPort
1548 */
1549TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001550 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001551 const std::string uniqueId1 = "uniqueId1";
1552 const std::string uniqueId2 = "uniqueId2";
1553 constexpr int32_t displayId1 = 1;
1554 constexpr int32_t displayId2 = 2;
1555 const uint8_t hdmi1 = 0;
1556 const uint8_t hdmi2 = 1;
1557 const uint8_t hdmi3 = 2;
1558
1559 mFakePolicy->clearViewports();
1560 // Add a viewport that's associated with some display port that's not of interest.
1561 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001562 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1563 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001564 // Add another viewport, connected to HDMI1 port
1565 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001566 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1567 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001568
1569 // Check that correct display viewport was returned by comparing the display ports.
1570 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1571 ASSERT_TRUE(hdmi1Viewport);
1572 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1573 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1574
1575 // Check that we can still get the same viewport using the uniqueId
1576 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1577 ASSERT_TRUE(hdmi1Viewport);
1578 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1579 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1580 ASSERT_EQ(type, hdmi1Viewport->type);
1581
1582 // Check that we cannot find a port with "HDMI2", because we never added one
1583 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1584 ASSERT_FALSE(hdmi2Viewport);
1585}
1586
Michael Wrightd02c5b62014-02-10 15:10:22 -08001587// --- InputReaderTest ---
1588
1589class InputReaderTest : public testing::Test {
1590protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001591 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001592 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001593 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001594 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001595
Chris Yea52ade12020-08-27 16:49:20 -07001596 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001597 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001598 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001599 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001600
Prabir Pradhan28efc192019-11-05 01:10:04 +00001601 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001602 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001603 }
1604
Chris Yea52ade12020-08-27 16:49:20 -07001605 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001606 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001607 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001608 }
1609
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001610 void addDevice(int32_t eventHubId, const std::string& name,
1611 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001612 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001613
1614 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001615 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001616 }
1617 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001618 mReader->loopOnce();
1619 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001620 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1621 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001622 }
1623
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001624 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001625 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001626 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001627 }
1628
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001629 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001630 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001631 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001632 }
1633
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001634 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001635 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001636 ftl::Flags<InputDeviceClass> classes,
1637 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001638 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001639 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1640 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001641 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001642 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001643 return mapper;
1644 }
1645};
1646
Chris Ye98d3f532020-10-01 21:48:59 -07001647TEST_F(InputReaderTest, PolicyGetInputDevices) {
1648 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001649 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001650 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001651
1652 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001653 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001654 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001655 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001656 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001657 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1658 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001659 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001660}
1661
Chris Yee7310032020-09-22 15:36:28 -07001662TEST_F(InputReaderTest, GetMergedInputDevices) {
1663 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1664 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1665 // Add two subdevices to device
1666 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1667 // Must add at least one mapper or the device will be ignored!
1668 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1669 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1670
1671 // Push same device instance for next device to be added, so they'll have same identifier.
1672 mReader->pushNextDevice(device);
1673 mReader->pushNextDevice(device);
1674 ASSERT_NO_FATAL_FAILURE(
1675 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1676 ASSERT_NO_FATAL_FAILURE(
1677 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1678
1679 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001680 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001681}
1682
Chris Yee14523a2020-12-19 13:46:00 -08001683TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1684 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1685 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1686 // Add two subdevices to device
1687 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1688 // Must add at least one mapper or the device will be ignored!
1689 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1690 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1691
1692 // Push same device instance for next device to be added, so they'll have same identifier.
1693 mReader->pushNextDevice(device);
1694 mReader->pushNextDevice(device);
1695 // Sensor device is initially disabled
1696 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1697 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1698 nullptr));
1699 // Device is disabled because the only sub device is a sensor device and disabled initially.
1700 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1701 ASSERT_FALSE(device->isEnabled());
1702 ASSERT_NO_FATAL_FAILURE(
1703 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1704 // The merged device is enabled if any sub device is enabled
1705 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1706 ASSERT_TRUE(device->isEnabled());
1707}
1708
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001709TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001710 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001711 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001712 constexpr int32_t eventHubId = 1;
1713 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001714 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001715 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001716 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001717 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001718
Yi Kong9b14ac62018-07-17 13:48:38 -07001719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001720
1721 NotifyDeviceResetArgs resetArgs;
1722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001723 ASSERT_EQ(deviceId, resetArgs.deviceId);
1724
1725 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001726 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001727 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001728
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001730 ASSERT_EQ(deviceId, resetArgs.deviceId);
1731 ASSERT_EQ(device->isEnabled(), false);
1732
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001733 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001734 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001737 ASSERT_EQ(device->isEnabled(), false);
1738
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001739 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001740 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001742 ASSERT_EQ(deviceId, resetArgs.deviceId);
1743 ASSERT_EQ(device->isEnabled(), true);
1744}
1745
Michael Wrightd02c5b62014-02-10 15:10:22 -08001746TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001747 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001748 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001749 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001750 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001751 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001752 AINPUT_SOURCE_KEYBOARD, nullptr);
1753 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001754
1755 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1756 AINPUT_SOURCE_ANY, AKEYCODE_A))
1757 << "Should return unknown when the device id is >= 0 but unknown.";
1758
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001759 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1760 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1761 << "Should return unknown when the device id is valid but the sources are not "
1762 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001763
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001764 ASSERT_EQ(AKEY_STATE_DOWN,
1765 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1766 AKEYCODE_A))
1767 << "Should return value provided by mapper when device id is valid and the device "
1768 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001769
1770 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1771 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1772 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1773
1774 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1775 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1776 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1777}
1778
Philip Junker4af3b3d2021-12-14 10:36:55 +01001779TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1780 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1781 constexpr int32_t eventHubId = 1;
1782 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1783 InputDeviceClass::KEYBOARD,
1784 AINPUT_SOURCE_KEYBOARD, nullptr);
1785 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1786
1787 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1788 << "Should return unknown when the device with the specified id is not found.";
1789
1790 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1791 << "Should return correct mapping when device id is valid and mapping exists.";
1792
1793 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1794 << "Should return the location key code when device id is valid and there's no "
1795 "mapping.";
1796}
1797
1798TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1799 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1800 constexpr int32_t eventHubId = 1;
1801 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1802 InputDeviceClass::JOYSTICK,
1803 AINPUT_SOURCE_GAMEPAD, nullptr);
1804 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1805
1806 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1807 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1808}
1809
Michael Wrightd02c5b62014-02-10 15:10:22 -08001810TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001811 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001812 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001813 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001814 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001815 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001816 AINPUT_SOURCE_KEYBOARD, nullptr);
1817 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001818
1819 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1820 AINPUT_SOURCE_ANY, KEY_A))
1821 << "Should return unknown when the device id is >= 0 but unknown.";
1822
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001823 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1824 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1825 << "Should return unknown when the device id is valid but the sources are not "
1826 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001827
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001828 ASSERT_EQ(AKEY_STATE_DOWN,
1829 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1830 KEY_A))
1831 << "Should return value provided by mapper when device id is valid and the device "
1832 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001833
1834 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1835 AINPUT_SOURCE_TRACKBALL, KEY_A))
1836 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1837
1838 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1839 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1840 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1841}
1842
1843TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001844 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001845 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001846 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001847 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001848 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001849 AINPUT_SOURCE_KEYBOARD, nullptr);
1850 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001851
1852 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1853 AINPUT_SOURCE_ANY, SW_LID))
1854 << "Should return unknown when the device id is >= 0 but unknown.";
1855
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001856 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1857 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1858 << "Should return unknown when the device id is valid but the sources are not "
1859 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001860
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001861 ASSERT_EQ(AKEY_STATE_DOWN,
1862 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1863 SW_LID))
1864 << "Should return value provided by mapper when device id is valid and the device "
1865 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001866
1867 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1868 AINPUT_SOURCE_TRACKBALL, SW_LID))
1869 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1870
1871 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1872 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1873 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1874}
1875
1876TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001877 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001878 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001879 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001880 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001881 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001882 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001883
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001884 mapper.addSupportedKeyCode(AKEYCODE_A);
1885 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001886
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001887 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001888 uint8_t flags[4] = { 0, 0, 0, 1 };
1889
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001890 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08001891 << "Should return false when device id is >= 0 but unknown.";
1892 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1893
1894 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001895 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001896 << "Should return false when device id is valid but the sources are not supported by "
1897 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001898 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1899
1900 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001901 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001902 keyCodes, flags))
1903 << "Should return value provided by mapper when device id is valid and the device "
1904 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001905 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1906
1907 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001908 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1909 << "Should return false when the device id is < 0 but the sources are not supported by "
1910 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001911 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1912
1913 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001914 ASSERT_TRUE(
1915 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1916 << "Should return value provided by mapper when device id is < 0 and one of the "
1917 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001918 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1919}
1920
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001921TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001922 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001923 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001924
1925 NotifyConfigurationChangedArgs args;
1926
1927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1928 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1929}
1930
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001931TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001932 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001933 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001934 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001935 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001936 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001937 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001938 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001939 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001940
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001941 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001942 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001943 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1944
1945 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001946 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001947 ASSERT_EQ(when, event.when);
1948 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001949 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001950 ASSERT_EQ(EV_KEY, event.type);
1951 ASSERT_EQ(KEY_A, event.code);
1952 ASSERT_EQ(1, event.value);
1953}
1954
Garfield Tan1c7bc862020-01-28 13:24:04 -08001955TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001956 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001957 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001958 constexpr int32_t eventHubId = 1;
1959 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001960 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001961 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001962 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001963 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001964
1965 NotifyDeviceResetArgs resetArgs;
1966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001967 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001968
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001969 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001970 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001972 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001973 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001974
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001975 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001976 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001978 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001979 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001980
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001981 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001982 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001984 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001985 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001986}
1987
Garfield Tan1c7bc862020-01-28 13:24:04 -08001988TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1989 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001990 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001991 constexpr int32_t eventHubId = 1;
1992 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1993 // Must add at least one mapper or the device will be ignored!
1994 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001995 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001996 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1997
1998 NotifyDeviceResetArgs resetArgs;
1999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2000 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
2001}
2002
Arthur Hungc23540e2018-11-29 20:42:11 +08002003TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002004 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002005 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002006 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08002007 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002008 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2009 FakeInputMapper& mapper =
2010 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002011 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08002012
2013 const uint8_t hdmi1 = 1;
2014
2015 // Associated touch screen with second display.
2016 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2017
2018 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00002019 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08002020 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002021 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002022 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002023 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002024 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002025 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002026 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002027 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002028
2029 // Add the device, and make sure all of the callbacks are triggered.
2030 // The device is added after the input port associations are processed since
2031 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002032 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002035 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002036
Arthur Hung2c9a3342019-07-23 14:18:59 +08002037 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002038 ASSERT_EQ(deviceId, device->getId());
2039 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2040 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002041
2042 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002043 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002044 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002045 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002046}
2047
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002048TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2049 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002050 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002051 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2052 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2053 // Must add at least one mapper or the device will be ignored!
2054 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2055 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2056 mReader->pushNextDevice(device);
2057 mReader->pushNextDevice(device);
2058 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2059 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2060
2061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2062
2063 NotifyDeviceResetArgs resetArgs;
2064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2065 ASSERT_EQ(deviceId, resetArgs.deviceId);
2066 ASSERT_TRUE(device->isEnabled());
2067 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2068 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2069
2070 disableDevice(deviceId);
2071 mReader->loopOnce();
2072
2073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2074 ASSERT_EQ(deviceId, resetArgs.deviceId);
2075 ASSERT_FALSE(device->isEnabled());
2076 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2077 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2078
2079 enableDevice(deviceId);
2080 mReader->loopOnce();
2081
2082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2083 ASSERT_EQ(deviceId, resetArgs.deviceId);
2084 ASSERT_TRUE(device->isEnabled());
2085 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2086 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2087}
2088
2089TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2090 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002091 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002092 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2093 // Add two subdevices to device
2094 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2095 FakeInputMapper& mapperDevice1 =
2096 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2097 FakeInputMapper& mapperDevice2 =
2098 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2099 mReader->pushNextDevice(device);
2100 mReader->pushNextDevice(device);
2101 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2102 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2103
2104 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2105 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2106
2107 ASSERT_EQ(AKEY_STATE_DOWN,
2108 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2109 ASSERT_EQ(AKEY_STATE_DOWN,
2110 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2111 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2112 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2113}
2114
Prabir Pradhan7e186182020-11-10 13:56:45 -08002115TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2116 NotifyPointerCaptureChangedArgs args;
2117
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002118 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002119 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2120 mReader->loopOnce();
2121 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002122 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2123 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002124
2125 mFakePolicy->setPointerCapture(false);
2126 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2127 mReader->loopOnce();
2128 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002129 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002130
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002131 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002132 // does not change.
2133 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2134 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002135 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002136}
2137
Chris Ye87143712020-11-10 05:05:58 +00002138class FakeVibratorInputMapper : public FakeInputMapper {
2139public:
2140 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2141 : FakeInputMapper(deviceContext, sources) {}
2142
2143 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2144};
2145
2146TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2147 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002148 ftl::Flags<InputDeviceClass> deviceClass =
2149 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002150 constexpr int32_t eventHubId = 1;
2151 const char* DEVICE_LOCATION = "BLUETOOTH";
2152 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2153 FakeVibratorInputMapper& mapper =
2154 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2155 mReader->pushNextDevice(device);
2156
2157 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2158 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2159
2160 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2161 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2162}
2163
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002164// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002165
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002166class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002167public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002168 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002169
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002170 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002171
Andy Chenf9f1a022022-08-29 20:07:10 -04002172 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
2173
Chris Yee2b1e5c2021-03-10 22:45:12 -08002174 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2175
2176 void dump(std::string& dump) override {}
2177
2178 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2179 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002180 }
2181
Chris Yee2b1e5c2021-03-10 22:45:12 -08002182 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2183 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002184 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002185
2186 bool setLightColor(int32_t lightId, int32_t color) override {
2187 getDeviceContext().setLightBrightness(lightId, color >> 24);
2188 return true;
2189 }
2190
2191 std::optional<int32_t> getLightColor(int32_t lightId) override {
2192 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2193 if (!result.has_value()) {
2194 return std::nullopt;
2195 }
2196 return result.value() << 24;
2197 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002198
2199 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2200
2201 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2202
2203private:
2204 InputDeviceContext& mDeviceContext;
2205 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2206 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04002207 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002208};
2209
Chris Yee2b1e5c2021-03-10 22:45:12 -08002210TEST_F(InputReaderTest, BatteryGetCapacity) {
2211 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002212 ftl::Flags<InputDeviceClass> deviceClass =
2213 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002214 constexpr int32_t eventHubId = 1;
2215 const char* DEVICE_LOCATION = "BLUETOOTH";
2216 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002217 FakePeripheralController& controller =
2218 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002219 mReader->pushNextDevice(device);
2220
2221 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2222
2223 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2224 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2225}
2226
2227TEST_F(InputReaderTest, BatteryGetStatus) {
2228 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002229 ftl::Flags<InputDeviceClass> deviceClass =
2230 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002231 constexpr int32_t eventHubId = 1;
2232 const char* DEVICE_LOCATION = "BLUETOOTH";
2233 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002234 FakePeripheralController& controller =
2235 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002236 mReader->pushNextDevice(device);
2237
2238 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2239
2240 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2241 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2242}
2243
Prabir Pradhane287ecd2022-09-07 21:18:05 +00002244TEST_F(InputReaderTest, BatteryGetDevicePath) {
2245 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2246 ftl::Flags<InputDeviceClass> deviceClass =
2247 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2248 constexpr int32_t eventHubId = 1;
2249 const char* DEVICE_LOCATION = "BLUETOOTH";
2250 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2251 device->addController<FakePeripheralController>(eventHubId);
2252 mReader->pushNextDevice(device);
2253
2254 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2255
2256 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), BATTERY_DEVPATH);
2257}
2258
Chris Ye3fdbfef2021-01-06 18:45:18 -08002259TEST_F(InputReaderTest, LightGetColor) {
2260 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002261 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002262 constexpr int32_t eventHubId = 1;
2263 const char* DEVICE_LOCATION = "BLUETOOTH";
2264 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002265 FakePeripheralController& controller =
2266 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002267 mReader->pushNextDevice(device);
2268 RawLightInfo info = {.id = 1,
2269 .name = "Mono",
2270 .maxBrightness = 255,
2271 .flags = InputLightClass::BRIGHTNESS,
2272 .path = ""};
2273 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2274 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2275
2276 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002277
Chris Yee2b1e5c2021-03-10 22:45:12 -08002278 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2279 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002280 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2281 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2282}
2283
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002284// --- InputReaderIntegrationTest ---
2285
2286// These tests create and interact with the InputReader only through its interface.
2287// The InputReader is started during SetUp(), which starts its processing in its own
2288// thread. The tests use linux uinput to emulate input devices.
2289// NOTE: Interacting with the physical device while these tests are running may cause
2290// the tests to fail.
2291class InputReaderIntegrationTest : public testing::Test {
2292protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002293 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002294 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002295 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002296
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002297 std::shared_ptr<FakePointerController> mFakePointerController;
2298
Chris Yea52ade12020-08-27 16:49:20 -07002299 void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002300 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002301 mFakePointerController = std::make_shared<FakePointerController>();
2302 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002303 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2304 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002305
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002306 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2307 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002308 ASSERT_EQ(mReader->start(), OK);
2309
2310 // Since this test is run on a real device, all the input devices connected
2311 // to the test device will show up in mReader. We wait for those input devices to
2312 // show up before beginning the tests.
2313 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2314 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2315 }
2316
Chris Yea52ade12020-08-27 16:49:20 -07002317 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002318 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002319 mReader.reset();
2320 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002321 mFakePolicy.clear();
2322 }
2323};
2324
2325TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2326 // An invalid input device that is only used for this test.
2327 class InvalidUinputDevice : public UinputDevice {
2328 public:
2329 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2330
2331 private:
2332 void configureDevice(int fd, uinput_user_dev* device) override {}
2333 };
2334
2335 const size_t numDevices = mFakePolicy->getInputDevices().size();
2336
2337 // UinputDevice does not set any event or key bits, so InputReader should not
2338 // consider it as a valid device.
2339 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2340 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2341 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2342 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2343
2344 invalidDevice.reset();
2345 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2346 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2347 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2348}
2349
2350TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2351 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2352
2353 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2354 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2355 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2356 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2357
2358 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002359 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002360 const auto& it =
2361 std::find_if(inputDevices.begin(), inputDevices.end(),
2362 [&keyboard](const InputDeviceInfo& info) {
2363 return info.getIdentifier().name == keyboard->getName();
2364 });
2365
2366 ASSERT_NE(it, inputDevices.end());
2367 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2368 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2369 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002370
2371 keyboard.reset();
2372 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2373 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2374 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2375}
2376
2377TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2378 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2379 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2380
2381 NotifyConfigurationChangedArgs configChangedArgs;
2382 ASSERT_NO_FATAL_FAILURE(
2383 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002384 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002385 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2386
2387 NotifyKeyArgs keyArgs;
2388 keyboard->pressAndReleaseHomeKey();
2389 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2390 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002391 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002392 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002393 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002394 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002395 prevTimestamp = keyArgs.eventTime;
2396
2397 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2398 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002399 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002400 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002401 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002402}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002403
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002404/**
2405 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2406 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2407 * are passed to the listener.
2408 */
2409static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2410TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2411 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2412 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2413 NotifyKeyArgs keyArgs;
2414
2415 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2416 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2417 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2418 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2419
2420 controller->pressAndReleaseKey(BTN_GEAR_UP);
2421 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2422 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2423 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2424}
2425
Arthur Hungaab25622020-01-16 11:22:11 +08002426// --- TouchProcessTest ---
2427class TouchIntegrationTest : public InputReaderIntegrationTest {
2428protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002429 const std::string UNIQUE_ID = "local:0";
2430
Chris Yea52ade12020-08-27 16:49:20 -07002431 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002432 InputReaderIntegrationTest::SetUp();
2433 // At least add an internal display.
2434 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2435 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002436 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002437
2438 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2439 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2440 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2441 }
2442
2443 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2444 int32_t orientation, const std::string& uniqueId,
2445 std::optional<uint8_t> physicalPort,
2446 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002447 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2448 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002449 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2450 }
2451
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002452 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2453 NotifyMotionArgs args;
2454 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2455 EXPECT_EQ(action, args.action);
2456 ASSERT_EQ(points.size(), args.pointerCount);
2457 for (size_t i = 0; i < args.pointerCount; i++) {
2458 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2459 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2460 }
2461 }
2462
Arthur Hungaab25622020-01-16 11:22:11 +08002463 std::unique_ptr<UinputTouchScreen> mDevice;
2464};
2465
2466TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2467 NotifyMotionArgs args;
2468 const Point centerPoint = mDevice->getCenterPoint();
2469
2470 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002471 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002472 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002473 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002474 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2475 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2476
2477 // ACTION_MOVE
2478 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002479 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002480 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2482
2483 // ACTION_UP
2484 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002485 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002486 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2487 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2488}
2489
2490TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2491 NotifyMotionArgs args;
2492 const Point centerPoint = mDevice->getCenterPoint();
2493
2494 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002495 mDevice->sendSlot(FIRST_SLOT);
2496 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002497 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002498 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002499 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2500 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2501
2502 // ACTION_POINTER_DOWN (Second slot)
2503 const Point secondPoint = centerPoint + Point(100, 100);
2504 mDevice->sendSlot(SECOND_SLOT);
2505 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002506 mDevice->sendDown(secondPoint);
2507 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002508 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002509 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002510
2511 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002512 mDevice->sendMove(secondPoint + Point(1, 1));
2513 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002514 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2515 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2516
2517 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002518 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002519 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002520 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002521 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002522
2523 // ACTION_UP
2524 mDevice->sendSlot(FIRST_SLOT);
2525 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002526 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002527 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2528 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2529}
2530
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002531/**
2532 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2533 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2534 * data?
2535 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2536 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2537 * for Pointer 0 only is generated after.
2538 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2539 * events, we will not miss any information.
2540 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2541 * event generated afterwards that contains the newest movement of pointer 0.
2542 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2543 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2544 * losing information about non-palm pointers.
2545 */
2546TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2547 NotifyMotionArgs args;
2548 const Point centerPoint = mDevice->getCenterPoint();
2549
2550 // ACTION_DOWN
2551 mDevice->sendSlot(FIRST_SLOT);
2552 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2553 mDevice->sendDown(centerPoint);
2554 mDevice->sendSync();
2555 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2556
2557 // ACTION_POINTER_DOWN (Second slot)
2558 const Point secondPoint = centerPoint + Point(100, 100);
2559 mDevice->sendSlot(SECOND_SLOT);
2560 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2561 mDevice->sendDown(secondPoint);
2562 mDevice->sendSync();
2563 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2564
2565 // ACTION_MOVE (First slot)
2566 mDevice->sendSlot(FIRST_SLOT);
2567 mDevice->sendMove(centerPoint + Point(5, 5));
2568 // ACTION_POINTER_UP (Second slot)
2569 mDevice->sendSlot(SECOND_SLOT);
2570 mDevice->sendPointerUp();
2571 // Send a single sync for the above 2 pointer updates
2572 mDevice->sendSync();
2573
2574 // First, we should get POINTER_UP for the second pointer
2575 assertReceivedMotion(ACTION_POINTER_1_UP,
2576 {/*first pointer */ centerPoint + Point(5, 5),
2577 /*second pointer*/ secondPoint});
2578
2579 // Next, the MOVE event for the first pointer
2580 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2581}
2582
2583/**
2584 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2585 * move, and then it will go up, all in the same frame.
2586 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2587 * gets sent to the listener.
2588 */
2589TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2590 NotifyMotionArgs args;
2591 const Point centerPoint = mDevice->getCenterPoint();
2592
2593 // ACTION_DOWN
2594 mDevice->sendSlot(FIRST_SLOT);
2595 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2596 mDevice->sendDown(centerPoint);
2597 mDevice->sendSync();
2598 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2599
2600 // ACTION_POINTER_DOWN (Second slot)
2601 const Point secondPoint = centerPoint + Point(100, 100);
2602 mDevice->sendSlot(SECOND_SLOT);
2603 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2604 mDevice->sendDown(secondPoint);
2605 mDevice->sendSync();
2606 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2607
2608 // ACTION_MOVE (First slot)
2609 mDevice->sendSlot(FIRST_SLOT);
2610 mDevice->sendMove(centerPoint + Point(5, 5));
2611 // ACTION_POINTER_UP (Second slot)
2612 mDevice->sendSlot(SECOND_SLOT);
2613 mDevice->sendMove(secondPoint + Point(6, 6));
2614 mDevice->sendPointerUp();
2615 // Send a single sync for the above 2 pointer updates
2616 mDevice->sendSync();
2617
2618 // First, we should get POINTER_UP for the second pointer
2619 // The movement of the second pointer during the liftoff frame is ignored.
2620 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2621 assertReceivedMotion(ACTION_POINTER_1_UP,
2622 {/*first pointer */ centerPoint + Point(5, 5),
2623 /*second pointer*/ secondPoint});
2624
2625 // Next, the MOVE event for the first pointer
2626 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2627}
2628
Arthur Hungaab25622020-01-16 11:22:11 +08002629TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2630 NotifyMotionArgs args;
2631 const Point centerPoint = mDevice->getCenterPoint();
2632
2633 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002634 mDevice->sendSlot(FIRST_SLOT);
2635 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002636 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002637 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002638 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2639 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2640
arthurhungcc7f9802020-04-30 17:55:40 +08002641 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002642 const Point secondPoint = centerPoint + Point(100, 100);
2643 mDevice->sendSlot(SECOND_SLOT);
2644 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2645 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002646 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002647 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002648 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002649
arthurhungcc7f9802020-04-30 17:55:40 +08002650 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002651 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002652 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002653 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2654 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2655
arthurhungcc7f9802020-04-30 17:55:40 +08002656 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2657 // a palm event.
2658 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002659 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002660 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002661 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002662 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002663 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002664
arthurhungcc7f9802020-04-30 17:55:40 +08002665 // Send up to second slot, expect first slot send moving.
2666 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002667 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002668 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2669 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002670
arthurhungcc7f9802020-04-30 17:55:40 +08002671 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002672 mDevice->sendSlot(FIRST_SLOT);
2673 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002674 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002675
arthurhungcc7f9802020-04-30 17:55:40 +08002676 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2677 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002678}
2679
Michael Wrightd02c5b62014-02-10 15:10:22 -08002680// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002681class InputDeviceTest : public testing::Test {
2682protected:
2683 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002684 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002685 static const int32_t DEVICE_ID;
2686 static const int32_t DEVICE_GENERATION;
2687 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002688 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002689 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002690
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002691 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002692 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002693 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002694 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002695 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002696
Chris Yea52ade12020-08-27 16:49:20 -07002697 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002698 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002699 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002700 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002701 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002702 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002703 InputDeviceIdentifier identifier;
2704 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002705 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002706 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002707 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002708 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002709 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002710 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002711 }
2712
Chris Yea52ade12020-08-27 16:49:20 -07002713 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002714 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002715 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002716 }
2717};
2718
2719const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002720const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002721const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002722const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2723const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002724const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002725 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002726const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002727
2728TEST_F(InputDeviceTest, ImmutableProperties) {
2729 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002730 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002731 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002732}
2733
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002734TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
2735 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
2736
2737 // Configuration
2738 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2739 InputReaderConfiguration config;
2740 mDevice->configure(ARBITRARY_TIME, &config, 0);
2741
2742 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
2743}
2744
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002745TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2746 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002747}
2748
Michael Wrightd02c5b62014-02-10 15:10:22 -08002749TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2750 // Configuration.
2751 InputReaderConfiguration config;
2752 mDevice->configure(ARBITRARY_TIME, &config, 0);
2753
2754 // Reset.
2755 mDevice->reset(ARBITRARY_TIME);
2756
2757 NotifyDeviceResetArgs resetArgs;
2758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2759 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2760 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2761
2762 // Metadata.
2763 ASSERT_TRUE(mDevice->isIgnored());
2764 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2765
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002766 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002767 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002768 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002769 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2770 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2771
2772 // State queries.
2773 ASSERT_EQ(0, mDevice->getMetaState());
2774
2775 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2776 << "Ignored device should return unknown key code state.";
2777 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2778 << "Ignored device should return unknown scan code state.";
2779 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2780 << "Ignored device should return unknown switch state.";
2781
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002782 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002783 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002784 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002785 << "Ignored device should never mark any key codes.";
2786 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2787 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2788}
2789
2790TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2791 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002792 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08002793
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002794 FakeInputMapper& mapper1 =
2795 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002796 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2797 mapper1.setMetaState(AMETA_ALT_ON);
2798 mapper1.addSupportedKeyCode(AKEYCODE_A);
2799 mapper1.addSupportedKeyCode(AKEYCODE_B);
2800 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2801 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2802 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2803 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2804 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002805
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002806 FakeInputMapper& mapper2 =
2807 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002808 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002809
2810 InputReaderConfiguration config;
2811 mDevice->configure(ARBITRARY_TIME, &config, 0);
2812
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002813 std::string propertyValue;
2814 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002815 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002816 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002817
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002818 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2819 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002820
2821 // Reset
2822 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002823 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2824 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002825
2826 NotifyDeviceResetArgs resetArgs;
2827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2828 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2829 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2830
2831 // Metadata.
2832 ASSERT_FALSE(mDevice->isIgnored());
2833 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2834
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002835 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002836 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002837 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002838 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2839 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2840
2841 // State queries.
2842 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2843 << "Should query mappers and combine meta states.";
2844
2845 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2846 << "Should return unknown key code state when source not supported.";
2847 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2848 << "Should return unknown scan code state when source not supported.";
2849 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2850 << "Should return unknown switch state when source not supported.";
2851
2852 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2853 << "Should query mapper when source is supported.";
2854 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2855 << "Should query mapper when source is supported.";
2856 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2857 << "Should query mapper when source is supported.";
2858
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002859 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002860 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002861 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002862 << "Should do nothing when source is unsupported.";
2863 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2864 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2865 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2866 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2867
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002868 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002869 << "Should query mapper when source is supported.";
2870 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2871 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2872 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2873 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2874
2875 // Event handling.
2876 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002877 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002878 mDevice->process(&event, 1);
2879
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002880 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2881 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002882}
2883
Arthur Hung2c9a3342019-07-23 14:18:59 +08002884// A single input device is associated with a specific display. Check that:
2885// 1. Device is disabled if the viewport corresponding to the associated display is not found
2886// 2. Device is disabled when setEnabled API is called
2887TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002888 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002889
2890 // First Configuration.
2891 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2892
2893 // Device should be enabled by default.
2894 ASSERT_TRUE(mDevice->isEnabled());
2895
2896 // Prepare associated info.
2897 constexpr uint8_t hdmi = 1;
2898 const std::string UNIQUE_ID = "local:1";
2899
2900 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2901 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2902 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2903 // Device should be disabled because it is associated with a specific display via
2904 // input port <-> display port association, but the corresponding display is not found
2905 ASSERT_FALSE(mDevice->isEnabled());
2906
2907 // Prepare displays.
2908 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002909 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2910 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002911 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2912 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2913 ASSERT_TRUE(mDevice->isEnabled());
2914
2915 // Device should be disabled after set disable.
2916 mFakePolicy->addDisabledDevice(mDevice->getId());
2917 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2918 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2919 ASSERT_FALSE(mDevice->isEnabled());
2920
2921 // Device should still be disabled even found the associated display.
2922 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2923 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2924 ASSERT_FALSE(mDevice->isEnabled());
2925}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002926
Christine Franks1ba71cc2021-04-07 14:37:42 -07002927TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2928 // Device should be enabled by default.
2929 mFakePolicy->clearViewports();
2930 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2931 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2932 ASSERT_TRUE(mDevice->isEnabled());
2933
2934 // Device should be disabled because it is associated with a specific display, but the
2935 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002936 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002937 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2938 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2939 ASSERT_FALSE(mDevice->isEnabled());
2940
2941 // Device should be enabled when a display is found.
2942 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2943 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2944 NO_PORT, ViewportType::INTERNAL);
2945 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2946 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2947 ASSERT_TRUE(mDevice->isEnabled());
2948
2949 // Device should be disabled after set disable.
2950 mFakePolicy->addDisabledDevice(mDevice->getId());
2951 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2952 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2953 ASSERT_FALSE(mDevice->isEnabled());
2954
2955 // Device should still be disabled even found the associated display.
2956 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2957 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2958 ASSERT_FALSE(mDevice->isEnabled());
2959}
2960
Christine Franks2a2293c2022-01-18 11:51:16 -08002961TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2962 mFakePolicy->clearViewports();
2963 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2964 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2965
Christine Franks2a2293c2022-01-18 11:51:16 -08002966 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2967 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2968 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2969 NO_PORT, ViewportType::INTERNAL);
2970 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2971 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2972 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2973}
2974
Michael Wrightd02c5b62014-02-10 15:10:22 -08002975// --- InputMapperTest ---
2976
2977class InputMapperTest : public testing::Test {
2978protected:
2979 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002980 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002981 static const int32_t DEVICE_ID;
2982 static const int32_t DEVICE_GENERATION;
2983 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002984 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002985 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002986
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002987 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002988 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002989 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002990 std::unique_ptr<InstrumentedInputReader> mReader;
2991 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002992
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002993 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002994 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002995 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002996 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002997 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002998 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002999 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003000 // Consume the device reset notification generated when adding a new device.
3001 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003002 }
3003
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003004 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003005 SetUp(DEVICE_CLASSES);
3006 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003007
Chris Yea52ade12020-08-27 16:49:20 -07003008 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003009 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003010 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003011 }
3012
3013 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003014 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003015 }
3016
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003017 void configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00003018 if (!changes ||
3019 (changes &
3020 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
3021 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003022 mReader->requestRefreshConfiguration(changes);
3023 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08003024 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003025 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003026 // Loop the reader to flush the input listener queue.
3027 mReader->loopOnce();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003028 }
3029
arthurhungdcef2dc2020-08-11 14:47:50 +08003030 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
3031 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003032 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003033 InputDeviceIdentifier identifier;
3034 identifier.name = name;
3035 identifier.location = location;
3036 std::shared_ptr<InputDevice> device =
3037 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
3038 identifier);
3039 mReader->pushNextDevice(device);
3040 mFakeEventHub->addDevice(eventHubId, name, classes);
3041 mReader->loopOnce();
3042 return device;
3043 }
3044
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003045 template <class T, typename... Args>
3046 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003047 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003048 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003049 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07003050 mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003051 // Loop the reader to flush the input listener queue.
3052 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003053 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003054 }
3055
3056 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003057 int32_t orientation, const std::string& uniqueId,
3058 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003059 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3060 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003061 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3062 }
3063
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003064 void clearViewports() {
3065 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003066 }
3067
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003068 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
3069 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003070 RawEvent event;
3071 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003072 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003073 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003074 event.type = type;
3075 event.code = code;
3076 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003077 mapper.process(&event);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003078 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003079 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003080 }
3081
3082 static void assertMotionRange(const InputDeviceInfo& info,
3083 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3084 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003085 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3087 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3088 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3089 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3090 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3091 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3092 }
3093
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003094 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3095 float size, float touchMajor, float touchMinor, float toolMajor,
3096 float toolMinor, float orientation, float distance,
3097 float scaledAxisEpsilon = 1.f) {
3098 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3099 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3101 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003102 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3103 scaledAxisEpsilon);
3104 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3105 scaledAxisEpsilon);
3106 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3107 scaledAxisEpsilon);
3108 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3109 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003110 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3111 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3112 }
3113
Michael Wright17db18e2020-06-26 20:51:44 +01003114 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003115 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003116 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003117 ASSERT_NEAR(x, actualX, 1);
3118 ASSERT_NEAR(y, actualY, 1);
3119 }
3120};
3121
3122const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003123const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003124const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3126const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003127const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3128 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003129const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003130
3131// --- SwitchInputMapperTest ---
3132
3133class SwitchInputMapperTest : public InputMapperTest {
3134protected:
3135};
3136
3137TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003138 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003139
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003140 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003141}
3142
3143TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003144 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003145
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003146 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003147 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003148
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003149 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003150 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003151}
3152
3153TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003154 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003155
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003156 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3157 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3158 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3159 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003160
3161 NotifySwitchArgs args;
3162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3163 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003164 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3165 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003166 args.switchMask);
3167 ASSERT_EQ(uint32_t(0), args.policyFlags);
3168}
3169
Chris Ye87143712020-11-10 05:05:58 +00003170// --- VibratorInputMapperTest ---
3171class VibratorInputMapperTest : public InputMapperTest {
3172protected:
3173 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3174};
3175
3176TEST_F(VibratorInputMapperTest, GetSources) {
3177 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3178
3179 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3180}
3181
3182TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3183 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3184
3185 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3186}
3187
3188TEST_F(VibratorInputMapperTest, Vibrate) {
3189 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003190 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003191 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3192
3193 VibrationElement pattern(2);
3194 VibrationSequence sequence(2);
3195 pattern.duration = std::chrono::milliseconds(200);
3196 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3197 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3198 sequence.addElement(pattern);
3199 pattern.duration = std::chrono::milliseconds(500);
3200 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3201 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3202 sequence.addElement(pattern);
3203
3204 std::vector<int64_t> timings = {0, 1};
3205 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3206
3207 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003208 // Start vibrating
3209 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003210 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003211 // Verify vibrator state listener was notified.
3212 mReader->loopOnce();
3213 NotifyVibratorStateArgs args;
3214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3215 ASSERT_EQ(DEVICE_ID, args.deviceId);
3216 ASSERT_TRUE(args.isOn);
3217 // Stop vibrating
3218 mapper.cancelVibrate(VIBRATION_TOKEN);
3219 ASSERT_FALSE(mapper.isVibrating());
3220 // Verify vibrator state listener was notified.
3221 mReader->loopOnce();
3222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3223 ASSERT_EQ(DEVICE_ID, args.deviceId);
3224 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003225}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003226
Chris Yef59a2f42020-10-16 12:55:26 -07003227// --- SensorInputMapperTest ---
3228
3229class SensorInputMapperTest : public InputMapperTest {
3230protected:
3231 static const int32_t ACCEL_RAW_MIN;
3232 static const int32_t ACCEL_RAW_MAX;
3233 static const int32_t ACCEL_RAW_FUZZ;
3234 static const int32_t ACCEL_RAW_FLAT;
3235 static const int32_t ACCEL_RAW_RESOLUTION;
3236
3237 static const int32_t GYRO_RAW_MIN;
3238 static const int32_t GYRO_RAW_MAX;
3239 static const int32_t GYRO_RAW_FUZZ;
3240 static const int32_t GYRO_RAW_FLAT;
3241 static const int32_t GYRO_RAW_RESOLUTION;
3242
3243 static const float GRAVITY_MS2_UNIT;
3244 static const float DEGREE_RADIAN_UNIT;
3245
3246 void prepareAccelAxes();
3247 void prepareGyroAxes();
3248 void setAccelProperties();
3249 void setGyroProperties();
3250 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3251};
3252
3253const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3254const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3255const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3256const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3257const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3258
3259const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3260const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3261const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3262const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3263const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3264
3265const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3266const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3267
3268void SensorInputMapperTest::prepareAccelAxes() {
3269 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3270 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3271 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3272 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3273 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3274 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3275}
3276
3277void SensorInputMapperTest::prepareGyroAxes() {
3278 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3279 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3280 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3281 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3282 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3283 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3284}
3285
3286void SensorInputMapperTest::setAccelProperties() {
3287 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3288 /* sensorDataIndex */ 0);
3289 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3290 /* sensorDataIndex */ 1);
3291 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3292 /* sensorDataIndex */ 2);
3293 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3294 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3295 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3296 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3297 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3298}
3299
3300void SensorInputMapperTest::setGyroProperties() {
3301 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3302 /* sensorDataIndex */ 0);
3303 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3304 /* sensorDataIndex */ 1);
3305 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3306 /* sensorDataIndex */ 2);
3307 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3308 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3309 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3310 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3311 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3312}
3313
3314TEST_F(SensorInputMapperTest, GetSources) {
3315 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3316
3317 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3318}
3319
3320TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3321 setAccelProperties();
3322 prepareAccelAxes();
3323 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3324
3325 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3326 std::chrono::microseconds(10000),
3327 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003328 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003329 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3330 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3331 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3332 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3333 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003334
3335 NotifySensorArgs args;
3336 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3337 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3338 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3339
3340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3341 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3342 ASSERT_EQ(args.deviceId, DEVICE_ID);
3343 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3344 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3345 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3346 ASSERT_EQ(args.values, values);
3347 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3348}
3349
3350TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3351 setGyroProperties();
3352 prepareGyroAxes();
3353 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3354
3355 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3356 std::chrono::microseconds(10000),
3357 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003358 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003359 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3360 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3361 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3362 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3363 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003364
3365 NotifySensorArgs args;
3366 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3367 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3368 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3369
3370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3371 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3372 ASSERT_EQ(args.deviceId, DEVICE_ID);
3373 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3374 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3375 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3376 ASSERT_EQ(args.values, values);
3377 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3378}
3379
Michael Wrightd02c5b62014-02-10 15:10:22 -08003380// --- KeyboardInputMapperTest ---
3381
3382class KeyboardInputMapperTest : public InputMapperTest {
3383protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003384 const std::string UNIQUE_ID = "local:0";
3385
3386 void prepareDisplay(int32_t orientation);
3387
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003388 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003389 int32_t originalKeyCode, int32_t rotatedKeyCode,
3390 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003391};
3392
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003393/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3394 * orientation.
3395 */
3396void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003397 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3398 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003399}
3400
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003401void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003402 int32_t originalScanCode, int32_t originalKeyCode,
3403 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003404 NotifyKeyArgs args;
3405
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003406 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3408 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3409 ASSERT_EQ(originalScanCode, args.scanCode);
3410 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003411 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003412
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003413 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3415 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3416 ASSERT_EQ(originalScanCode, args.scanCode);
3417 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003418 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003419}
3420
Michael Wrightd02c5b62014-02-10 15:10:22 -08003421TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003422 KeyboardInputMapper& mapper =
3423 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3424 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003425
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003426 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003427}
3428
3429TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3430 const int32_t USAGE_A = 0x070004;
3431 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003432 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3433 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003434 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3435 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3436 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003437
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003438 KeyboardInputMapper& mapper =
3439 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3440 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003441 // Initial metastate is AMETA_NONE.
3442 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003443
3444 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003445 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003446 NotifyKeyArgs args;
3447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3448 ASSERT_EQ(DEVICE_ID, args.deviceId);
3449 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3450 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3451 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3452 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3453 ASSERT_EQ(KEY_HOME, args.scanCode);
3454 ASSERT_EQ(AMETA_NONE, args.metaState);
3455 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3456 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3457 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3458
3459 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003460 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3462 ASSERT_EQ(DEVICE_ID, args.deviceId);
3463 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3464 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3465 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3466 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3467 ASSERT_EQ(KEY_HOME, args.scanCode);
3468 ASSERT_EQ(AMETA_NONE, args.metaState);
3469 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3470 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3471 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3472
3473 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003474 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3477 ASSERT_EQ(DEVICE_ID, args.deviceId);
3478 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3479 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3480 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3481 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3482 ASSERT_EQ(0, args.scanCode);
3483 ASSERT_EQ(AMETA_NONE, args.metaState);
3484 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3485 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3486 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3487
3488 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003489 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3490 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3492 ASSERT_EQ(DEVICE_ID, args.deviceId);
3493 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3494 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3495 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3496 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3497 ASSERT_EQ(0, args.scanCode);
3498 ASSERT_EQ(AMETA_NONE, args.metaState);
3499 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3500 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3501 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3502
3503 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003504 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3505 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3507 ASSERT_EQ(DEVICE_ID, args.deviceId);
3508 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3509 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3510 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3511 ASSERT_EQ(0, args.keyCode);
3512 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3513 ASSERT_EQ(AMETA_NONE, args.metaState);
3514 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3515 ASSERT_EQ(0U, args.policyFlags);
3516 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3517
3518 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3520 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3522 ASSERT_EQ(DEVICE_ID, args.deviceId);
3523 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3524 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3525 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3526 ASSERT_EQ(0, args.keyCode);
3527 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3528 ASSERT_EQ(AMETA_NONE, args.metaState);
3529 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3530 ASSERT_EQ(0U, args.policyFlags);
3531 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3532}
3533
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003534/**
3535 * Ensure that the readTime is set to the time when the EV_KEY is received.
3536 */
3537TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3538 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3539
3540 KeyboardInputMapper& mapper =
3541 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3542 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3543 NotifyKeyArgs args;
3544
3545 // Key down
3546 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3548 ASSERT_EQ(12, args.readTime);
3549
3550 // Key up
3551 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3553 ASSERT_EQ(15, args.readTime);
3554}
3555
Michael Wrightd02c5b62014-02-10 15:10:22 -08003556TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003557 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3558 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003559 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3560 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3561 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003562
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003563 KeyboardInputMapper& mapper =
3564 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3565 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003566
Arthur Hung95f68612022-04-07 14:08:22 +08003567 // Initial metastate is AMETA_NONE.
3568 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003569
3570 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003571 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003572 NotifyKeyArgs args;
3573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3574 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003575 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003576 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003577
3578 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003579 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3581 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003582 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003583
3584 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003585 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3587 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003588 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003589
3590 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003591 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3593 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003594 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003595 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003596}
3597
3598TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003599 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3600 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3601 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3602 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003603
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003604 KeyboardInputMapper& mapper =
3605 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3606 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003607
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003608 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003609 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3610 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3611 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3612 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3613 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3614 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3615 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3616 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3617}
3618
3619TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003620 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3621 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3622 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3623 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003624
Michael Wrightd02c5b62014-02-10 15:10:22 -08003625 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003626 KeyboardInputMapper& mapper =
3627 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3628 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003629
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003630 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003631 ASSERT_NO_FATAL_FAILURE(
3632 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3633 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3634 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3635 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3636 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3637 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3638 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003639
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003640 clearViewports();
3641 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003642 ASSERT_NO_FATAL_FAILURE(
3643 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3644 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3645 AKEYCODE_DPAD_UP, DISPLAY_ID));
3646 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3647 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3648 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3649 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003650
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003651 clearViewports();
3652 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003653 ASSERT_NO_FATAL_FAILURE(
3654 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3655 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3656 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3657 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3658 AKEYCODE_DPAD_UP, DISPLAY_ID));
3659 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3660 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003661
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003662 clearViewports();
3663 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003664 ASSERT_NO_FATAL_FAILURE(
3665 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3666 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3667 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3668 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3669 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3670 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3671 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003672
3673 // Special case: if orientation changes while key is down, we still emit the same keycode
3674 // in the key up as we did in the key down.
3675 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003676 clearViewports();
3677 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003678 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3680 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3681 ASSERT_EQ(KEY_UP, args.scanCode);
3682 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3683
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003684 clearViewports();
3685 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3688 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3689 ASSERT_EQ(KEY_UP, args.scanCode);
3690 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3691}
3692
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003693TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3694 // If the keyboard is not orientation aware,
3695 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003696 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003697
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003698 KeyboardInputMapper& mapper =
3699 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3700 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003701 NotifyKeyArgs args;
3702
3703 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003704 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003706 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3708 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3709
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003710 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003711 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003713 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3715 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3716}
3717
3718TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3719 // If the keyboard is orientation aware,
3720 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003721 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003722
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003723 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003724 KeyboardInputMapper& mapper =
3725 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3726 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003727 NotifyKeyArgs args;
3728
3729 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3730 // ^--- already checked by the previous test
3731
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003732 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003733 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003734 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003736 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3738 ASSERT_EQ(DISPLAY_ID, args.displayId);
3739
3740 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003741 clearViewports();
3742 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003743 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003744 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003746 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3748 ASSERT_EQ(newDisplayId, args.displayId);
3749}
3750
Michael Wrightd02c5b62014-02-10 15:10:22 -08003751TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003752 KeyboardInputMapper& mapper =
3753 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3754 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003755
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003756 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003757 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003758
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003759 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003760 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003761}
3762
Philip Junker4af3b3d2021-12-14 10:36:55 +01003763TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3764 KeyboardInputMapper& mapper =
3765 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3766 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3767
3768 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3769 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3770 << "If a mapping is available, the result is equal to the mapping";
3771
3772 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3773 << "If no mapping is available, the result is the key location";
3774}
3775
Michael Wrightd02c5b62014-02-10 15:10:22 -08003776TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003777 KeyboardInputMapper& mapper =
3778 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3779 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003780
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003781 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003782 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003783
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003784 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003785 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003786}
3787
3788TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003789 KeyboardInputMapper& mapper =
3790 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3791 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003792
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003793 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003794
Michael Wrightd02c5b62014-02-10 15:10:22 -08003795 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003796 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003797 ASSERT_TRUE(flags[0]);
3798 ASSERT_FALSE(flags[1]);
3799}
3800
3801TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003802 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3803 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3804 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3805 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3806 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3807 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003808
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003809 KeyboardInputMapper& mapper =
3810 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3811 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003812 // Initial metastate is AMETA_NONE.
3813 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003814
3815 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003816 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3817 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3818 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003819
3820 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003821 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3822 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003823 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3824 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3825 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003826 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003827
3828 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003831 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3832 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3833 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003834 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003835
3836 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003837 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3838 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003839 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3840 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3841 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003842 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003843
3844 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003845 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3846 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003847 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3848 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3849 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003850 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003851
3852 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003853 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3854 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003855 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3856 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3857 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003858 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003859
3860 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003861 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3862 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003863 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3864 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3865 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003866 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003867}
3868
Chris Yea52ade12020-08-27 16:49:20 -07003869TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3870 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3871 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3872 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3873 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3874
3875 KeyboardInputMapper& mapper =
3876 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3877 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3878
Chris Yea52ade12020-08-27 16:49:20 -07003879 // Meta state should be AMETA_NONE after reset
3880 mapper.reset(ARBITRARY_TIME);
3881 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3882 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3883 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3884 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3885
3886 NotifyKeyArgs args;
3887 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003888 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3890 ASSERT_EQ(AMETA_NONE, args.metaState);
3891 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3892 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3893 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3894
3895 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003896 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3898 ASSERT_EQ(AMETA_NONE, args.metaState);
3899 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3900 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3901 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3902}
3903
Arthur Hung2c9a3342019-07-23 14:18:59 +08003904TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3905 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003906 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3907 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3908 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3909 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003910
3911 // keyboard 2.
3912 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003913 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003914 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003915 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003916 std::shared_ptr<InputDevice> device2 =
3917 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003918 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003919
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003920 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3921 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3922 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3923 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003924
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003925 KeyboardInputMapper& mapper =
3926 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3927 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003928
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003929 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003930 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003931 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003932 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3933 device2->reset(ARBITRARY_TIME);
3934
3935 // Prepared displays and associated info.
3936 constexpr uint8_t hdmi1 = 0;
3937 constexpr uint8_t hdmi2 = 1;
3938 const std::string SECONDARY_UNIQUE_ID = "local:1";
3939
3940 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3941 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3942
3943 // No associated display viewport found, should disable the device.
3944 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3945 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3946 ASSERT_FALSE(device2->isEnabled());
3947
3948 // Prepare second display.
3949 constexpr int32_t newDisplayId = 2;
3950 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003951 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003952 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003953 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003954 // Default device will reconfigure above, need additional reconfiguration for another device.
3955 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3956 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3957
3958 // Device should be enabled after the associated display is found.
3959 ASSERT_TRUE(mDevice->isEnabled());
3960 ASSERT_TRUE(device2->isEnabled());
3961
3962 // Test pad key events
3963 ASSERT_NO_FATAL_FAILURE(
3964 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3965 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3966 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3967 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3968 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3969 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3970 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3971
3972 ASSERT_NO_FATAL_FAILURE(
3973 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3974 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3975 AKEYCODE_DPAD_RIGHT, newDisplayId));
3976 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3977 AKEYCODE_DPAD_DOWN, newDisplayId));
3978 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3979 AKEYCODE_DPAD_LEFT, newDisplayId));
3980}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003981
arthurhungc903df12020-08-11 15:08:42 +08003982TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3983 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3984 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3985 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3986 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3987 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3988 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3989
3990 KeyboardInputMapper& mapper =
3991 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3992 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003993 // Initial metastate is AMETA_NONE.
3994 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003995
3996 // Initialization should have turned all of the lights off.
3997 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3998 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3999 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4000
4001 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004002 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4003 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004004 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4005 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4006
4007 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004008 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4009 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004010 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4011 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
4012
4013 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004014 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4015 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004016 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4017 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
4018
4019 mFakeEventHub->removeDevice(EVENTHUB_ID);
4020 mReader->loopOnce();
4021
4022 // keyboard 2 should default toggle keys.
4023 const std::string USB2 = "USB2";
4024 const std::string DEVICE_NAME2 = "KEYBOARD2";
4025 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4026 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4027 std::shared_ptr<InputDevice> device2 =
4028 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004029 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08004030 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4031 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4032 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4033 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4034 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4035 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4036
arthurhung6fe95782020-10-05 22:41:16 +08004037 KeyboardInputMapper& mapper2 =
4038 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4039 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08004040 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4041 device2->reset(ARBITRARY_TIME);
4042
4043 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4044 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4045 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004046 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4047 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004048}
4049
Arthur Hungcb40a002021-08-03 14:31:01 +00004050TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4051 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4052 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4053 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4054
4055 // Suppose we have two mappers. (DPAD + KEYBOARD)
4056 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4057 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4058 KeyboardInputMapper& mapper =
4059 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4060 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004061 // Initial metastate is AMETA_NONE.
4062 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004063
4064 mReader->toggleCapsLockState(DEVICE_ID);
4065 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4066}
4067
Arthur Hungfb3cc112022-04-13 07:39:50 +00004068TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4069 // keyboard 1.
4070 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4071 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4072 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4073 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4074 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4075 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4076
4077 KeyboardInputMapper& mapper1 =
4078 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4079 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4080
4081 // keyboard 2.
4082 const std::string USB2 = "USB2";
4083 const std::string DEVICE_NAME2 = "KEYBOARD2";
4084 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4085 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4086 std::shared_ptr<InputDevice> device2 =
4087 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4088 ftl::Flags<InputDeviceClass>(0));
4089 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4090 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4091 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4092 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4093 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4094 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4095
4096 KeyboardInputMapper& mapper2 =
4097 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4098 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4099 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4100 device2->reset(ARBITRARY_TIME);
4101
Arthur Hung95f68612022-04-07 14:08:22 +08004102 // Initial metastate is AMETA_NONE.
4103 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4104 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4105
4106 // Toggle num lock on and off.
4107 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4108 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004109 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4110 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4111 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4112
4113 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4114 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4115 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4116 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4117 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4118
4119 // Toggle caps lock on and off.
4120 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4121 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4122 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4123 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4124 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4125
4126 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4127 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4128 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4129 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4130 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4131
4132 // Toggle scroll lock on and off.
4133 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4134 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4135 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4136 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4137 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4138
4139 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4140 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4141 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4142 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4143 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4144}
4145
Arthur Hung2141d542022-08-23 07:45:21 +00004146TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4147 const int32_t USAGE_A = 0x070004;
4148 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4149 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4150
4151 KeyboardInputMapper& mapper =
4152 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4153 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4154 // Key down by scan code.
4155 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4156 NotifyKeyArgs args;
4157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4158 ASSERT_EQ(DEVICE_ID, args.deviceId);
4159 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4160 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4161 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4162 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4163 ASSERT_EQ(KEY_HOME, args.scanCode);
4164 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4165
4166 // Disable device, it should synthesize cancellation events for down events.
4167 mFakePolicy->addDisabledDevice(DEVICE_ID);
4168 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
4169
4170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4171 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4172 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4173 ASSERT_EQ(KEY_HOME, args.scanCode);
4174 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4175}
4176
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004177// --- KeyboardInputMapperTest_ExternalDevice ---
4178
4179class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4180protected:
Chris Yea52ade12020-08-27 16:49:20 -07004181 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004182};
4183
4184TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004185 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4186 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004187
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004188 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4189 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4190 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4191 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004192
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004193 KeyboardInputMapper& mapper =
4194 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4195 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004196
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004198 NotifyKeyArgs args;
4199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4200 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4201
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004202 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4204 ASSERT_EQ(uint32_t(0), args.policyFlags);
4205
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004206 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4208 ASSERT_EQ(uint32_t(0), args.policyFlags);
4209
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004210 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4212 ASSERT_EQ(uint32_t(0), args.policyFlags);
4213
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004214 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4216 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4217
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004218 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4220 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4221}
4222
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004223TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004224 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004225
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004226 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4227 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4228 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004229
Powei Fengd041c5d2019-05-03 17:11:33 -07004230 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004231 KeyboardInputMapper& mapper =
4232 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4233 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004234
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004235 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004236 NotifyKeyArgs args;
4237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4238 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4239
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004240 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4242 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4243
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004244 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4246 ASSERT_EQ(uint32_t(0), args.policyFlags);
4247
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004248 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4250 ASSERT_EQ(uint32_t(0), args.policyFlags);
4251
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004252 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4254 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4255
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004256 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4258 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4259}
4260
Michael Wrightd02c5b62014-02-10 15:10:22 -08004261// --- CursorInputMapperTest ---
4262
4263class CursorInputMapperTest : public InputMapperTest {
4264protected:
4265 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4266
Michael Wright17db18e2020-06-26 20:51:44 +01004267 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004268
Chris Yea52ade12020-08-27 16:49:20 -07004269 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004270 InputMapperTest::SetUp();
4271
Michael Wright17db18e2020-06-26 20:51:44 +01004272 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004273 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274 }
4275
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004276 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4277 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004278
4279 void prepareDisplay(int32_t orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004280 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4281 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4282 }
4283
4284 void prepareSecondaryDisplay() {
4285 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4286 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
4287 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004288 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004289
4290 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4291 float pressure) {
4292 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4293 0.0f, 0.0f, 0.0f, EPSILON));
4294 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295};
4296
4297const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4298
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004299void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4300 int32_t originalY, int32_t rotatedX,
4301 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004302 NotifyMotionArgs args;
4303
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004304 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4305 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4306 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4308 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004309 ASSERT_NO_FATAL_FAILURE(
4310 assertCursorPointerCoords(args.pointerCoords[0],
4311 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4312 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004313}
4314
4315TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004316 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004317 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004318
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004319 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004320}
4321
4322TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004323 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004324 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004325
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004326 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004327}
4328
4329TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004330 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004331 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332
4333 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004334 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004335
4336 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004337 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4338 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004339 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4340 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4341
4342 // When the bounds are set, then there should be a valid motion range.
4343 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4344
4345 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004346 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004347
4348 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4349 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4350 1, 800 - 1, 0.0f, 0.0f));
4351 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4352 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4353 2, 480 - 1, 0.0f, 0.0f));
4354 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4355 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4356 0.0f, 1.0f, 0.0f, 0.0f));
4357}
4358
4359TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004360 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004361 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004362
4363 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004364 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004365
4366 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4367 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4368 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4369 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4370 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4371 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4372 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4373 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4374 0.0f, 1.0f, 0.0f, 0.0f));
4375}
4376
4377TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004378 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004379 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004380
arthurhungdcef2dc2020-08-11 14:47:50 +08004381 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004382
4383 NotifyMotionArgs args;
4384
4385 // Button press.
4386 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004387 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4388 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4390 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4391 ASSERT_EQ(DEVICE_ID, args.deviceId);
4392 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4393 ASSERT_EQ(uint32_t(0), args.policyFlags);
4394 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4395 ASSERT_EQ(0, args.flags);
4396 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4397 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4398 ASSERT_EQ(0, args.edgeFlags);
4399 ASSERT_EQ(uint32_t(1), args.pointerCount);
4400 ASSERT_EQ(0, args.pointerProperties[0].id);
4401 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004402 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4404 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4405 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4406
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4408 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4409 ASSERT_EQ(DEVICE_ID, args.deviceId);
4410 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4411 ASSERT_EQ(uint32_t(0), args.policyFlags);
4412 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4413 ASSERT_EQ(0, args.flags);
4414 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4415 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4416 ASSERT_EQ(0, args.edgeFlags);
4417 ASSERT_EQ(uint32_t(1), args.pointerCount);
4418 ASSERT_EQ(0, args.pointerProperties[0].id);
4419 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004420 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004421 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4422 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4423 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4424
Michael Wrightd02c5b62014-02-10 15:10:22 -08004425 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004426 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4427 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4429 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4430 ASSERT_EQ(DEVICE_ID, args.deviceId);
4431 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4432 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004433 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4434 ASSERT_EQ(0, args.flags);
4435 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4436 ASSERT_EQ(0, args.buttonState);
4437 ASSERT_EQ(0, args.edgeFlags);
4438 ASSERT_EQ(uint32_t(1), args.pointerCount);
4439 ASSERT_EQ(0, args.pointerProperties[0].id);
4440 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004441 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004442 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4443 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4444 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4445
4446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4447 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4448 ASSERT_EQ(DEVICE_ID, args.deviceId);
4449 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4450 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004451 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4452 ASSERT_EQ(0, args.flags);
4453 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4454 ASSERT_EQ(0, args.buttonState);
4455 ASSERT_EQ(0, args.edgeFlags);
4456 ASSERT_EQ(uint32_t(1), args.pointerCount);
4457 ASSERT_EQ(0, args.pointerProperties[0].id);
4458 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004459 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004460 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4461 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4462 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4463}
4464
4465TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004467 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004468
4469 NotifyMotionArgs args;
4470
4471 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004472 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4473 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4475 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004476 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4477 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4478 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479
4480 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004481 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4482 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4484 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004485 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4486 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004487}
4488
4489TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004490 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004491 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004492
4493 NotifyMotionArgs args;
4494
4495 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004496 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4497 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4499 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004500 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004501
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4503 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004504 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004505
Michael Wrightd02c5b62014-02-10 15:10:22 -08004506 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004507 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4508 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004510 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004511 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004512
4513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004514 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004515 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004516}
4517
4518TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004519 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004520 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004521
4522 NotifyMotionArgs args;
4523
4524 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004525 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4526 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4527 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4528 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4530 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004531 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4532 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4533 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004534
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4536 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004537 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4538 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4539 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004540
Michael Wrightd02c5b62014-02-10 15:10:22 -08004541 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004542 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4543 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4544 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4546 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004547 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4548 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4549 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004550
4551 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004552 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4553 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004555 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004556 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004557
4558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004559 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004560 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004561}
4562
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004563TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004564 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004565 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004566 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4567 // need to be rotated.
4568 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004569 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004570
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004571 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004572 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4573 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4574 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4575 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4576 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4577 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4578 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4579 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4580}
4581
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004582TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004583 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004584 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004585 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4586 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004587 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004588
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004589 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004590 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004591 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4592 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4593 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4594 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4595 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4596 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4597 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4598 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4599
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004600 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004601 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004602 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4603 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4604 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4605 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4606 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4607 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4608 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4609 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004610
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004611 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004612 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004613 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4614 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4615 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4616 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4617 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4618 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4619 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4620 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4621
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004622 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004623 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004624 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4625 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4626 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4627 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4628 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4629 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4630 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4631 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632}
4633
4634TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004635 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004636 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004637
4638 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4639 mFakePointerController->setPosition(100, 200);
4640 mFakePointerController->setButtonState(0);
4641
4642 NotifyMotionArgs motionArgs;
4643 NotifyKeyArgs keyArgs;
4644
4645 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004646 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4647 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4649 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4650 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4651 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004652 ASSERT_NO_FATAL_FAILURE(
4653 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004654
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4656 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4657 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4658 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004659 ASSERT_NO_FATAL_FAILURE(
4660 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004661
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004662 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4663 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004665 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004666 ASSERT_EQ(0, motionArgs.buttonState);
4667 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004668 ASSERT_NO_FATAL_FAILURE(
4669 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004670
4671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004672 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004673 ASSERT_EQ(0, motionArgs.buttonState);
4674 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004675 ASSERT_NO_FATAL_FAILURE(
4676 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004677
4678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004679 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004680 ASSERT_EQ(0, motionArgs.buttonState);
4681 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004682 ASSERT_NO_FATAL_FAILURE(
4683 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004684
4685 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4687 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4688 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4690 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4691 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4692 motionArgs.buttonState);
4693 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4694 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004695 ASSERT_NO_FATAL_FAILURE(
4696 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004697
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4699 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4700 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4701 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4702 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004703 ASSERT_NO_FATAL_FAILURE(
4704 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004705
4706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4707 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4708 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4709 motionArgs.buttonState);
4710 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4711 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004712 ASSERT_NO_FATAL_FAILURE(
4713 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004714
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004715 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4716 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004718 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004719 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4720 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004721 ASSERT_NO_FATAL_FAILURE(
4722 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004723
4724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004725 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004726 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4727 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004728 ASSERT_NO_FATAL_FAILURE(
4729 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004730
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004731 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4732 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004734 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4735 ASSERT_EQ(0, motionArgs.buttonState);
4736 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004737 ASSERT_NO_FATAL_FAILURE(
4738 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004739 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4740 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004741
4742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004743 ASSERT_EQ(0, motionArgs.buttonState);
4744 ASSERT_EQ(0, mFakePointerController->getButtonState());
4745 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004746 ASSERT_NO_FATAL_FAILURE(
4747 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004748
Michael Wrightd02c5b62014-02-10 15:10:22 -08004749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4750 ASSERT_EQ(0, motionArgs.buttonState);
4751 ASSERT_EQ(0, mFakePointerController->getButtonState());
4752 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004753 ASSERT_NO_FATAL_FAILURE(
4754 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004755
4756 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004757 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4758 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4760 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4761 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004762
Michael Wrightd02c5b62014-02-10 15:10:22 -08004763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004764 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004765 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4766 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004767 ASSERT_NO_FATAL_FAILURE(
4768 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004769
4770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4771 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4772 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4773 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004774 ASSERT_NO_FATAL_FAILURE(
4775 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004776
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004777 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4778 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004780 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004781 ASSERT_EQ(0, motionArgs.buttonState);
4782 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004783 ASSERT_NO_FATAL_FAILURE(
4784 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004785
4786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004787 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004788 ASSERT_EQ(0, motionArgs.buttonState);
4789 ASSERT_EQ(0, mFakePointerController->getButtonState());
4790
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004791 ASSERT_NO_FATAL_FAILURE(
4792 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4794 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4795 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4796
4797 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004798 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4801 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4802 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004803
Michael Wrightd02c5b62014-02-10 15:10:22 -08004804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004805 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004806 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4807 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004808 ASSERT_NO_FATAL_FAILURE(
4809 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004810
4811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4812 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4813 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4814 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004815 ASSERT_NO_FATAL_FAILURE(
4816 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004817
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004818 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4819 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004821 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004822 ASSERT_EQ(0, motionArgs.buttonState);
4823 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004824 ASSERT_NO_FATAL_FAILURE(
4825 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004826
4827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4828 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4829 ASSERT_EQ(0, motionArgs.buttonState);
4830 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004831 ASSERT_NO_FATAL_FAILURE(
4832 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004833
Michael Wrightd02c5b62014-02-10 15:10:22 -08004834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4835 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4836 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4837
4838 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004839 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4840 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4842 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4843 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004844
Michael Wrightd02c5b62014-02-10 15:10:22 -08004845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004846 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004847 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4848 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004849 ASSERT_NO_FATAL_FAILURE(
4850 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004851
4852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4853 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4854 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4855 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004856 ASSERT_NO_FATAL_FAILURE(
4857 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004858
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004859 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004862 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004863 ASSERT_EQ(0, motionArgs.buttonState);
4864 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004865 ASSERT_NO_FATAL_FAILURE(
4866 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004867
4868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4869 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4870 ASSERT_EQ(0, motionArgs.buttonState);
4871 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004872 ASSERT_NO_FATAL_FAILURE(
4873 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004874
Michael Wrightd02c5b62014-02-10 15:10:22 -08004875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4876 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4877 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4878
4879 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004880 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4881 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4883 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4884 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004885
Michael Wrightd02c5b62014-02-10 15:10:22 -08004886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004887 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004888 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4889 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004890 ASSERT_NO_FATAL_FAILURE(
4891 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004892
4893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4894 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4895 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4896 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004897 ASSERT_NO_FATAL_FAILURE(
4898 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004899
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004900 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4901 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004903 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004904 ASSERT_EQ(0, motionArgs.buttonState);
4905 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004906 ASSERT_NO_FATAL_FAILURE(
4907 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004908
4909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4910 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4911 ASSERT_EQ(0, motionArgs.buttonState);
4912 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004913 ASSERT_NO_FATAL_FAILURE(
4914 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004915
Michael Wrightd02c5b62014-02-10 15:10:22 -08004916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4917 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4918 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4919}
4920
4921TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004922 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004923 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004924
4925 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4926 mFakePointerController->setPosition(100, 200);
4927 mFakePointerController->setButtonState(0);
4928
4929 NotifyMotionArgs args;
4930
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004931 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4932 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
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->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004935 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4936 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4937 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4938 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 +01004939 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004940}
4941
4942TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004943 addConfigurationProperty("cursor.mode", "pointer");
4944 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004945 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004946
4947 NotifyDeviceResetArgs resetArgs;
4948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4949 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4950 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4951
4952 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4953 mFakePointerController->setPosition(100, 200);
4954 mFakePointerController->setButtonState(0);
4955
4956 NotifyMotionArgs args;
4957
4958 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004959 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4960 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4961 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4963 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4964 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4965 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4966 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 +01004967 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004968
4969 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004970 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4971 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4973 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4974 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4975 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4976 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4978 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4979 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4980 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4981 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4982
4983 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004984 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4985 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4987 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4988 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4989 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4990 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4992 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4993 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4994 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4995 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4996
4997 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004998 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4999 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
5000 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5002 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5003 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5004 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5005 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 +01005006 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005007
5008 // Disable pointer capture and check that the device generation got bumped
5009 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08005010 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005011 mFakePolicy->setPointerCapture(false);
5012 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08005013 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005014
5015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005016 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5017
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005018 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5019 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5020 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5022 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005023 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5024 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5025 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 +01005026 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005027}
5028
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00005029/**
5030 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
5031 * pointer acceleration or speed processing should not be applied.
5032 */
5033TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
5034 addConfigurationProperty("cursor.mode", "pointer");
5035 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
5036 100.f /*high threshold*/, 10.f /*acceleration*/);
5037 mFakePolicy->setVelocityControlParams(testParams);
5038 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5039
5040 NotifyDeviceResetArgs resetArgs;
5041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5042 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5043 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5044
5045 NotifyMotionArgs args;
5046
5047 // Move and verify scale is applied.
5048 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5049 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5050 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5052 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5053 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5054 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5055 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5056 ASSERT_GT(relX, 10);
5057 ASSERT_GT(relY, 20);
5058
5059 // Enable Pointer Capture
5060 mFakePolicy->setPointerCapture(true);
5061 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5062 NotifyPointerCaptureChangedArgs captureArgs;
5063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5064 ASSERT_TRUE(captureArgs.request.enable);
5065
5066 // Move and verify scale is not applied.
5067 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5068 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5069 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5071 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5072 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5073 ASSERT_EQ(10, args.pointerCoords[0].getX());
5074 ASSERT_EQ(20, args.pointerCoords[0].getY());
5075}
5076
Prabir Pradhan208360b2022-06-24 18:37:04 +00005077TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5078 addConfigurationProperty("cursor.mode", "pointer");
5079 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5080
5081 NotifyDeviceResetArgs resetArgs;
5082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5083 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5084 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5085
5086 // Ensure the display is rotated.
5087 prepareDisplay(DISPLAY_ORIENTATION_90);
5088
5089 NotifyMotionArgs args;
5090
5091 // Verify that the coordinates are rotated.
5092 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5093 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5094 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5096 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5097 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5098 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5099 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5100
5101 // Enable Pointer Capture.
5102 mFakePolicy->setPointerCapture(true);
5103 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5104 NotifyPointerCaptureChangedArgs captureArgs;
5105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5106 ASSERT_TRUE(captureArgs.request.enable);
5107
5108 // Move and verify rotation is not applied.
5109 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5110 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5111 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5113 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5114 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5115 ASSERT_EQ(10, args.pointerCoords[0].getX());
5116 ASSERT_EQ(20, args.pointerCoords[0].getY());
5117}
5118
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005119TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005120 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005121
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005122 // Set up the default display.
5123 prepareDisplay(DISPLAY_ORIENTATION_90);
5124
5125 // Set up the secondary display as the display on which the pointer should be shown.
5126 // The InputDevice is not associated with any display.
5127 prepareSecondaryDisplay();
5128 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08005129 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5130
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005131 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005132 mFakePointerController->setPosition(100, 200);
5133 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005134
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005135 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5137 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5138 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005140 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5141 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5142 WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01005143 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005144}
5145
5146TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
5147 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5148
5149 // Set up the default display.
5150 prepareDisplay(DISPLAY_ORIENTATION_90);
5151
5152 // Set up the secondary display as the display on which the pointer should be shown,
5153 // and associate the InputDevice with the secondary display.
5154 prepareSecondaryDisplay();
5155 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5156 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5157 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5158
5159 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5160 mFakePointerController->setPosition(100, 200);
5161 mFakePointerController->setButtonState(0);
5162
5163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5165 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005167 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5168 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5169 WithCoords(110.0f, 220.0f))));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005170 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
5171}
5172
5173TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
5174 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5175
5176 // Set up the default display as the display on which the pointer should be shown.
5177 prepareDisplay(DISPLAY_ORIENTATION_90);
5178 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5179
5180 // Associate the InputDevice with the secondary display.
5181 prepareSecondaryDisplay();
5182 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5183 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5184
5185 // The mapper should not generate any events because it is associated with a display that is
5186 // different from the pointer display.
5187 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5188 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5189 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005191}
5192
Michael Wrightd02c5b62014-02-10 15:10:22 -08005193// --- TouchInputMapperTest ---
5194
5195class TouchInputMapperTest : public InputMapperTest {
5196protected:
5197 static const int32_t RAW_X_MIN;
5198 static const int32_t RAW_X_MAX;
5199 static const int32_t RAW_Y_MIN;
5200 static const int32_t RAW_Y_MAX;
5201 static const int32_t RAW_TOUCH_MIN;
5202 static const int32_t RAW_TOUCH_MAX;
5203 static const int32_t RAW_TOOL_MIN;
5204 static const int32_t RAW_TOOL_MAX;
5205 static const int32_t RAW_PRESSURE_MIN;
5206 static const int32_t RAW_PRESSURE_MAX;
5207 static const int32_t RAW_ORIENTATION_MIN;
5208 static const int32_t RAW_ORIENTATION_MAX;
5209 static const int32_t RAW_DISTANCE_MIN;
5210 static const int32_t RAW_DISTANCE_MAX;
5211 static const int32_t RAW_TILT_MIN;
5212 static const int32_t RAW_TILT_MAX;
5213 static const int32_t RAW_ID_MIN;
5214 static const int32_t RAW_ID_MAX;
5215 static const int32_t RAW_SLOT_MIN;
5216 static const int32_t RAW_SLOT_MAX;
5217 static const float X_PRECISION;
5218 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005219 static const float X_PRECISION_VIRTUAL;
5220 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005221
5222 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005223 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005224
5225 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5226
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005227 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005228 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005229
Michael Wrightd02c5b62014-02-10 15:10:22 -08005230 enum Axes {
5231 POSITION = 1 << 0,
5232 TOUCH = 1 << 1,
5233 TOOL = 1 << 2,
5234 PRESSURE = 1 << 3,
5235 ORIENTATION = 1 << 4,
5236 MINOR = 1 << 5,
5237 ID = 1 << 6,
5238 DISTANCE = 1 << 7,
5239 TILT = 1 << 8,
5240 SLOT = 1 << 9,
5241 TOOL_TYPE = 1 << 10,
5242 };
5243
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005244 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5245 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005246 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005247 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005248 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005249 int32_t toRawX(float displayX);
5250 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005251 int32_t toRotatedRawX(float displayX);
5252 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005253 float toCookedX(float rawX, float rawY);
5254 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005255 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005256 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005257 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005258 float toDisplayY(int32_t rawY, int32_t displayHeight);
5259
Michael Wrightd02c5b62014-02-10 15:10:22 -08005260};
5261
5262const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5263const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5264const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5265const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5266const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5267const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5268const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5269const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005270const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5271const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005272const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5273const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5274const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5275const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5276const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5277const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5278const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5279const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5280const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5281const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5282const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5283const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005284const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5285 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5286const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5287 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005288const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5289 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005290
5291const float TouchInputMapperTest::GEOMETRIC_SCALE =
5292 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5293 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5294
5295const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5296 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5297 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5298};
5299
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005300void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005301 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5302 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005303}
5304
5305void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5306 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5307 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005308}
5309
Santos Cordonfa5cf462017-04-05 10:37:00 -07005310void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005311 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5312 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5313 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005314}
5315
Michael Wrightd02c5b62014-02-10 15:10:22 -08005316void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005317 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5318 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5319 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5320 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005321}
5322
Jason Gerecke489fda82012-09-07 17:19:40 -07005323void TouchInputMapperTest::prepareLocationCalibration() {
5324 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5325}
5326
Michael Wrightd02c5b62014-02-10 15:10:22 -08005327int32_t TouchInputMapperTest::toRawX(float displayX) {
5328 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5329}
5330
5331int32_t TouchInputMapperTest::toRawY(float displayY) {
5332 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5333}
5334
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005335int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5336 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5337}
5338
5339int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5340 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5341}
5342
Jason Gerecke489fda82012-09-07 17:19:40 -07005343float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5344 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5345 return rawX;
5346}
5347
5348float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5349 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5350 return rawY;
5351}
5352
Michael Wrightd02c5b62014-02-10 15:10:22 -08005353float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005354 return toDisplayX(rawX, DISPLAY_WIDTH);
5355}
5356
5357float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5358 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005359}
5360
5361float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005362 return toDisplayY(rawY, DISPLAY_HEIGHT);
5363}
5364
5365float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5366 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005367}
5368
5369
5370// --- SingleTouchInputMapperTest ---
5371
5372class SingleTouchInputMapperTest : public TouchInputMapperTest {
5373protected:
5374 void prepareButtons();
5375 void prepareAxes(int axes);
5376
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005377 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5378 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5379 void processUp(SingleTouchInputMapper& mappery);
5380 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5381 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5382 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5383 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5384 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5385 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005386};
5387
5388void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005389 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005390}
5391
5392void SingleTouchInputMapperTest::prepareAxes(int axes) {
5393 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005394 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5395 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005396 }
5397 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005398 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5399 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005400 }
5401 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005402 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5403 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005404 }
5405 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005406 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5407 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005408 }
5409 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005410 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5411 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005412 }
5413}
5414
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005415void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005416 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5417 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5418 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005419}
5420
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005421void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005422 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5423 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005424}
5425
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005426void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005427 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005428}
5429
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005430void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005431 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005432}
5433
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005434void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5435 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005436 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005437}
5438
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005439void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005440 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005441}
5442
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005443void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5444 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005445 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005447}
5448
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005449void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5450 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005451 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005452}
5453
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005454void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005455 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005456}
5457
Michael Wrightd02c5b62014-02-10 15:10:22 -08005458TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005459 prepareButtons();
5460 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005461 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005462
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005463 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005464}
5465
Michael Wrightd02c5b62014-02-10 15:10:22 -08005466TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005467 prepareButtons();
5468 prepareAxes(POSITION);
5469 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005470 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005471
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005472 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005473}
5474
5475TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005476 addConfigurationProperty("touch.deviceType", "touchScreen");
5477 prepareDisplay(DISPLAY_ORIENTATION_0);
5478 prepareButtons();
5479 prepareAxes(POSITION);
5480 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005481 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005482
5483 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005484 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005485
5486 // Virtual key is down.
5487 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5488 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5489 processDown(mapper, x, y);
5490 processSync(mapper);
5491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5492
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005493 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005494
5495 // Virtual key is up.
5496 processUp(mapper);
5497 processSync(mapper);
5498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5499
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005500 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005501}
5502
5503TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005504 addConfigurationProperty("touch.deviceType", "touchScreen");
5505 prepareDisplay(DISPLAY_ORIENTATION_0);
5506 prepareButtons();
5507 prepareAxes(POSITION);
5508 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005509 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005510
5511 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005512 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005513
5514 // Virtual key is down.
5515 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5516 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5517 processDown(mapper, x, y);
5518 processSync(mapper);
5519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5520
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005521 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005522
5523 // Virtual key is up.
5524 processUp(mapper);
5525 processSync(mapper);
5526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5527
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005528 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005529}
5530
5531TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005532 addConfigurationProperty("touch.deviceType", "touchScreen");
5533 prepareDisplay(DISPLAY_ORIENTATION_0);
5534 prepareButtons();
5535 prepareAxes(POSITION);
5536 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005537 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005538
Michael Wrightd02c5b62014-02-10 15:10:22 -08005539 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005540 ASSERT_TRUE(
5541 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005542 ASSERT_TRUE(flags[0]);
5543 ASSERT_FALSE(flags[1]);
5544}
5545
5546TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005547 addConfigurationProperty("touch.deviceType", "touchScreen");
5548 prepareDisplay(DISPLAY_ORIENTATION_0);
5549 prepareButtons();
5550 prepareAxes(POSITION);
5551 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005552 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005553
arthurhungdcef2dc2020-08-11 14:47:50 +08005554 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005555
5556 NotifyKeyArgs args;
5557
5558 // Press virtual key.
5559 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5560 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5561 processDown(mapper, x, y);
5562 processSync(mapper);
5563
5564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5565 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5566 ASSERT_EQ(DEVICE_ID, args.deviceId);
5567 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5568 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5569 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5570 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5571 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5572 ASSERT_EQ(KEY_HOME, args.scanCode);
5573 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5574 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5575
5576 // Release virtual key.
5577 processUp(mapper);
5578 processSync(mapper);
5579
5580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5581 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5582 ASSERT_EQ(DEVICE_ID, args.deviceId);
5583 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5584 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5585 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5586 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5587 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5588 ASSERT_EQ(KEY_HOME, args.scanCode);
5589 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5590 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5591
5592 // Should not have sent any motions.
5593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5594}
5595
5596TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005597 addConfigurationProperty("touch.deviceType", "touchScreen");
5598 prepareDisplay(DISPLAY_ORIENTATION_0);
5599 prepareButtons();
5600 prepareAxes(POSITION);
5601 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005602 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005603
arthurhungdcef2dc2020-08-11 14:47:50 +08005604 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005605
5606 NotifyKeyArgs keyArgs;
5607
5608 // Press virtual key.
5609 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5610 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5611 processDown(mapper, x, y);
5612 processSync(mapper);
5613
5614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5615 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5616 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5617 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5618 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5619 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5620 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5621 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5622 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5623 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5624 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5625
5626 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5627 // into the display area.
5628 y -= 100;
5629 processMove(mapper, x, y);
5630 processSync(mapper);
5631
5632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5633 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5634 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5635 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5636 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5637 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5638 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5639 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5640 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5641 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5642 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5643 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5644
5645 NotifyMotionArgs motionArgs;
5646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5647 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5648 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5649 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5650 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5651 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5652 ASSERT_EQ(0, motionArgs.flags);
5653 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5654 ASSERT_EQ(0, motionArgs.buttonState);
5655 ASSERT_EQ(0, motionArgs.edgeFlags);
5656 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5657 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5658 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5660 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5661 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5662 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5663 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5664
5665 // Keep moving out of bounds. Should generate a pointer move.
5666 y -= 50;
5667 processMove(mapper, x, y);
5668 processSync(mapper);
5669
5670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5671 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5672 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5673 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5674 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5675 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5676 ASSERT_EQ(0, motionArgs.flags);
5677 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5678 ASSERT_EQ(0, motionArgs.buttonState);
5679 ASSERT_EQ(0, motionArgs.edgeFlags);
5680 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5681 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5682 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5683 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5684 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5685 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5686 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5687 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5688
5689 // Release out of bounds. Should generate a pointer up.
5690 processUp(mapper);
5691 processSync(mapper);
5692
5693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5694 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5695 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5696 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5697 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5698 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5699 ASSERT_EQ(0, motionArgs.flags);
5700 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5701 ASSERT_EQ(0, motionArgs.buttonState);
5702 ASSERT_EQ(0, motionArgs.edgeFlags);
5703 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5704 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5705 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5706 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5707 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5708 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5709 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5710 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5711
5712 // Should not have sent any more keys or motions.
5713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5715}
5716
5717TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005718 addConfigurationProperty("touch.deviceType", "touchScreen");
5719 prepareDisplay(DISPLAY_ORIENTATION_0);
5720 prepareButtons();
5721 prepareAxes(POSITION);
5722 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005723 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005724
arthurhungdcef2dc2020-08-11 14:47:50 +08005725 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005726
5727 NotifyMotionArgs motionArgs;
5728
5729 // Initially go down out of bounds.
5730 int32_t x = -10;
5731 int32_t y = -10;
5732 processDown(mapper, x, y);
5733 processSync(mapper);
5734
5735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5736
5737 // Move into the display area. Should generate a pointer down.
5738 x = 50;
5739 y = 75;
5740 processMove(mapper, x, y);
5741 processSync(mapper);
5742
5743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5744 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5745 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5746 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5747 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5748 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5749 ASSERT_EQ(0, motionArgs.flags);
5750 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5751 ASSERT_EQ(0, motionArgs.buttonState);
5752 ASSERT_EQ(0, motionArgs.edgeFlags);
5753 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5754 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5755 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5756 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5757 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5758 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5759 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5760 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5761
5762 // Release. Should generate a pointer up.
5763 processUp(mapper);
5764 processSync(mapper);
5765
5766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5767 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5768 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5769 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5770 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5771 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5772 ASSERT_EQ(0, motionArgs.flags);
5773 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5774 ASSERT_EQ(0, motionArgs.buttonState);
5775 ASSERT_EQ(0, motionArgs.edgeFlags);
5776 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5777 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5778 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5780 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5781 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5782 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5783 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5784
5785 // Should not have sent any more keys or motions.
5786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5788}
5789
Santos Cordonfa5cf462017-04-05 10:37:00 -07005790TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005791 addConfigurationProperty("touch.deviceType", "touchScreen");
5792 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5793
5794 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5795 prepareButtons();
5796 prepareAxes(POSITION);
5797 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005798 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005799
arthurhungdcef2dc2020-08-11 14:47:50 +08005800 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005801
5802 NotifyMotionArgs motionArgs;
5803
5804 // Down.
5805 int32_t x = 100;
5806 int32_t y = 125;
5807 processDown(mapper, x, y);
5808 processSync(mapper);
5809
5810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5811 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5812 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5813 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5814 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5815 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5816 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5817 ASSERT_EQ(0, motionArgs.flags);
5818 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5819 ASSERT_EQ(0, motionArgs.buttonState);
5820 ASSERT_EQ(0, motionArgs.edgeFlags);
5821 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5822 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5823 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5824 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5825 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5826 1, 0, 0, 0, 0, 0, 0, 0));
5827 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5828 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5829 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5830
5831 // Move.
5832 x += 50;
5833 y += 75;
5834 processMove(mapper, x, y);
5835 processSync(mapper);
5836
5837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5838 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5839 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5840 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5841 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5842 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5843 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5844 ASSERT_EQ(0, motionArgs.flags);
5845 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5846 ASSERT_EQ(0, motionArgs.buttonState);
5847 ASSERT_EQ(0, motionArgs.edgeFlags);
5848 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5849 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5850 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5851 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5852 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5853 1, 0, 0, 0, 0, 0, 0, 0));
5854 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5855 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5856 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5857
5858 // Up.
5859 processUp(mapper);
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_UP, 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 // Should not have sent any more keys or motions.
5884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5886}
5887
Michael Wrightd02c5b62014-02-10 15:10:22 -08005888TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005889 addConfigurationProperty("touch.deviceType", "touchScreen");
5890 prepareDisplay(DISPLAY_ORIENTATION_0);
5891 prepareButtons();
5892 prepareAxes(POSITION);
5893 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005894 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005895
arthurhungdcef2dc2020-08-11 14:47:50 +08005896 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005897
5898 NotifyMotionArgs motionArgs;
5899
5900 // Down.
5901 int32_t x = 100;
5902 int32_t y = 125;
5903 processDown(mapper, x, y);
5904 processSync(mapper);
5905
5906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5907 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5908 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5909 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5910 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5911 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5912 ASSERT_EQ(0, motionArgs.flags);
5913 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5914 ASSERT_EQ(0, motionArgs.buttonState);
5915 ASSERT_EQ(0, motionArgs.edgeFlags);
5916 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5917 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5918 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5920 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5921 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5922 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5923 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5924
5925 // Move.
5926 x += 50;
5927 y += 75;
5928 processMove(mapper, x, y);
5929 processSync(mapper);
5930
5931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5932 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5933 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5934 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5935 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5936 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5937 ASSERT_EQ(0, motionArgs.flags);
5938 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5939 ASSERT_EQ(0, motionArgs.buttonState);
5940 ASSERT_EQ(0, motionArgs.edgeFlags);
5941 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5942 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5943 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5944 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5945 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5946 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5947 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5948 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5949
5950 // Up.
5951 processUp(mapper);
5952 processSync(mapper);
5953
5954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5955 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5956 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5957 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5958 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5959 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5960 ASSERT_EQ(0, motionArgs.flags);
5961 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5962 ASSERT_EQ(0, motionArgs.buttonState);
5963 ASSERT_EQ(0, motionArgs.edgeFlags);
5964 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5965 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5966 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5967 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5968 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5969 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5970 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5971 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5972
5973 // Should not have sent any more keys or motions.
5974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5976}
5977
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005978TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005979 addConfigurationProperty("touch.deviceType", "touchScreen");
5980 prepareButtons();
5981 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005982 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5983 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005984 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005985
5986 NotifyMotionArgs args;
5987
5988 // Rotation 90.
5989 prepareDisplay(DISPLAY_ORIENTATION_90);
5990 processDown(mapper, toRawX(50), toRawY(75));
5991 processSync(mapper);
5992
5993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5994 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5995 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5996
5997 processUp(mapper);
5998 processSync(mapper);
5999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6000}
6001
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006002TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006003 addConfigurationProperty("touch.deviceType", "touchScreen");
6004 prepareButtons();
6005 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006006 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6007 // orientation-aware are affected by display rotation.
6008 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006009 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006010
6011 NotifyMotionArgs args;
6012
6013 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006014 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006015 prepareDisplay(DISPLAY_ORIENTATION_0);
6016 processDown(mapper, toRawX(50), toRawY(75));
6017 processSync(mapper);
6018
6019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6020 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6021 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6022
6023 processUp(mapper);
6024 processSync(mapper);
6025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6026
6027 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006028 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006029 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006030 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006031 processSync(mapper);
6032
6033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6034 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6035 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6036
6037 processUp(mapper);
6038 processSync(mapper);
6039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6040
6041 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006042 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006043 prepareDisplay(DISPLAY_ORIENTATION_180);
6044 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6045 processSync(mapper);
6046
6047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6048 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6049 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6050
6051 processUp(mapper);
6052 processSync(mapper);
6053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6054
6055 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006056 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006057 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006058 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006059 processSync(mapper);
6060
6061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6062 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6063 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6064
6065 processUp(mapper);
6066 processSync(mapper);
6067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6068}
6069
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006070TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6071 addConfigurationProperty("touch.deviceType", "touchScreen");
6072 prepareButtons();
6073 prepareAxes(POSITION);
6074 addConfigurationProperty("touch.orientationAware", "1");
6075 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6076 clearViewports();
6077 prepareDisplay(DISPLAY_ORIENTATION_0);
6078 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6079 NotifyMotionArgs args;
6080
6081 // Orientation 0.
6082 processDown(mapper, toRawX(50), toRawY(75));
6083 processSync(mapper);
6084
6085 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6086 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6087 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6088
6089 processUp(mapper);
6090 processSync(mapper);
6091 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6092}
6093
6094TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6095 addConfigurationProperty("touch.deviceType", "touchScreen");
6096 prepareButtons();
6097 prepareAxes(POSITION);
6098 addConfigurationProperty("touch.orientationAware", "1");
6099 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6100 clearViewports();
6101 prepareDisplay(DISPLAY_ORIENTATION_0);
6102 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6103 NotifyMotionArgs args;
6104
6105 // Orientation 90.
6106 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6107 processSync(mapper);
6108
6109 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6110 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6111 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6112
6113 processUp(mapper);
6114 processSync(mapper);
6115 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6116}
6117
6118TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6119 addConfigurationProperty("touch.deviceType", "touchScreen");
6120 prepareButtons();
6121 prepareAxes(POSITION);
6122 addConfigurationProperty("touch.orientationAware", "1");
6123 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6124 clearViewports();
6125 prepareDisplay(DISPLAY_ORIENTATION_0);
6126 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6127 NotifyMotionArgs args;
6128
6129 // Orientation 180.
6130 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6131 processSync(mapper);
6132
6133 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6134 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6135 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6136
6137 processUp(mapper);
6138 processSync(mapper);
6139 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6140}
6141
6142TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6143 addConfigurationProperty("touch.deviceType", "touchScreen");
6144 prepareButtons();
6145 prepareAxes(POSITION);
6146 addConfigurationProperty("touch.orientationAware", "1");
6147 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6148 clearViewports();
6149 prepareDisplay(DISPLAY_ORIENTATION_0);
6150 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6151 NotifyMotionArgs args;
6152
6153 // Orientation 270.
6154 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6155 processSync(mapper);
6156
6157 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6158 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6159 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6160
6161 processUp(mapper);
6162 processSync(mapper);
6163 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6164}
6165
6166TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6167 addConfigurationProperty("touch.deviceType", "touchScreen");
6168 prepareButtons();
6169 prepareAxes(POSITION);
6170 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6171 // orientation-aware are affected by display rotation.
6172 addConfigurationProperty("touch.orientationAware", "0");
6173 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6174 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6175
6176 NotifyMotionArgs args;
6177
6178 // Orientation 90, Rotation 0.
6179 clearViewports();
6180 prepareDisplay(DISPLAY_ORIENTATION_0);
6181 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6182 processSync(mapper);
6183
6184 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6185 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6186 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6187
6188 processUp(mapper);
6189 processSync(mapper);
6190 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6191
6192 // Orientation 90, Rotation 90.
6193 clearViewports();
6194 prepareDisplay(DISPLAY_ORIENTATION_90);
6195 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6196 processSync(mapper);
6197
6198 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6199 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6200 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6201
6202 processUp(mapper);
6203 processSync(mapper);
6204 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6205
6206 // Orientation 90, Rotation 180.
6207 clearViewports();
6208 prepareDisplay(DISPLAY_ORIENTATION_180);
6209 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6210 processSync(mapper);
6211
6212 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6213 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6214 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6215
6216 processUp(mapper);
6217 processSync(mapper);
6218 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6219
6220 // Orientation 90, Rotation 270.
6221 clearViewports();
6222 prepareDisplay(DISPLAY_ORIENTATION_270);
6223 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6224 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6225 processSync(mapper);
6226
6227 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6228 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6229 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6230
6231 processUp(mapper);
6232 processSync(mapper);
6233 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6234}
6235
Michael Wrightd02c5b62014-02-10 15:10:22 -08006236TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006237 addConfigurationProperty("touch.deviceType", "touchScreen");
6238 prepareDisplay(DISPLAY_ORIENTATION_0);
6239 prepareButtons();
6240 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006241 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006242
6243 // These calculations are based on the input device calibration documentation.
6244 int32_t rawX = 100;
6245 int32_t rawY = 200;
6246 int32_t rawPressure = 10;
6247 int32_t rawToolMajor = 12;
6248 int32_t rawDistance = 2;
6249 int32_t rawTiltX = 30;
6250 int32_t rawTiltY = 110;
6251
6252 float x = toDisplayX(rawX);
6253 float y = toDisplayY(rawY);
6254 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6255 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6256 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6257 float distance = float(rawDistance);
6258
6259 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6260 float tiltScale = M_PI / 180;
6261 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6262 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6263 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6264 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6265
6266 processDown(mapper, rawX, rawY);
6267 processPressure(mapper, rawPressure);
6268 processToolMajor(mapper, rawToolMajor);
6269 processDistance(mapper, rawDistance);
6270 processTilt(mapper, rawTiltX, rawTiltY);
6271 processSync(mapper);
6272
6273 NotifyMotionArgs args;
6274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6276 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6277 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6278}
6279
Jason Gerecke489fda82012-09-07 17:19:40 -07006280TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006281 addConfigurationProperty("touch.deviceType", "touchScreen");
6282 prepareDisplay(DISPLAY_ORIENTATION_0);
6283 prepareLocationCalibration();
6284 prepareButtons();
6285 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006286 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006287
6288 int32_t rawX = 100;
6289 int32_t rawY = 200;
6290
6291 float x = toDisplayX(toCookedX(rawX, rawY));
6292 float y = toDisplayY(toCookedY(rawX, rawY));
6293
6294 processDown(mapper, rawX, rawY);
6295 processSync(mapper);
6296
6297 NotifyMotionArgs args;
6298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6299 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6300 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6301}
6302
Michael Wrightd02c5b62014-02-10 15:10:22 -08006303TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006304 addConfigurationProperty("touch.deviceType", "touchScreen");
6305 prepareDisplay(DISPLAY_ORIENTATION_0);
6306 prepareButtons();
6307 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006308 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006309
6310 NotifyMotionArgs motionArgs;
6311 NotifyKeyArgs keyArgs;
6312
6313 processDown(mapper, 100, 200);
6314 processSync(mapper);
6315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6316 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6317 ASSERT_EQ(0, motionArgs.buttonState);
6318
6319 // press BTN_LEFT, release BTN_LEFT
6320 processKey(mapper, BTN_LEFT, 1);
6321 processSync(mapper);
6322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6323 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6324 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6325
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6327 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6328 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6329
Michael Wrightd02c5b62014-02-10 15:10:22 -08006330 processKey(mapper, BTN_LEFT, 0);
6331 processSync(mapper);
6332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006333 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006334 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006335
6336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006337 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006338 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006339
6340 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6341 processKey(mapper, BTN_RIGHT, 1);
6342 processKey(mapper, BTN_MIDDLE, 1);
6343 processSync(mapper);
6344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6345 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6346 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6347 motionArgs.buttonState);
6348
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6350 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6351 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6352
6353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6354 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6355 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6356 motionArgs.buttonState);
6357
Michael Wrightd02c5b62014-02-10 15:10:22 -08006358 processKey(mapper, BTN_RIGHT, 0);
6359 processSync(mapper);
6360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006361 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006362 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006363
6364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006365 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006366 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006367
6368 processKey(mapper, BTN_MIDDLE, 0);
6369 processSync(mapper);
6370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006371 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006372 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006373
6374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006376 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006377
6378 // press BTN_BACK, release BTN_BACK
6379 processKey(mapper, BTN_BACK, 1);
6380 processSync(mapper);
6381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6382 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6383 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006384
Michael Wrightd02c5b62014-02-10 15:10:22 -08006385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006386 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006387 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6388
6389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6390 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6391 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006392
6393 processKey(mapper, BTN_BACK, 0);
6394 processSync(mapper);
6395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006396 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006397 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006398
6399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006400 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006401 ASSERT_EQ(0, motionArgs.buttonState);
6402
Michael Wrightd02c5b62014-02-10 15:10:22 -08006403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6404 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6405 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6406
6407 // press BTN_SIDE, release BTN_SIDE
6408 processKey(mapper, BTN_SIDE, 1);
6409 processSync(mapper);
6410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6411 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6412 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006413
Michael Wrightd02c5b62014-02-10 15:10:22 -08006414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006415 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006416 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6417
6418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6419 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6420 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006421
6422 processKey(mapper, BTN_SIDE, 0);
6423 processSync(mapper);
6424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006425 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006426 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006427
6428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006429 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006430 ASSERT_EQ(0, motionArgs.buttonState);
6431
Michael Wrightd02c5b62014-02-10 15:10:22 -08006432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6433 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6434 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6435
6436 // press BTN_FORWARD, release BTN_FORWARD
6437 processKey(mapper, BTN_FORWARD, 1);
6438 processSync(mapper);
6439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6440 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6441 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006442
Michael Wrightd02c5b62014-02-10 15:10:22 -08006443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006444 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006445 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6446
6447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6448 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6449 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006450
6451 processKey(mapper, BTN_FORWARD, 0);
6452 processSync(mapper);
6453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006454 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006455 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006456
6457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006458 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006459 ASSERT_EQ(0, motionArgs.buttonState);
6460
Michael Wrightd02c5b62014-02-10 15:10:22 -08006461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6462 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6463 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6464
6465 // press BTN_EXTRA, release BTN_EXTRA
6466 processKey(mapper, BTN_EXTRA, 1);
6467 processSync(mapper);
6468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6469 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6470 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006471
Michael Wrightd02c5b62014-02-10 15:10:22 -08006472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006473 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006474 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6475
6476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6477 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6478 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006479
6480 processKey(mapper, BTN_EXTRA, 0);
6481 processSync(mapper);
6482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006483 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006484 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006485
6486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006487 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006488 ASSERT_EQ(0, motionArgs.buttonState);
6489
Michael Wrightd02c5b62014-02-10 15:10:22 -08006490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6491 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6492 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6493
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6495
Michael Wrightd02c5b62014-02-10 15:10:22 -08006496 // press BTN_STYLUS, release BTN_STYLUS
6497 processKey(mapper, BTN_STYLUS, 1);
6498 processSync(mapper);
6499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6500 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006501 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6502
6503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6504 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6505 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006506
6507 processKey(mapper, BTN_STYLUS, 0);
6508 processSync(mapper);
6509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006510 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006511 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006512
6513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006514 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006515 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006516
6517 // press BTN_STYLUS2, release BTN_STYLUS2
6518 processKey(mapper, BTN_STYLUS2, 1);
6519 processSync(mapper);
6520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6521 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006522 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6523
6524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6525 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6526 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006527
6528 processKey(mapper, BTN_STYLUS2, 0);
6529 processSync(mapper);
6530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006531 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006532 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006533
6534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006535 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006536 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006537
6538 // release touch
6539 processUp(mapper);
6540 processSync(mapper);
6541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6542 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6543 ASSERT_EQ(0, motionArgs.buttonState);
6544}
6545
6546TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006547 addConfigurationProperty("touch.deviceType", "touchScreen");
6548 prepareDisplay(DISPLAY_ORIENTATION_0);
6549 prepareButtons();
6550 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006551 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006552
6553 NotifyMotionArgs motionArgs;
6554
6555 // default tool type is finger
6556 processDown(mapper, 100, 200);
6557 processSync(mapper);
6558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6559 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6560 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6561
6562 // eraser
6563 processKey(mapper, BTN_TOOL_RUBBER, 1);
6564 processSync(mapper);
6565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6566 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6567 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6568
6569 // stylus
6570 processKey(mapper, BTN_TOOL_RUBBER, 0);
6571 processKey(mapper, BTN_TOOL_PEN, 1);
6572 processSync(mapper);
6573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6574 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6575 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6576
6577 // brush
6578 processKey(mapper, BTN_TOOL_PEN, 0);
6579 processKey(mapper, BTN_TOOL_BRUSH, 1);
6580 processSync(mapper);
6581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6582 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6583 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6584
6585 // pencil
6586 processKey(mapper, BTN_TOOL_BRUSH, 0);
6587 processKey(mapper, BTN_TOOL_PENCIL, 1);
6588 processSync(mapper);
6589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6590 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6591 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6592
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006593 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006594 processKey(mapper, BTN_TOOL_PENCIL, 0);
6595 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6596 processSync(mapper);
6597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6598 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6599 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6600
6601 // mouse
6602 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6603 processKey(mapper, BTN_TOOL_MOUSE, 1);
6604 processSync(mapper);
6605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6606 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6607 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6608
6609 // lens
6610 processKey(mapper, BTN_TOOL_MOUSE, 0);
6611 processKey(mapper, BTN_TOOL_LENS, 1);
6612 processSync(mapper);
6613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6614 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6615 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6616
6617 // double-tap
6618 processKey(mapper, BTN_TOOL_LENS, 0);
6619 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6620 processSync(mapper);
6621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6622 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6623 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6624
6625 // triple-tap
6626 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6627 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6628 processSync(mapper);
6629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6630 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6631 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6632
6633 // quad-tap
6634 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6635 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6636 processSync(mapper);
6637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6638 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6639 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6640
6641 // finger
6642 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6643 processKey(mapper, BTN_TOOL_FINGER, 1);
6644 processSync(mapper);
6645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6646 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6647 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6648
6649 // stylus trumps finger
6650 processKey(mapper, BTN_TOOL_PEN, 1);
6651 processSync(mapper);
6652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6653 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6654 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6655
6656 // eraser trumps stylus
6657 processKey(mapper, BTN_TOOL_RUBBER, 1);
6658 processSync(mapper);
6659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6660 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6661 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6662
6663 // mouse trumps eraser
6664 processKey(mapper, BTN_TOOL_MOUSE, 1);
6665 processSync(mapper);
6666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6667 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6668 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6669
6670 // back to default tool type
6671 processKey(mapper, BTN_TOOL_MOUSE, 0);
6672 processKey(mapper, BTN_TOOL_RUBBER, 0);
6673 processKey(mapper, BTN_TOOL_PEN, 0);
6674 processKey(mapper, BTN_TOOL_FINGER, 0);
6675 processSync(mapper);
6676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6677 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6678 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6679}
6680
6681TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006682 addConfigurationProperty("touch.deviceType", "touchScreen");
6683 prepareDisplay(DISPLAY_ORIENTATION_0);
6684 prepareButtons();
6685 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006686 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006687 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006688
6689 NotifyMotionArgs motionArgs;
6690
6691 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6692 processKey(mapper, BTN_TOOL_FINGER, 1);
6693 processMove(mapper, 100, 200);
6694 processSync(mapper);
6695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6696 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6697 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6698 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6699
6700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6701 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6703 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6704
6705 // move a little
6706 processMove(mapper, 150, 250);
6707 processSync(mapper);
6708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6709 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6710 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6711 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6712
6713 // down when BTN_TOUCH is pressed, pressure defaults to 1
6714 processKey(mapper, BTN_TOUCH, 1);
6715 processSync(mapper);
6716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6717 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6719 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6720
6721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6722 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6723 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6724 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6725
6726 // up when BTN_TOUCH is released, hover restored
6727 processKey(mapper, BTN_TOUCH, 0);
6728 processSync(mapper);
6729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6730 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6731 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6732 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6733
6734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6735 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6737 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6738
6739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6740 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6741 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6742 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6743
6744 // exit hover when pointer goes away
6745 processKey(mapper, BTN_TOOL_FINGER, 0);
6746 processSync(mapper);
6747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6748 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6749 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6750 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6751}
6752
6753TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006754 addConfigurationProperty("touch.deviceType", "touchScreen");
6755 prepareDisplay(DISPLAY_ORIENTATION_0);
6756 prepareButtons();
6757 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006758 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006759
6760 NotifyMotionArgs motionArgs;
6761
6762 // initially hovering because pressure is 0
6763 processDown(mapper, 100, 200);
6764 processPressure(mapper, 0);
6765 processSync(mapper);
6766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6767 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6769 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6770
6771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6772 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6774 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6775
6776 // move a little
6777 processMove(mapper, 150, 250);
6778 processSync(mapper);
6779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6780 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6781 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6782 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6783
6784 // down when pressure is non-zero
6785 processPressure(mapper, RAW_PRESSURE_MAX);
6786 processSync(mapper);
6787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6788 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6789 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6790 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6791
6792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6793 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6794 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6795 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6796
6797 // up when pressure becomes 0, hover restored
6798 processPressure(mapper, 0);
6799 processSync(mapper);
6800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6801 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6803 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6804
6805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6806 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6807 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6808 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6809
6810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6811 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6812 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6813 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6814
6815 // exit hover when pointer goes away
6816 processUp(mapper);
6817 processSync(mapper);
6818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6819 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6820 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6821 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6822}
6823
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006824TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6825 addConfigurationProperty("touch.deviceType", "touchScreen");
6826 prepareDisplay(DISPLAY_ORIENTATION_0);
6827 prepareButtons();
6828 prepareAxes(POSITION | PRESSURE);
6829 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6830
6831 // Set the initial state for the touch pointer.
6832 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6833 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6834 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6835 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6836
6837 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
6838 // state by reading the current axis values.
6839 mapper.reset(ARBITRARY_TIME);
6840
6841 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6842 // the recreated touch state to generate a down event.
6843 processSync(mapper);
6844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6845 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6846
6847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6848}
6849
lilinnan687e58f2022-07-19 16:00:50 +08006850TEST_F(SingleTouchInputMapperTest,
6851 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6852 addConfigurationProperty("touch.deviceType", "touchScreen");
6853 prepareDisplay(DISPLAY_ORIENTATION_0);
6854 prepareButtons();
6855 prepareAxes(POSITION);
6856 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6857 NotifyMotionArgs motionArgs;
6858
6859 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006860 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006861 processSync(mapper);
6862
6863 // We should receive a down event
6864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6865 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6866
6867 // Change display id
6868 clearViewports();
6869 prepareSecondaryDisplay(ViewportType::INTERNAL);
6870
6871 // We should receive a cancel event
6872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6873 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6874 // Then receive reset called
6875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6876}
6877
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006878TEST_F(SingleTouchInputMapperTest,
6879 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6880 addConfigurationProperty("touch.deviceType", "touchScreen");
6881 prepareDisplay(DISPLAY_ORIENTATION_0);
6882 prepareButtons();
6883 prepareAxes(POSITION);
6884 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6886 NotifyMotionArgs motionArgs;
6887
6888 // Start a new gesture.
6889 processDown(mapper, 100, 200);
6890 processSync(mapper);
6891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6892 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6893
6894 // Make the viewport inactive. This will put the device in disabled mode.
6895 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6896 viewport->isActive = false;
6897 mFakePolicy->updateViewport(*viewport);
6898 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6899
6900 // We should receive a cancel event for the ongoing gesture.
6901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6902 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6903 // Then we should be notified that the device was reset.
6904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6905
6906 // No events are generated while the viewport is inactive.
6907 processMove(mapper, 101, 201);
6908 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006909 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006910 processSync(mapper);
6911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6912
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006913 // Start a new gesture while the viewport is still inactive.
6914 processDown(mapper, 300, 400);
6915 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6916 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6917 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6918 processSync(mapper);
6919
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006920 // Make the viewport active again. The device should resume processing events.
6921 viewport->isActive = true;
6922 mFakePolicy->updateViewport(*viewport);
6923 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6924
6925 // The device is reset because it changes back to direct mode, without generating any events.
6926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6928
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006929 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006930 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6932 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006933
6934 // No more events.
6935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6937}
6938
Prabir Pradhan5632d622021-09-06 07:57:20 -07006939// --- TouchDisplayProjectionTest ---
6940
6941class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6942public:
6943 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6944 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6945 // rotated equivalent of the given un-rotated physical display bounds.
6946 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6947 uint32_t inverseRotationFlags;
6948 auto width = DISPLAY_WIDTH;
6949 auto height = DISPLAY_HEIGHT;
6950 switch (orientation) {
6951 case DISPLAY_ORIENTATION_90:
6952 inverseRotationFlags = ui::Transform::ROT_270;
6953 std::swap(width, height);
6954 break;
6955 case DISPLAY_ORIENTATION_180:
6956 inverseRotationFlags = ui::Transform::ROT_180;
6957 break;
6958 case DISPLAY_ORIENTATION_270:
6959 inverseRotationFlags = ui::Transform::ROT_90;
6960 std::swap(width, height);
6961 break;
6962 case DISPLAY_ORIENTATION_0:
6963 inverseRotationFlags = ui::Transform::ROT_0;
6964 break;
6965 default:
6966 FAIL() << "Invalid orientation: " << orientation;
6967 }
6968
6969 const ui::Transform rotation(inverseRotationFlags, width, height);
6970 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6971
6972 std::optional<DisplayViewport> internalViewport =
6973 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6974 DisplayViewport& v = *internalViewport;
6975 v.displayId = DISPLAY_ID;
6976 v.orientation = orientation;
6977
6978 v.logicalLeft = 0;
6979 v.logicalTop = 0;
6980 v.logicalRight = 100;
6981 v.logicalBottom = 100;
6982
6983 v.physicalLeft = rotatedPhysicalDisplay.left;
6984 v.physicalTop = rotatedPhysicalDisplay.top;
6985 v.physicalRight = rotatedPhysicalDisplay.right;
6986 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6987
6988 v.deviceWidth = width;
6989 v.deviceHeight = height;
6990
6991 v.isActive = true;
6992 v.uniqueId = UNIQUE_ID;
6993 v.type = ViewportType::INTERNAL;
6994 mFakePolicy->updateViewport(v);
6995 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6996 }
6997
6998 void assertReceivedMove(const Point& point) {
6999 NotifyMotionArgs motionArgs;
7000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7001 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7002 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7003 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7004 1, 0, 0, 0, 0, 0, 0, 0));
7005 }
7006};
7007
7008TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7009 addConfigurationProperty("touch.deviceType", "touchScreen");
7010 prepareDisplay(DISPLAY_ORIENTATION_0);
7011
7012 prepareButtons();
7013 prepareAxes(POSITION);
7014 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7015
7016 NotifyMotionArgs motionArgs;
7017
7018 // Configure the DisplayViewport such that the logical display maps to a subsection of
7019 // the display panel called the physical display. Here, the physical display is bounded by the
7020 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7021 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7022 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7023 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7024
7025 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7026 DISPLAY_ORIENTATION_270}) {
7027 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7028
7029 // Touches outside the physical display should be ignored, and should not generate any
7030 // events. Ensure touches at the following points that lie outside of the physical display
7031 // area do not generate any events.
7032 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7033 processDown(mapper, toRawX(point.x), toRawY(point.y));
7034 processSync(mapper);
7035 processUp(mapper);
7036 processSync(mapper);
7037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7038 << "Unexpected event generated for touch outside physical display at point: "
7039 << point.x << ", " << point.y;
7040 }
7041 }
7042}
7043
7044TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7045 addConfigurationProperty("touch.deviceType", "touchScreen");
7046 prepareDisplay(DISPLAY_ORIENTATION_0);
7047
7048 prepareButtons();
7049 prepareAxes(POSITION);
7050 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7051
7052 NotifyMotionArgs motionArgs;
7053
7054 // Configure the DisplayViewport such that the logical display maps to a subsection of
7055 // the display panel called the physical display. Here, the physical display is bounded by the
7056 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7057 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7058
7059 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7060 DISPLAY_ORIENTATION_270}) {
7061 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7062
7063 // Touches that start outside the physical display should be ignored until it enters the
7064 // physical display bounds, at which point it should generate a down event. Start a touch at
7065 // the point (5, 100), which is outside the physical display bounds.
7066 static const Point kOutsidePoint{5, 100};
7067 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7068 processSync(mapper);
7069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7070
7071 // Move the touch into the physical display area. This should generate a pointer down.
7072 processMove(mapper, toRawX(11), toRawY(21));
7073 processSync(mapper);
7074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7075 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7076 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7077 ASSERT_NO_FATAL_FAILURE(
7078 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7079
7080 // Move the touch inside the physical display area. This should generate a pointer move.
7081 processMove(mapper, toRawX(69), toRawY(159));
7082 processSync(mapper);
7083 assertReceivedMove({69, 159});
7084
7085 // Move outside the physical display area. Since the pointer is already down, this should
7086 // now continue generating events.
7087 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7088 processSync(mapper);
7089 assertReceivedMove(kOutsidePoint);
7090
7091 // Release. This should generate a pointer up.
7092 processUp(mapper);
7093 processSync(mapper);
7094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7095 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7096 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7097 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7098
7099 // Ensure no more events were generated.
7100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7102 }
7103}
7104
Michael Wrightd02c5b62014-02-10 15:10:22 -08007105// --- MultiTouchInputMapperTest ---
7106
7107class MultiTouchInputMapperTest : public TouchInputMapperTest {
7108protected:
7109 void prepareAxes(int axes);
7110
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007111 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7112 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7113 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7114 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7115 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7116 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7117 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7118 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7119 void processId(MultiTouchInputMapper& mapper, int32_t id);
7120 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7121 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7122 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
7123 void processMTSync(MultiTouchInputMapper& mapper);
7124 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007125};
7126
7127void MultiTouchInputMapperTest::prepareAxes(int axes) {
7128 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007129 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7130 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007131 }
7132 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007133 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7134 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007135 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007136 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7137 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007138 }
7139 }
7140 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007141 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7142 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007143 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007144 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007145 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007146 }
7147 }
7148 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007149 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7150 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007151 }
7152 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007153 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7154 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007155 }
7156 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007157 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7158 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007159 }
7160 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007161 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7162 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007163 }
7164 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007165 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7166 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007167 }
7168 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007169 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007170 }
7171}
7172
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007173void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7174 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007175 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7176 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007177}
7178
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007179void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7180 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007181 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007182}
7183
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007184void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7185 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007186 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007187}
7188
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007189void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007190 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007191}
7192
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007193void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007194 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007195}
7196
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007197void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7198 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007199 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007200}
7201
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007202void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007203 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007204}
7205
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007206void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007207 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007208}
7209
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007210void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007211 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007212}
7213
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007214void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007215 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007216}
7217
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007218void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007219 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007220}
7221
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007222void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7223 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007224 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007225}
7226
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007227void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007228 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007229}
7230
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007231void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007232 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007233}
7234
Michael Wrightd02c5b62014-02-10 15:10:22 -08007235TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007236 addConfigurationProperty("touch.deviceType", "touchScreen");
7237 prepareDisplay(DISPLAY_ORIENTATION_0);
7238 prepareAxes(POSITION);
7239 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007240 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007241
arthurhungdcef2dc2020-08-11 14:47:50 +08007242 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007243
7244 NotifyMotionArgs motionArgs;
7245
7246 // Two fingers down at once.
7247 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7248 processPosition(mapper, x1, y1);
7249 processMTSync(mapper);
7250 processPosition(mapper, x2, y2);
7251 processMTSync(mapper);
7252 processSync(mapper);
7253
7254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7255 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7256 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7257 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7258 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7259 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7260 ASSERT_EQ(0, motionArgs.flags);
7261 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7262 ASSERT_EQ(0, motionArgs.buttonState);
7263 ASSERT_EQ(0, motionArgs.edgeFlags);
7264 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7265 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7266 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7267 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7268 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7269 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7270 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7271 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7272
7273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7274 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7275 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7276 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7277 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007278 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007279 ASSERT_EQ(0, motionArgs.flags);
7280 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7281 ASSERT_EQ(0, motionArgs.buttonState);
7282 ASSERT_EQ(0, motionArgs.edgeFlags);
7283 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7284 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7285 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7286 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7287 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7288 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7289 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7290 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7291 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7292 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7293 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7294 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7295
7296 // Move.
7297 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7298 processPosition(mapper, x1, y1);
7299 processMTSync(mapper);
7300 processPosition(mapper, x2, y2);
7301 processMTSync(mapper);
7302 processSync(mapper);
7303
7304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7305 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7306 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7307 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7308 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7309 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7310 ASSERT_EQ(0, motionArgs.flags);
7311 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7312 ASSERT_EQ(0, motionArgs.buttonState);
7313 ASSERT_EQ(0, motionArgs.edgeFlags);
7314 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7315 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7316 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7317 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7318 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7319 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7320 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7321 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7322 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7323 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7324 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7325 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7326
7327 // First finger up.
7328 x2 += 15; y2 -= 20;
7329 processPosition(mapper, x2, y2);
7330 processMTSync(mapper);
7331 processSync(mapper);
7332
7333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7334 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7335 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7336 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7337 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007338 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007339 ASSERT_EQ(0, motionArgs.flags);
7340 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7341 ASSERT_EQ(0, motionArgs.buttonState);
7342 ASSERT_EQ(0, motionArgs.edgeFlags);
7343 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7344 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7345 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7346 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7347 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7348 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7349 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7350 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7351 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7352 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7353 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7354 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7355
7356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7357 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7358 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7359 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7360 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7361 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7362 ASSERT_EQ(0, motionArgs.flags);
7363 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7364 ASSERT_EQ(0, motionArgs.buttonState);
7365 ASSERT_EQ(0, motionArgs.edgeFlags);
7366 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7367 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7368 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7369 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7370 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7371 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7372 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7373 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7374
7375 // Move.
7376 x2 += 20; y2 -= 25;
7377 processPosition(mapper, x2, y2);
7378 processMTSync(mapper);
7379 processSync(mapper);
7380
7381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7382 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7383 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7384 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7385 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7386 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7387 ASSERT_EQ(0, motionArgs.flags);
7388 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7389 ASSERT_EQ(0, motionArgs.buttonState);
7390 ASSERT_EQ(0, motionArgs.edgeFlags);
7391 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7392 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7393 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7394 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7395 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7396 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7397 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7398 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7399
7400 // New finger down.
7401 int32_t x3 = 700, y3 = 300;
7402 processPosition(mapper, x2, y2);
7403 processMTSync(mapper);
7404 processPosition(mapper, x3, y3);
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_DOWN, 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(x3), toDisplayY(y3), 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 // Second finger up.
7432 x3 += 30; y3 -= 20;
7433 processPosition(mapper, x3, y3);
7434 processMTSync(mapper);
7435 processSync(mapper);
7436
7437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7438 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7439 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7440 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7441 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007442 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007443 ASSERT_EQ(0, motionArgs.flags);
7444 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7445 ASSERT_EQ(0, motionArgs.buttonState);
7446 ASSERT_EQ(0, motionArgs.edgeFlags);
7447 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7448 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7449 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7450 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7451 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7453 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7455 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7456 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7457 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7458 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7459
7460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7461 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7462 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7463 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7464 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7465 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7466 ASSERT_EQ(0, motionArgs.flags);
7467 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7468 ASSERT_EQ(0, motionArgs.buttonState);
7469 ASSERT_EQ(0, motionArgs.edgeFlags);
7470 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7471 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7472 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7473 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7474 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7475 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7476 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7477 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7478
7479 // Last finger up.
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);
7488 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7489 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(1), motionArgs.pointerCount);
7494 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7495 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7497 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7498 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7499 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7500 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7501
7502 // Should not have sent any more keys or motions.
7503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7505}
7506
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007507TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7508 addConfigurationProperty("touch.deviceType", "touchScreen");
7509 prepareDisplay(DISPLAY_ORIENTATION_0);
7510
7511 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7512 /*fuzz*/ 0, /*resolution*/ 10);
7513 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7514 /*fuzz*/ 0, /*resolution*/ 11);
7515 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7516 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7517 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7518 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7519 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7520 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7521 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7522 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7523
7524 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7525
7526 // X and Y axes
7527 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7528 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7529 // Touch major and minor
7530 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7531 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7532 // Tool major and minor
7533 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7534 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7535}
7536
7537TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7538 addConfigurationProperty("touch.deviceType", "touchScreen");
7539 prepareDisplay(DISPLAY_ORIENTATION_0);
7540
7541 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7542 /*fuzz*/ 0, /*resolution*/ 10);
7543 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7544 /*fuzz*/ 0, /*resolution*/ 11);
7545
7546 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7547
7548 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7549
7550 // Touch major and minor
7551 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7552 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7553 // Tool major and minor
7554 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7555 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7556}
7557
Michael Wrightd02c5b62014-02-10 15:10:22 -08007558TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007559 addConfigurationProperty("touch.deviceType", "touchScreen");
7560 prepareDisplay(DISPLAY_ORIENTATION_0);
7561 prepareAxes(POSITION | ID);
7562 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007563 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007564
arthurhungdcef2dc2020-08-11 14:47:50 +08007565 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007566
7567 NotifyMotionArgs motionArgs;
7568
7569 // Two fingers down at once.
7570 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7571 processPosition(mapper, x1, y1);
7572 processId(mapper, 1);
7573 processMTSync(mapper);
7574 processPosition(mapper, x2, y2);
7575 processId(mapper, 2);
7576 processMTSync(mapper);
7577 processSync(mapper);
7578
7579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7580 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7581 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7582 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7583 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7585 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7586
7587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007588 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007589 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7590 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7591 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7592 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7593 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7594 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7595 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7597 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7598
7599 // Move.
7600 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7601 processPosition(mapper, x1, y1);
7602 processId(mapper, 1);
7603 processMTSync(mapper);
7604 processPosition(mapper, x2, y2);
7605 processId(mapper, 2);
7606 processMTSync(mapper);
7607 processSync(mapper);
7608
7609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7610 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7611 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7612 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7613 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7614 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7615 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7616 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7617 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7619 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7620
7621 // First finger up.
7622 x2 += 15; y2 -= 20;
7623 processPosition(mapper, x2, y2);
7624 processId(mapper, 2);
7625 processMTSync(mapper);
7626 processSync(mapper);
7627
7628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007629 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007630 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7631 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7632 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7633 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7634 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7635 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7636 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7638 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7639
7640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7641 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7642 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7643 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7644 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7645 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7646 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7647
7648 // Move.
7649 x2 += 20; y2 -= 25;
7650 processPosition(mapper, x2, y2);
7651 processId(mapper, 2);
7652 processMTSync(mapper);
7653 processSync(mapper);
7654
7655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7656 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7657 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7658 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7659 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7661 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7662
7663 // New finger down.
7664 int32_t x3 = 700, y3 = 300;
7665 processPosition(mapper, x2, y2);
7666 processId(mapper, 2);
7667 processMTSync(mapper);
7668 processPosition(mapper, x3, y3);
7669 processId(mapper, 3);
7670 processMTSync(mapper);
7671 processSync(mapper);
7672
7673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007674 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007675 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7676 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7677 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7678 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7679 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7680 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7681 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7682 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7683 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7684
7685 // Second finger up.
7686 x3 += 30; y3 -= 20;
7687 processPosition(mapper, x3, y3);
7688 processId(mapper, 3);
7689 processMTSync(mapper);
7690 processSync(mapper);
7691
7692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007693 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007694 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7695 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7696 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7697 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7698 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7699 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7700 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7701 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7702 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7703
7704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7705 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7706 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7707 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7708 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7709 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7710 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7711
7712 // Last finger up.
7713 processMTSync(mapper);
7714 processSync(mapper);
7715
7716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7717 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7718 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7719 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7720 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7721 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7722 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7723
7724 // Should not have sent any more keys or motions.
7725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7727}
7728
7729TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007730 addConfigurationProperty("touch.deviceType", "touchScreen");
7731 prepareDisplay(DISPLAY_ORIENTATION_0);
7732 prepareAxes(POSITION | ID | SLOT);
7733 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007734 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007735
arthurhungdcef2dc2020-08-11 14:47:50 +08007736 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007737
7738 NotifyMotionArgs motionArgs;
7739
7740 // Two fingers down at once.
7741 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7742 processPosition(mapper, x1, y1);
7743 processId(mapper, 1);
7744 processSlot(mapper, 1);
7745 processPosition(mapper, x2, y2);
7746 processId(mapper, 2);
7747 processSync(mapper);
7748
7749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7750 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7751 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7752 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7753 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7754 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7755 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7756
7757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007758 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007759 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7760 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7761 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7762 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7763 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7764 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7765 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7767 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7768
7769 // Move.
7770 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7771 processSlot(mapper, 0);
7772 processPosition(mapper, x1, y1);
7773 processSlot(mapper, 1);
7774 processPosition(mapper, x2, y2);
7775 processSync(mapper);
7776
7777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7778 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7779 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7780 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7781 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7782 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7783 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7785 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7786 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7787 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7788
7789 // First finger up.
7790 x2 += 15; y2 -= 20;
7791 processSlot(mapper, 0);
7792 processId(mapper, -1);
7793 processSlot(mapper, 1);
7794 processPosition(mapper, x2, y2);
7795 processSync(mapper);
7796
7797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007798 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007799 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7800 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7801 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7802 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7803 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7804 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7805 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7806 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7807 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7808
7809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7810 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7811 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7812 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7813 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7814 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7815 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7816
7817 // Move.
7818 x2 += 20; y2 -= 25;
7819 processPosition(mapper, x2, y2);
7820 processSync(mapper);
7821
7822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7823 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7824 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7825 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7826 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7827 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7828 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7829
7830 // New finger down.
7831 int32_t x3 = 700, y3 = 300;
7832 processPosition(mapper, x2, y2);
7833 processSlot(mapper, 0);
7834 processId(mapper, 3);
7835 processPosition(mapper, x3, y3);
7836 processSync(mapper);
7837
7838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007839 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007840 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7841 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7842 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7843 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7844 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7845 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7846 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7847 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7848 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7849
7850 // Second finger up.
7851 x3 += 30; y3 -= 20;
7852 processSlot(mapper, 1);
7853 processId(mapper, -1);
7854 processSlot(mapper, 0);
7855 processPosition(mapper, x3, y3);
7856 processSync(mapper);
7857
7858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007859 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007860 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7861 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7862 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7863 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7864 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7865 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7866 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7867 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7868 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7869
7870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7871 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7872 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7873 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7874 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7875 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7876 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7877
7878 // Last finger up.
7879 processId(mapper, -1);
7880 processSync(mapper);
7881
7882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7883 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7884 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7885 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7886 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7887 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7888 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7889
7890 // Should not have sent any more keys or motions.
7891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7893}
7894
7895TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007896 addConfigurationProperty("touch.deviceType", "touchScreen");
7897 prepareDisplay(DISPLAY_ORIENTATION_0);
7898 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007899 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007900
7901 // These calculations are based on the input device calibration documentation.
7902 int32_t rawX = 100;
7903 int32_t rawY = 200;
7904 int32_t rawTouchMajor = 7;
7905 int32_t rawTouchMinor = 6;
7906 int32_t rawToolMajor = 9;
7907 int32_t rawToolMinor = 8;
7908 int32_t rawPressure = 11;
7909 int32_t rawDistance = 0;
7910 int32_t rawOrientation = 3;
7911 int32_t id = 5;
7912
7913 float x = toDisplayX(rawX);
7914 float y = toDisplayY(rawY);
7915 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7916 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7917 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7918 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7919 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7920 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7921 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7922 float distance = float(rawDistance);
7923
7924 processPosition(mapper, rawX, rawY);
7925 processTouchMajor(mapper, rawTouchMajor);
7926 processTouchMinor(mapper, rawTouchMinor);
7927 processToolMajor(mapper, rawToolMajor);
7928 processToolMinor(mapper, rawToolMinor);
7929 processPressure(mapper, rawPressure);
7930 processOrientation(mapper, rawOrientation);
7931 processDistance(mapper, rawDistance);
7932 processId(mapper, id);
7933 processMTSync(mapper);
7934 processSync(mapper);
7935
7936 NotifyMotionArgs args;
7937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7938 ASSERT_EQ(0, args.pointerProperties[0].id);
7939 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7940 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7941 orientation, distance));
7942}
7943
7944TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007945 addConfigurationProperty("touch.deviceType", "touchScreen");
7946 prepareDisplay(DISPLAY_ORIENTATION_0);
7947 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7948 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007949 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007950
7951 // These calculations are based on the input device calibration documentation.
7952 int32_t rawX = 100;
7953 int32_t rawY = 200;
7954 int32_t rawTouchMajor = 140;
7955 int32_t rawTouchMinor = 120;
7956 int32_t rawToolMajor = 180;
7957 int32_t rawToolMinor = 160;
7958
7959 float x = toDisplayX(rawX);
7960 float y = toDisplayY(rawY);
7961 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7962 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7963 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7964 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7965 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7966
7967 processPosition(mapper, rawX, rawY);
7968 processTouchMajor(mapper, rawTouchMajor);
7969 processTouchMinor(mapper, rawTouchMinor);
7970 processToolMajor(mapper, rawToolMajor);
7971 processToolMinor(mapper, rawToolMinor);
7972 processMTSync(mapper);
7973 processSync(mapper);
7974
7975 NotifyMotionArgs args;
7976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7977 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7978 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7979}
7980
7981TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007982 addConfigurationProperty("touch.deviceType", "touchScreen");
7983 prepareDisplay(DISPLAY_ORIENTATION_0);
7984 prepareAxes(POSITION | TOUCH | TOOL);
7985 addConfigurationProperty("touch.size.calibration", "diameter");
7986 addConfigurationProperty("touch.size.scale", "10");
7987 addConfigurationProperty("touch.size.bias", "160");
7988 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007989 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007990
7991 // These calculations are based on the input device calibration documentation.
7992 // Note: We only provide a single common touch/tool value because the device is assumed
7993 // not to emit separate values for each pointer (isSummed = 1).
7994 int32_t rawX = 100;
7995 int32_t rawY = 200;
7996 int32_t rawX2 = 150;
7997 int32_t rawY2 = 250;
7998 int32_t rawTouchMajor = 5;
7999 int32_t rawToolMajor = 8;
8000
8001 float x = toDisplayX(rawX);
8002 float y = toDisplayY(rawY);
8003 float x2 = toDisplayX(rawX2);
8004 float y2 = toDisplayY(rawY2);
8005 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8006 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8007 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8008
8009 processPosition(mapper, rawX, rawY);
8010 processTouchMajor(mapper, rawTouchMajor);
8011 processToolMajor(mapper, rawToolMajor);
8012 processMTSync(mapper);
8013 processPosition(mapper, rawX2, rawY2);
8014 processTouchMajor(mapper, rawTouchMajor);
8015 processToolMajor(mapper, rawToolMajor);
8016 processMTSync(mapper);
8017 processSync(mapper);
8018
8019 NotifyMotionArgs args;
8020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8021 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8022
8023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008024 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008025 ASSERT_EQ(size_t(2), args.pointerCount);
8026 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8027 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8028 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8029 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8030}
8031
8032TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008033 addConfigurationProperty("touch.deviceType", "touchScreen");
8034 prepareDisplay(DISPLAY_ORIENTATION_0);
8035 prepareAxes(POSITION | TOUCH | TOOL);
8036 addConfigurationProperty("touch.size.calibration", "area");
8037 addConfigurationProperty("touch.size.scale", "43");
8038 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008039 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008040
8041 // These calculations are based on the input device calibration documentation.
8042 int32_t rawX = 100;
8043 int32_t rawY = 200;
8044 int32_t rawTouchMajor = 5;
8045 int32_t rawToolMajor = 8;
8046
8047 float x = toDisplayX(rawX);
8048 float y = toDisplayY(rawY);
8049 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8050 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8051 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8052
8053 processPosition(mapper, rawX, rawY);
8054 processTouchMajor(mapper, rawTouchMajor);
8055 processToolMajor(mapper, rawToolMajor);
8056 processMTSync(mapper);
8057 processSync(mapper);
8058
8059 NotifyMotionArgs args;
8060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8061 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8062 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8063}
8064
8065TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008066 addConfigurationProperty("touch.deviceType", "touchScreen");
8067 prepareDisplay(DISPLAY_ORIENTATION_0);
8068 prepareAxes(POSITION | PRESSURE);
8069 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8070 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008071 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008072
Michael Wrightaa449c92017-12-13 21:21:43 +00008073 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008074 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008075 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8076 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8077 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8078
Michael Wrightd02c5b62014-02-10 15:10:22 -08008079 // These calculations are based on the input device calibration documentation.
8080 int32_t rawX = 100;
8081 int32_t rawY = 200;
8082 int32_t rawPressure = 60;
8083
8084 float x = toDisplayX(rawX);
8085 float y = toDisplayY(rawY);
8086 float pressure = float(rawPressure) * 0.01f;
8087
8088 processPosition(mapper, rawX, rawY);
8089 processPressure(mapper, rawPressure);
8090 processMTSync(mapper);
8091 processSync(mapper);
8092
8093 NotifyMotionArgs args;
8094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8095 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8096 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8097}
8098
8099TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008100 addConfigurationProperty("touch.deviceType", "touchScreen");
8101 prepareDisplay(DISPLAY_ORIENTATION_0);
8102 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008103 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008104
8105 NotifyMotionArgs motionArgs;
8106 NotifyKeyArgs keyArgs;
8107
8108 processId(mapper, 1);
8109 processPosition(mapper, 100, 200);
8110 processSync(mapper);
8111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8112 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8113 ASSERT_EQ(0, motionArgs.buttonState);
8114
8115 // press BTN_LEFT, release BTN_LEFT
8116 processKey(mapper, BTN_LEFT, 1);
8117 processSync(mapper);
8118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8119 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8120 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8121
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8123 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8124 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8125
Michael Wrightd02c5b62014-02-10 15:10:22 -08008126 processKey(mapper, BTN_LEFT, 0);
8127 processSync(mapper);
8128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008129 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008130 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008131
8132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008134 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008135
8136 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8137 processKey(mapper, BTN_RIGHT, 1);
8138 processKey(mapper, BTN_MIDDLE, 1);
8139 processSync(mapper);
8140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8141 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8142 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8143 motionArgs.buttonState);
8144
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8146 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8147 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8148
8149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8150 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8151 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8152 motionArgs.buttonState);
8153
Michael Wrightd02c5b62014-02-10 15:10:22 -08008154 processKey(mapper, BTN_RIGHT, 0);
8155 processSync(mapper);
8156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008157 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008158 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008159
8160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008161 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008162 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008163
8164 processKey(mapper, BTN_MIDDLE, 0);
8165 processSync(mapper);
8166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008167 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008168 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008169
8170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008171 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008172 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008173
8174 // press BTN_BACK, release BTN_BACK
8175 processKey(mapper, BTN_BACK, 1);
8176 processSync(mapper);
8177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8178 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8179 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008180
Michael Wrightd02c5b62014-02-10 15:10:22 -08008181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008182 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008183 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8184
8185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8186 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8187 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008188
8189 processKey(mapper, BTN_BACK, 0);
8190 processSync(mapper);
8191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008192 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008193 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008194
8195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008196 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008197 ASSERT_EQ(0, motionArgs.buttonState);
8198
Michael Wrightd02c5b62014-02-10 15:10:22 -08008199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8200 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8201 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8202
8203 // press BTN_SIDE, release BTN_SIDE
8204 processKey(mapper, BTN_SIDE, 1);
8205 processSync(mapper);
8206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8207 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8208 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008209
Michael Wrightd02c5b62014-02-10 15:10:22 -08008210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008211 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008212 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8213
8214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8215 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8216 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008217
8218 processKey(mapper, BTN_SIDE, 0);
8219 processSync(mapper);
8220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008221 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008222 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008223
8224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008225 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008226 ASSERT_EQ(0, motionArgs.buttonState);
8227
Michael Wrightd02c5b62014-02-10 15:10:22 -08008228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8229 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8230 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8231
8232 // press BTN_FORWARD, release BTN_FORWARD
8233 processKey(mapper, BTN_FORWARD, 1);
8234 processSync(mapper);
8235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8236 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8237 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008238
Michael Wrightd02c5b62014-02-10 15:10:22 -08008239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008240 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008241 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8242
8243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8244 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8245 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008246
8247 processKey(mapper, BTN_FORWARD, 0);
8248 processSync(mapper);
8249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008250 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008251 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008252
8253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008254 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008255 ASSERT_EQ(0, motionArgs.buttonState);
8256
Michael Wrightd02c5b62014-02-10 15:10:22 -08008257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8258 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8259 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8260
8261 // press BTN_EXTRA, release BTN_EXTRA
8262 processKey(mapper, BTN_EXTRA, 1);
8263 processSync(mapper);
8264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8265 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8266 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008267
Michael Wrightd02c5b62014-02-10 15:10:22 -08008268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008269 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008270 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8271
8272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8273 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8274 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008275
8276 processKey(mapper, BTN_EXTRA, 0);
8277 processSync(mapper);
8278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008279 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008280 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008281
8282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008283 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008284 ASSERT_EQ(0, motionArgs.buttonState);
8285
Michael Wrightd02c5b62014-02-10 15:10:22 -08008286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8287 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8288 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8289
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8291
Michael Wrightd02c5b62014-02-10 15:10:22 -08008292 // press BTN_STYLUS, release BTN_STYLUS
8293 processKey(mapper, BTN_STYLUS, 1);
8294 processSync(mapper);
8295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8296 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008297 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8298
8299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8300 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8301 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008302
8303 processKey(mapper, BTN_STYLUS, 0);
8304 processSync(mapper);
8305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008306 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008307 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008308
8309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008310 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008311 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008312
8313 // press BTN_STYLUS2, release BTN_STYLUS2
8314 processKey(mapper, BTN_STYLUS2, 1);
8315 processSync(mapper);
8316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8317 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008318 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8319
8320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8321 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8322 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008323
8324 processKey(mapper, BTN_STYLUS2, 0);
8325 processSync(mapper);
8326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008327 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008328 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008329
8330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008331 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008332 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008333
8334 // release touch
8335 processId(mapper, -1);
8336 processSync(mapper);
8337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8338 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8339 ASSERT_EQ(0, motionArgs.buttonState);
8340}
8341
8342TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008343 addConfigurationProperty("touch.deviceType", "touchScreen");
8344 prepareDisplay(DISPLAY_ORIENTATION_0);
8345 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008346 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008347
8348 NotifyMotionArgs motionArgs;
8349
8350 // default tool type is finger
8351 processId(mapper, 1);
8352 processPosition(mapper, 100, 200);
8353 processSync(mapper);
8354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8355 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8356 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8357
8358 // eraser
8359 processKey(mapper, BTN_TOOL_RUBBER, 1);
8360 processSync(mapper);
8361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8362 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8363 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8364
8365 // stylus
8366 processKey(mapper, BTN_TOOL_RUBBER, 0);
8367 processKey(mapper, BTN_TOOL_PEN, 1);
8368 processSync(mapper);
8369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8370 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8371 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8372
8373 // brush
8374 processKey(mapper, BTN_TOOL_PEN, 0);
8375 processKey(mapper, BTN_TOOL_BRUSH, 1);
8376 processSync(mapper);
8377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8378 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8379 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8380
8381 // pencil
8382 processKey(mapper, BTN_TOOL_BRUSH, 0);
8383 processKey(mapper, BTN_TOOL_PENCIL, 1);
8384 processSync(mapper);
8385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8386 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8387 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8388
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008389 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008390 processKey(mapper, BTN_TOOL_PENCIL, 0);
8391 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8392 processSync(mapper);
8393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8394 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8395 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8396
8397 // mouse
8398 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8399 processKey(mapper, BTN_TOOL_MOUSE, 1);
8400 processSync(mapper);
8401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8402 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8403 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8404
8405 // lens
8406 processKey(mapper, BTN_TOOL_MOUSE, 0);
8407 processKey(mapper, BTN_TOOL_LENS, 1);
8408 processSync(mapper);
8409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8411 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8412
8413 // double-tap
8414 processKey(mapper, BTN_TOOL_LENS, 0);
8415 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8416 processSync(mapper);
8417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8418 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8419 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8420
8421 // triple-tap
8422 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8423 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8424 processSync(mapper);
8425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8426 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8427 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8428
8429 // quad-tap
8430 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8431 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8432 processSync(mapper);
8433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8434 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8435 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8436
8437 // finger
8438 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8439 processKey(mapper, BTN_TOOL_FINGER, 1);
8440 processSync(mapper);
8441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8442 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8443 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8444
8445 // stylus trumps finger
8446 processKey(mapper, BTN_TOOL_PEN, 1);
8447 processSync(mapper);
8448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8449 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8450 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8451
8452 // eraser trumps stylus
8453 processKey(mapper, BTN_TOOL_RUBBER, 1);
8454 processSync(mapper);
8455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8456 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8457 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8458
8459 // mouse trumps eraser
8460 processKey(mapper, BTN_TOOL_MOUSE, 1);
8461 processSync(mapper);
8462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8463 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8464 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8465
8466 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8467 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8468 processSync(mapper);
8469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8470 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8471 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8472
8473 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8474 processToolType(mapper, MT_TOOL_PEN);
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_STYLUS, motionArgs.pointerProperties[0].toolType);
8479
8480 // back to default tool type
8481 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8482 processKey(mapper, BTN_TOOL_MOUSE, 0);
8483 processKey(mapper, BTN_TOOL_RUBBER, 0);
8484 processKey(mapper, BTN_TOOL_PEN, 0);
8485 processKey(mapper, BTN_TOOL_FINGER, 0);
8486 processSync(mapper);
8487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8488 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8489 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8490}
8491
8492TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008493 addConfigurationProperty("touch.deviceType", "touchScreen");
8494 prepareDisplay(DISPLAY_ORIENTATION_0);
8495 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008496 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008497 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008498
8499 NotifyMotionArgs motionArgs;
8500
8501 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8502 processId(mapper, 1);
8503 processPosition(mapper, 100, 200);
8504 processSync(mapper);
8505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8506 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8508 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8509
8510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8511 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8512 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8513 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8514
8515 // move a little
8516 processPosition(mapper, 150, 250);
8517 processSync(mapper);
8518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8519 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8521 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8522
8523 // down when BTN_TOUCH is pressed, pressure defaults to 1
8524 processKey(mapper, BTN_TOUCH, 1);
8525 processSync(mapper);
8526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8527 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8529 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8530
8531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8532 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8534 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8535
8536 // up when BTN_TOUCH is released, hover restored
8537 processKey(mapper, BTN_TOUCH, 0);
8538 processSync(mapper);
8539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8540 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8541 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8542 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8543
8544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8545 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8546 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8547 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8548
8549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8550 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8551 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8552 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8553
8554 // exit hover when pointer goes away
8555 processId(mapper, -1);
8556 processSync(mapper);
8557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8558 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8560 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8561}
8562
8563TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008564 addConfigurationProperty("touch.deviceType", "touchScreen");
8565 prepareDisplay(DISPLAY_ORIENTATION_0);
8566 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008567 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008568
8569 NotifyMotionArgs motionArgs;
8570
8571 // initially hovering because pressure is 0
8572 processId(mapper, 1);
8573 processPosition(mapper, 100, 200);
8574 processPressure(mapper, 0);
8575 processSync(mapper);
8576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8577 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8579 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8580
8581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8582 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8584 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8585
8586 // move a little
8587 processPosition(mapper, 150, 250);
8588 processSync(mapper);
8589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8590 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8592 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8593
8594 // down when pressure becomes non-zero
8595 processPressure(mapper, RAW_PRESSURE_MAX);
8596 processSync(mapper);
8597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8598 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8600 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8601
8602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8603 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8604 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8605 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8606
8607 // up when pressure becomes 0, hover restored
8608 processPressure(mapper, 0);
8609 processSync(mapper);
8610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8611 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8613 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8614
8615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8616 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8617 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8618 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8619
8620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8621 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8623 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8624
8625 // exit hover when pointer goes away
8626 processId(mapper, -1);
8627 processSync(mapper);
8628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8629 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8631 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8632}
8633
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008634/**
8635 * Set the input device port <--> display port associations, and check that the
8636 * events are routed to the display that matches the display port.
8637 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8638 */
8639TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008640 const std::string usb2 = "USB2";
8641 const uint8_t hdmi1 = 0;
8642 const uint8_t hdmi2 = 1;
8643 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008644 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008645
8646 addConfigurationProperty("touch.deviceType", "touchScreen");
8647 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008648 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008649
8650 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8651 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8652
8653 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8654 // for this input device is specified, and the matching viewport is not present,
8655 // the input device should be disabled (at the mapper level).
8656
8657 // Add viewport for display 2 on hdmi2
8658 prepareSecondaryDisplay(type, hdmi2);
8659 // Send a touch event
8660 processPosition(mapper, 100, 100);
8661 processSync(mapper);
8662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8663
8664 // Add viewport for display 1 on hdmi1
8665 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8666 // Send a touch event again
8667 processPosition(mapper, 100, 100);
8668 processSync(mapper);
8669
8670 NotifyMotionArgs args;
8671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8672 ASSERT_EQ(DISPLAY_ID, args.displayId);
8673}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008674
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008675TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8676 addConfigurationProperty("touch.deviceType", "touchScreen");
8677 prepareAxes(POSITION);
8678 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8679
8680 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8681
8682 prepareDisplay(DISPLAY_ORIENTATION_0);
8683 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8684
8685 // Send a touch event
8686 processPosition(mapper, 100, 100);
8687 processSync(mapper);
8688
8689 NotifyMotionArgs args;
8690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8691 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8692}
8693
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008694TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008695 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008696 std::shared_ptr<FakePointerController> fakePointerController =
8697 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008698 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008699 fakePointerController->setPosition(100, 200);
8700 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008701 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008702
Garfield Tan888a6a42020-01-09 11:39:16 -08008703 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008704 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008705
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008706 prepareDisplay(DISPLAY_ORIENTATION_0);
8707 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008708 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008709
8710 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008711 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008712
8713 NotifyMotionArgs motionArgs;
8714 processPosition(mapper, 100, 100);
8715 processSync(mapper);
8716
8717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8718 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8719 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8720}
8721
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008722/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008723 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8724 */
8725TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8726 addConfigurationProperty("touch.deviceType", "touchScreen");
8727 prepareAxes(POSITION);
8728 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8729
8730 prepareDisplay(DISPLAY_ORIENTATION_0);
8731 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8732 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8733 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8734 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8735
8736 NotifyMotionArgs args;
8737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8738 ASSERT_EQ(26, args.readTime);
8739
8740 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8741 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8742 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8743
8744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8745 ASSERT_EQ(33, args.readTime);
8746}
8747
8748/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008749 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8750 * events should not be delivered to the listener.
8751 */
8752TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8753 addConfigurationProperty("touch.deviceType", "touchScreen");
8754 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8755 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8756 ViewportType::INTERNAL);
8757 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8758 prepareAxes(POSITION);
8759 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8760
8761 NotifyMotionArgs motionArgs;
8762 processPosition(mapper, 100, 100);
8763 processSync(mapper);
8764
8765 mFakeListener->assertNotifyMotionWasNotCalled();
8766}
8767
Garfield Tanc734e4f2021-01-15 20:01:39 -08008768TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8769 addConfigurationProperty("touch.deviceType", "touchScreen");
8770 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8771 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8772 ViewportType::INTERNAL);
8773 std::optional<DisplayViewport> optionalDisplayViewport =
8774 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8775 ASSERT_TRUE(optionalDisplayViewport.has_value());
8776 DisplayViewport displayViewport = *optionalDisplayViewport;
8777
8778 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8779 prepareAxes(POSITION);
8780 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8781
8782 // Finger down
8783 int32_t x = 100, y = 100;
8784 processPosition(mapper, x, y);
8785 processSync(mapper);
8786
8787 NotifyMotionArgs motionArgs;
8788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8789 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8790
8791 // Deactivate display viewport
8792 displayViewport.isActive = false;
8793 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8794 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8795
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008796 // The ongoing touch should be canceled immediately
8797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8798 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8799
8800 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008801 x += 10, y += 10;
8802 processPosition(mapper, x, y);
8803 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008805
8806 // Reactivate display viewport
8807 displayViewport.isActive = true;
8808 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8809 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8810
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008811 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008812 x += 10, y += 10;
8813 processPosition(mapper, x, y);
8814 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8816 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008817}
8818
Arthur Hung7c645402019-01-25 17:45:42 +08008819TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8820 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008821 prepareAxes(POSITION | ID | SLOT);
8822 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008823 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008824
8825 // Create the second touch screen device, and enable multi fingers.
8826 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008827 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008828 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008829 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008830 std::shared_ptr<InputDevice> device2 =
8831 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008832 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008833
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008834 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8835 0 /*flat*/, 0 /*fuzz*/);
8836 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8837 0 /*flat*/, 0 /*fuzz*/);
8838 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8839 0 /*flat*/, 0 /*fuzz*/);
8840 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8841 0 /*flat*/, 0 /*fuzz*/);
8842 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8843 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8844 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008845
8846 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008847 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008848 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8849 device2->reset(ARBITRARY_TIME);
8850
8851 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008852 std::shared_ptr<FakePointerController> fakePointerController =
8853 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008854 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008855
8856 // Setup policy for associated displays and show touches.
8857 const uint8_t hdmi1 = 0;
8858 const uint8_t hdmi2 = 1;
8859 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8860 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8861 mFakePolicy->setShowTouches(true);
8862
8863 // Create displays.
8864 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008865 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008866
8867 // Default device will reconfigure above, need additional reconfiguration for another device.
8868 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan197e0862022-07-01 14:28:00 +00008869 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8870 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008871
8872 // Two fingers down at default display.
8873 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8874 processPosition(mapper, x1, y1);
8875 processId(mapper, 1);
8876 processSlot(mapper, 1);
8877 processPosition(mapper, x2, y2);
8878 processId(mapper, 2);
8879 processSync(mapper);
8880
8881 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8882 fakePointerController->getSpots().find(DISPLAY_ID);
8883 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8884 ASSERT_EQ(size_t(2), iter->second.size());
8885
8886 // Two fingers down at second display.
8887 processPosition(mapper2, x1, y1);
8888 processId(mapper2, 1);
8889 processSlot(mapper2, 1);
8890 processPosition(mapper2, x2, y2);
8891 processId(mapper2, 2);
8892 processSync(mapper2);
8893
8894 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8895 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8896 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008897
8898 // Disable the show touches configuration and ensure the spots are cleared.
8899 mFakePolicy->setShowTouches(false);
8900 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8901 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
8902
8903 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008904}
8905
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008906TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008907 prepareAxes(POSITION);
8908 addConfigurationProperty("touch.deviceType", "touchScreen");
8909 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008910 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008911
8912 NotifyMotionArgs motionArgs;
8913 // Unrotated video frame
8914 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8915 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008916 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008917 processPosition(mapper, 100, 200);
8918 processSync(mapper);
8919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8920 ASSERT_EQ(frames, motionArgs.videoFrames);
8921
8922 // Subsequent touch events should not have any videoframes
8923 // This is implemented separately in FakeEventHub,
8924 // but that should match the behaviour of TouchVideoDevice.
8925 processPosition(mapper, 200, 200);
8926 processSync(mapper);
8927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8928 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8929}
8930
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008931TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008932 prepareAxes(POSITION);
8933 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008934 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008935 // Unrotated video frame
8936 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8937 NotifyMotionArgs motionArgs;
8938
8939 // Test all 4 orientations
8940 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008941 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8942 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8943 clearViewports();
8944 prepareDisplay(orientation);
8945 std::vector<TouchVideoFrame> frames{frame};
8946 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8947 processPosition(mapper, 100, 200);
8948 processSync(mapper);
8949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8950 ASSERT_EQ(frames, motionArgs.videoFrames);
8951 }
8952}
8953
8954TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8955 prepareAxes(POSITION);
8956 addConfigurationProperty("touch.deviceType", "touchScreen");
8957 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8958 // orientation-aware are affected by display rotation.
8959 addConfigurationProperty("touch.orientationAware", "0");
8960 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8961 // Unrotated video frame
8962 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8963 NotifyMotionArgs motionArgs;
8964
8965 // Test all 4 orientations
8966 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008967 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8968 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8969 clearViewports();
8970 prepareDisplay(orientation);
8971 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008972 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008973 processPosition(mapper, 100, 200);
8974 processSync(mapper);
8975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008976 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8977 // compared to the display. This is so that when the window transform (which contains the
8978 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8979 // window's coordinate space.
8980 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008981 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08008982
8983 // Release finger.
8984 processSync(mapper);
8985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008986 }
8987}
8988
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008989TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008990 prepareAxes(POSITION);
8991 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008992 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008993 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8994 // so mix these.
8995 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8996 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8997 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8998 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8999 NotifyMotionArgs motionArgs;
9000
9001 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009002 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009003 processPosition(mapper, 100, 200);
9004 processSync(mapper);
9005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009006 ASSERT_EQ(frames, motionArgs.videoFrames);
9007}
9008
9009TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9010 prepareAxes(POSITION);
9011 addConfigurationProperty("touch.deviceType", "touchScreen");
9012 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9013 // orientation-aware are affected by display rotation.
9014 addConfigurationProperty("touch.orientationAware", "0");
9015 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9016 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9017 // so mix these.
9018 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9019 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9020 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9021 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9022 NotifyMotionArgs motionArgs;
9023
9024 prepareDisplay(DISPLAY_ORIENTATION_90);
9025 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9026 processPosition(mapper, 100, 200);
9027 processSync(mapper);
9028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9029 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9030 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9031 // compared to the display. This is so that when the window transform (which contains the
9032 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9033 // window's coordinate space.
9034 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
9035 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009036 ASSERT_EQ(frames, motionArgs.videoFrames);
9037}
9038
Arthur Hung9da14732019-09-02 16:16:58 +08009039/**
9040 * If we had defined port associations, but the viewport is not ready, the touch device would be
9041 * expected to be disabled, and it should be enabled after the viewport has found.
9042 */
9043TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009044 constexpr uint8_t hdmi2 = 1;
9045 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009046 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009047
9048 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9049
9050 addConfigurationProperty("touch.deviceType", "touchScreen");
9051 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009052 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009053
9054 ASSERT_EQ(mDevice->isEnabled(), false);
9055
9056 // Add display on hdmi2, the device should be enabled and can receive touch event.
9057 prepareSecondaryDisplay(type, hdmi2);
9058 ASSERT_EQ(mDevice->isEnabled(), true);
9059
9060 // Send a touch event.
9061 processPosition(mapper, 100, 100);
9062 processSync(mapper);
9063
9064 NotifyMotionArgs args;
9065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9066 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9067}
9068
Arthur Hung421eb1c2020-01-16 00:09:42 +08009069TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009070 addConfigurationProperty("touch.deviceType", "touchScreen");
9071 prepareDisplay(DISPLAY_ORIENTATION_0);
9072 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009073 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009074
9075 NotifyMotionArgs motionArgs;
9076
9077 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9078 // finger down
9079 processId(mapper, 1);
9080 processPosition(mapper, x1, y1);
9081 processSync(mapper);
9082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9083 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9084 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9085
9086 // finger move
9087 processId(mapper, 1);
9088 processPosition(mapper, x2, y2);
9089 processSync(mapper);
9090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9091 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9092 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9093
9094 // finger up.
9095 processId(mapper, -1);
9096 processSync(mapper);
9097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9098 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9099 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9100
9101 // new finger down
9102 processId(mapper, 1);
9103 processPosition(mapper, x3, y3);
9104 processSync(mapper);
9105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9106 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9107 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9108}
9109
9110/**
arthurhungcc7f9802020-04-30 17:55:40 +08009111 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9112 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009113 */
arthurhungcc7f9802020-04-30 17:55:40 +08009114TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009115 addConfigurationProperty("touch.deviceType", "touchScreen");
9116 prepareDisplay(DISPLAY_ORIENTATION_0);
9117 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009118 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009119
9120 NotifyMotionArgs motionArgs;
9121
9122 // default tool type is finger
9123 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009124 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009125 processPosition(mapper, x1, y1);
9126 processSync(mapper);
9127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9128 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9129 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9130
9131 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9132 processToolType(mapper, MT_TOOL_PALM);
9133 processSync(mapper);
9134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9135 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9136
9137 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009138 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009139 processPosition(mapper, x2, y2);
9140 processSync(mapper);
9141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9142
9143 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009144 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009145 processSync(mapper);
9146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9147
9148 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009149 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009150 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009151 processPosition(mapper, x3, y3);
9152 processSync(mapper);
9153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9154 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9155 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9156}
9157
arthurhungbf89a482020-04-17 17:37:55 +08009158/**
arthurhungcc7f9802020-04-30 17:55:40 +08009159 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9160 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009161 */
arthurhungcc7f9802020-04-30 17:55:40 +08009162TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009163 addConfigurationProperty("touch.deviceType", "touchScreen");
9164 prepareDisplay(DISPLAY_ORIENTATION_0);
9165 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9166 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9167
9168 NotifyMotionArgs motionArgs;
9169
9170 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009171 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9172 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009173 processPosition(mapper, x1, y1);
9174 processSync(mapper);
9175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9176 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9177 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9178
9179 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009180 processSlot(mapper, SECOND_SLOT);
9181 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009182 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009183 processSync(mapper);
9184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009185 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009186 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9187
9188 // If the tool type of the first finger changes to MT_TOOL_PALM,
9189 // we expect to receive ACTION_POINTER_UP with cancel flag.
9190 processSlot(mapper, FIRST_SLOT);
9191 processId(mapper, FIRST_TRACKING_ID);
9192 processToolType(mapper, MT_TOOL_PALM);
9193 processSync(mapper);
9194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009195 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009196 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9197
9198 // The following MOVE events of second finger should be processed.
9199 processSlot(mapper, SECOND_SLOT);
9200 processId(mapper, SECOND_TRACKING_ID);
9201 processPosition(mapper, x2 + 1, y2 + 1);
9202 processSync(mapper);
9203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9204 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9205 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9206
9207 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9208 // it. Second finger receive move.
9209 processSlot(mapper, FIRST_SLOT);
9210 processId(mapper, INVALID_TRACKING_ID);
9211 processSync(mapper);
9212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9213 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9214 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9215
9216 // Second finger keeps moving.
9217 processSlot(mapper, SECOND_SLOT);
9218 processId(mapper, SECOND_TRACKING_ID);
9219 processPosition(mapper, x2 + 2, y2 + 2);
9220 processSync(mapper);
9221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9222 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9223 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9224
9225 // Second finger up.
9226 processId(mapper, INVALID_TRACKING_ID);
9227 processSync(mapper);
9228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9229 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9230 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9231}
9232
9233/**
9234 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9235 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9236 */
9237TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9238 addConfigurationProperty("touch.deviceType", "touchScreen");
9239 prepareDisplay(DISPLAY_ORIENTATION_0);
9240 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9241 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9242
9243 NotifyMotionArgs motionArgs;
9244
9245 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9246 // First finger down.
9247 processId(mapper, FIRST_TRACKING_ID);
9248 processPosition(mapper, x1, y1);
9249 processSync(mapper);
9250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9251 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9252 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9253
9254 // Second finger down.
9255 processSlot(mapper, SECOND_SLOT);
9256 processId(mapper, SECOND_TRACKING_ID);
9257 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009258 processSync(mapper);
9259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009260 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009261 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9262
arthurhungcc7f9802020-04-30 17:55:40 +08009263 // If the tool type of the first finger changes to MT_TOOL_PALM,
9264 // we expect to receive ACTION_POINTER_UP with cancel flag.
9265 processSlot(mapper, FIRST_SLOT);
9266 processId(mapper, FIRST_TRACKING_ID);
9267 processToolType(mapper, MT_TOOL_PALM);
9268 processSync(mapper);
9269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009270 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009271 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9272
9273 // Second finger keeps moving.
9274 processSlot(mapper, SECOND_SLOT);
9275 processId(mapper, SECOND_TRACKING_ID);
9276 processPosition(mapper, x2 + 1, y2 + 1);
9277 processSync(mapper);
9278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9279 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9280
9281 // second finger becomes palm, receive cancel due to only 1 finger is active.
9282 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009283 processToolType(mapper, MT_TOOL_PALM);
9284 processSync(mapper);
9285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9286 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9287
arthurhungcc7f9802020-04-30 17:55:40 +08009288 // third finger down.
9289 processSlot(mapper, THIRD_SLOT);
9290 processId(mapper, THIRD_TRACKING_ID);
9291 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009292 processPosition(mapper, x3, y3);
9293 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9295 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9296 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009297 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9298
9299 // third finger move
9300 processId(mapper, THIRD_TRACKING_ID);
9301 processPosition(mapper, x3 + 1, y3 + 1);
9302 processSync(mapper);
9303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9304 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9305
9306 // first finger up, third finger receive move.
9307 processSlot(mapper, FIRST_SLOT);
9308 processId(mapper, INVALID_TRACKING_ID);
9309 processSync(mapper);
9310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9311 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9312 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9313
9314 // second finger up, third finger receive move.
9315 processSlot(mapper, SECOND_SLOT);
9316 processId(mapper, INVALID_TRACKING_ID);
9317 processSync(mapper);
9318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9320 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9321
9322 // third finger up.
9323 processSlot(mapper, THIRD_SLOT);
9324 processId(mapper, INVALID_TRACKING_ID);
9325 processSync(mapper);
9326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9327 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9328 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9329}
9330
9331/**
9332 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9333 * and the active finger could still be allowed to receive the events
9334 */
9335TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9336 addConfigurationProperty("touch.deviceType", "touchScreen");
9337 prepareDisplay(DISPLAY_ORIENTATION_0);
9338 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9339 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9340
9341 NotifyMotionArgs motionArgs;
9342
9343 // default tool type is finger
9344 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9345 processId(mapper, FIRST_TRACKING_ID);
9346 processPosition(mapper, x1, y1);
9347 processSync(mapper);
9348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9349 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9350 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9351
9352 // Second finger down.
9353 processSlot(mapper, SECOND_SLOT);
9354 processId(mapper, SECOND_TRACKING_ID);
9355 processPosition(mapper, x2, y2);
9356 processSync(mapper);
9357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009358 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009359 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9360
9361 // If the tool type of the second finger changes to MT_TOOL_PALM,
9362 // we expect to receive ACTION_POINTER_UP with cancel flag.
9363 processId(mapper, SECOND_TRACKING_ID);
9364 processToolType(mapper, MT_TOOL_PALM);
9365 processSync(mapper);
9366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009367 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009368 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9369
9370 // The following MOVE event should be processed.
9371 processSlot(mapper, FIRST_SLOT);
9372 processId(mapper, FIRST_TRACKING_ID);
9373 processPosition(mapper, x1 + 1, y1 + 1);
9374 processSync(mapper);
9375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9376 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9377 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9378
9379 // second finger up.
9380 processSlot(mapper, SECOND_SLOT);
9381 processId(mapper, INVALID_TRACKING_ID);
9382 processSync(mapper);
9383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9384 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9385
9386 // first finger keep moving
9387 processSlot(mapper, FIRST_SLOT);
9388 processId(mapper, FIRST_TRACKING_ID);
9389 processPosition(mapper, x1 + 2, y1 + 2);
9390 processSync(mapper);
9391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9392 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9393
9394 // first finger up.
9395 processId(mapper, INVALID_TRACKING_ID);
9396 processSync(mapper);
9397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9398 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9399 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009400}
9401
Arthur Hung9ad18942021-06-19 02:04:46 +00009402/**
9403 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9404 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9405 * cause slot be valid again.
9406 */
9407TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9408 addConfigurationProperty("touch.deviceType", "touchScreen");
9409 prepareDisplay(DISPLAY_ORIENTATION_0);
9410 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9411 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9412
9413 NotifyMotionArgs motionArgs;
9414
9415 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9416 // First finger down.
9417 processId(mapper, FIRST_TRACKING_ID);
9418 processPosition(mapper, x1, y1);
9419 processPressure(mapper, RAW_PRESSURE_MAX);
9420 processSync(mapper);
9421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9422 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9423 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9424
9425 // First finger move.
9426 processId(mapper, FIRST_TRACKING_ID);
9427 processPosition(mapper, x1 + 1, y1 + 1);
9428 processPressure(mapper, RAW_PRESSURE_MAX);
9429 processSync(mapper);
9430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9432 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9433
9434 // Second finger down.
9435 processSlot(mapper, SECOND_SLOT);
9436 processId(mapper, SECOND_TRACKING_ID);
9437 processPosition(mapper, x2, y2);
9438 processPressure(mapper, RAW_PRESSURE_MAX);
9439 processSync(mapper);
9440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009441 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009442 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9443
9444 // second finger up with some unexpected data.
9445 processSlot(mapper, SECOND_SLOT);
9446 processId(mapper, INVALID_TRACKING_ID);
9447 processPosition(mapper, x2, y2);
9448 processSync(mapper);
9449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009450 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009451 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9452
9453 // first finger up with some unexpected data.
9454 processSlot(mapper, FIRST_SLOT);
9455 processId(mapper, INVALID_TRACKING_ID);
9456 processPosition(mapper, x2, y2);
9457 processPressure(mapper, RAW_PRESSURE_MAX);
9458 processSync(mapper);
9459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9460 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9461 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9462}
9463
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009464TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9465 addConfigurationProperty("touch.deviceType", "touchScreen");
9466 prepareDisplay(DISPLAY_ORIENTATION_0);
9467 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9468 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9469
9470 // First finger down.
9471 processId(mapper, FIRST_TRACKING_ID);
9472 processPosition(mapper, 100, 200);
9473 processPressure(mapper, RAW_PRESSURE_MAX);
9474 processSync(mapper);
9475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9476 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9477
9478 // Second finger down.
9479 processSlot(mapper, SECOND_SLOT);
9480 processId(mapper, SECOND_TRACKING_ID);
9481 processPosition(mapper, 300, 400);
9482 processPressure(mapper, RAW_PRESSURE_MAX);
9483 processSync(mapper);
9484 ASSERT_NO_FATAL_FAILURE(
9485 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9486
9487 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
9488 // preserved. Resetting should not generate any events.
9489 mapper.reset(ARBITRARY_TIME);
9490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9491
9492 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9493 // the existing touch state to generate a down event.
9494 processPosition(mapper, 301, 302);
9495 processSync(mapper);
9496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9497 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
9498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9499 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
9500
9501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9502}
9503
9504TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9505 addConfigurationProperty("touch.deviceType", "touchScreen");
9506 prepareDisplay(DISPLAY_ORIENTATION_0);
9507 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9508 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9509
9510 // First finger touches down and releases.
9511 processId(mapper, FIRST_TRACKING_ID);
9512 processPosition(mapper, 100, 200);
9513 processPressure(mapper, RAW_PRESSURE_MAX);
9514 processSync(mapper);
9515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9516 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9517 processId(mapper, INVALID_TRACKING_ID);
9518 processSync(mapper);
9519 ASSERT_NO_FATAL_FAILURE(
9520 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9521
9522 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9523 // raw state where no pointers are down.
9524 mapper.reset(ARBITRARY_TIME);
9525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9526
9527 // Send an empty sync frame. Since there are no pointers, no events are generated.
9528 processSync(mapper);
9529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9530}
9531
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009532// --- MultiTouchInputMapperTest_ExternalDevice ---
9533
9534class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9535protected:
Chris Yea52ade12020-08-27 16:49:20 -07009536 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009537};
9538
9539/**
9540 * Expect fallback to internal viewport if device is external and external viewport is not present.
9541 */
9542TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9543 prepareAxes(POSITION);
9544 addConfigurationProperty("touch.deviceType", "touchScreen");
9545 prepareDisplay(DISPLAY_ORIENTATION_0);
9546 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9547
9548 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9549
9550 NotifyMotionArgs motionArgs;
9551
9552 // Expect the event to be sent to the internal viewport,
9553 // because an external viewport is not present.
9554 processPosition(mapper, 100, 100);
9555 processSync(mapper);
9556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9557 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9558
9559 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009560 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009561 processPosition(mapper, 100, 100);
9562 processSync(mapper);
9563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9564 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9565}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009566
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009567TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9568 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9569 std::shared_ptr<FakePointerController> fakePointerController =
9570 std::make_shared<FakePointerController>();
9571 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9572 fakePointerController->setPosition(0, 0);
9573 fakePointerController->setButtonState(0);
9574
9575 // prepare device and capture
9576 prepareDisplay(DISPLAY_ORIENTATION_0);
9577 prepareAxes(POSITION | ID | SLOT);
9578 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9579 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9580 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009581 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009582 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9583
9584 // captured touchpad should be a touchpad source
9585 NotifyDeviceResetArgs resetArgs;
9586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9587 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9588
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009589 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009590
9591 const InputDeviceInfo::MotionRange* relRangeX =
9592 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9593 ASSERT_NE(relRangeX, nullptr);
9594 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9595 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9596 const InputDeviceInfo::MotionRange* relRangeY =
9597 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9598 ASSERT_NE(relRangeY, nullptr);
9599 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9600 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9601
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009602 // run captured pointer tests - note that this is unscaled, so input listener events should be
9603 // identical to what the hardware sends (accounting for any
9604 // calibration).
9605 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009606 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009607 processId(mapper, 1);
9608 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9609 processKey(mapper, BTN_TOUCH, 1);
9610 processSync(mapper);
9611
9612 // expect coord[0] to contain initial location of touch 0
9613 NotifyMotionArgs args;
9614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9615 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9616 ASSERT_EQ(1U, args.pointerCount);
9617 ASSERT_EQ(0, args.pointerProperties[0].id);
9618 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9619 ASSERT_NO_FATAL_FAILURE(
9620 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9621
9622 // FINGER 1 DOWN
9623 processSlot(mapper, 1);
9624 processId(mapper, 2);
9625 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9626 processSync(mapper);
9627
9628 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009630 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009631 ASSERT_EQ(2U, args.pointerCount);
9632 ASSERT_EQ(0, args.pointerProperties[0].id);
9633 ASSERT_EQ(1, args.pointerProperties[1].id);
9634 ASSERT_NO_FATAL_FAILURE(
9635 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9636 ASSERT_NO_FATAL_FAILURE(
9637 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9638
9639 // FINGER 1 MOVE
9640 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9641 processSync(mapper);
9642
9643 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9644 // from move
9645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9646 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9647 ASSERT_NO_FATAL_FAILURE(
9648 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9649 ASSERT_NO_FATAL_FAILURE(
9650 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9651
9652 // FINGER 0 MOVE
9653 processSlot(mapper, 0);
9654 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9655 processSync(mapper);
9656
9657 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9659 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9660 ASSERT_NO_FATAL_FAILURE(
9661 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9662 ASSERT_NO_FATAL_FAILURE(
9663 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9664
9665 // BUTTON DOWN
9666 processKey(mapper, BTN_LEFT, 1);
9667 processSync(mapper);
9668
9669 // touchinputmapper design sends a move before button press
9670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9671 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9673 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9674
9675 // BUTTON UP
9676 processKey(mapper, BTN_LEFT, 0);
9677 processSync(mapper);
9678
9679 // touchinputmapper design sends a move after button release
9680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9681 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9683 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9684
9685 // FINGER 0 UP
9686 processId(mapper, -1);
9687 processSync(mapper);
9688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9689 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9690
9691 // FINGER 1 MOVE
9692 processSlot(mapper, 1);
9693 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9694 processSync(mapper);
9695
9696 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9698 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9699 ASSERT_EQ(1U, args.pointerCount);
9700 ASSERT_EQ(1, args.pointerProperties[0].id);
9701 ASSERT_NO_FATAL_FAILURE(
9702 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9703
9704 // FINGER 1 UP
9705 processId(mapper, -1);
9706 processKey(mapper, BTN_TOUCH, 0);
9707 processSync(mapper);
9708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9709 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9710
9711 // non captured touchpad should be a mouse source
9712 mFakePolicy->setPointerCapture(false);
9713 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9715 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9716}
9717
9718TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9719 std::shared_ptr<FakePointerController> fakePointerController =
9720 std::make_shared<FakePointerController>();
9721 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9722 fakePointerController->setPosition(0, 0);
9723 fakePointerController->setButtonState(0);
9724
9725 // prepare device and capture
9726 prepareDisplay(DISPLAY_ORIENTATION_0);
9727 prepareAxes(POSITION | ID | SLOT);
9728 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9729 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009730 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009731 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9732 // run uncaptured pointer tests - pushes out generic events
9733 // FINGER 0 DOWN
9734 processId(mapper, 3);
9735 processPosition(mapper, 100, 100);
9736 processKey(mapper, BTN_TOUCH, 1);
9737 processSync(mapper);
9738
9739 // start at (100,100), cursor should be at (0,0) * scale
9740 NotifyMotionArgs args;
9741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9742 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9743 ASSERT_NO_FATAL_FAILURE(
9744 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9745
9746 // FINGER 0 MOVE
9747 processPosition(mapper, 200, 200);
9748 processSync(mapper);
9749
9750 // compute scaling to help with touch position checking
9751 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9752 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9753 float scale =
9754 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9755
9756 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9758 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9760 0, 0, 0, 0, 0, 0, 0));
9761}
9762
9763TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9764 std::shared_ptr<FakePointerController> fakePointerController =
9765 std::make_shared<FakePointerController>();
9766
9767 prepareDisplay(DISPLAY_ORIENTATION_0);
9768 prepareAxes(POSITION | ID | SLOT);
9769 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009770 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009771 mFakePolicy->setPointerCapture(false);
9772 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9773
9774 // uncaptured touchpad should be a pointer device
9775 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9776
9777 // captured touchpad should be a touchpad device
9778 mFakePolicy->setPointerCapture(true);
9779 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9780 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9781}
9782
HQ Liue6983c72022-04-19 22:14:56 +00009783class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9784protected:
9785 float mPointerMovementScale;
9786 float mPointerXZoomScale;
9787 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9788 addConfigurationProperty("touch.deviceType", "pointer");
9789 std::shared_ptr<FakePointerController> fakePointerController =
9790 std::make_shared<FakePointerController>();
9791 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9792 fakePointerController->setPosition(0, 0);
9793 fakePointerController->setButtonState(0);
9794 prepareDisplay(DISPLAY_ORIENTATION_0);
9795
9796 prepareAxes(POSITION);
9797 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9798 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9799 // needs to be disabled, and the pointer gesture needs to be enabled.
9800 mFakePolicy->setPointerCapture(false);
9801 mFakePolicy->setPointerGestureEnabled(true);
9802 mFakePolicy->setPointerController(fakePointerController);
9803
9804 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9805 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9806 mPointerMovementScale =
9807 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9808 mPointerXZoomScale =
9809 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9810 }
9811
9812 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9813 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9814 /*flat*/ 0,
9815 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9816 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9817 /*flat*/ 0,
9818 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9819 }
9820};
9821
9822/**
9823 * Two fingers down on a pointer mode touch pad. The width
9824 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9825 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9826 * be greater than the both value to be freeform gesture, so that after two
9827 * fingers start to move downwards, the gesture should be swipe.
9828 */
9829TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9830 // The min freeform gesture width is 25units/mm x 30mm = 750
9831 // which is greater than fraction of the diagnal length of the touchpad (349).
9832 // Thus, MaxSwipWidth is 750.
9833 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9834 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9835 NotifyMotionArgs motionArgs;
9836
9837 // Two fingers down at once.
9838 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9839 // Pointer's initial position is used the [0,0] coordinate.
9840 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9841
9842 processId(mapper, FIRST_TRACKING_ID);
9843 processPosition(mapper, x1, y1);
9844 processMTSync(mapper);
9845 processId(mapper, SECOND_TRACKING_ID);
9846 processPosition(mapper, x2, y2);
9847 processMTSync(mapper);
9848 processSync(mapper);
9849
9850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9851 ASSERT_EQ(1U, motionArgs.pointerCount);
9852 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9853 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009854 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009855 ASSERT_NO_FATAL_FAILURE(
9856 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9857
9858 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9859 // that there should be 1 pointer.
9860 int32_t movingDistance = 200;
9861 y1 += movingDistance;
9862 y2 += movingDistance;
9863
9864 processId(mapper, FIRST_TRACKING_ID);
9865 processPosition(mapper, x1, y1);
9866 processMTSync(mapper);
9867 processId(mapper, SECOND_TRACKING_ID);
9868 processPosition(mapper, x2, y2);
9869 processMTSync(mapper);
9870 processSync(mapper);
9871
9872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9873 ASSERT_EQ(1U, motionArgs.pointerCount);
9874 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9875 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009876 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009877 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9878 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9879 0, 0, 0, 0));
9880}
9881
9882/**
9883 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9884 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9885 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9886 * value to be freeform gesture, so that after two fingers start to move downwards,
9887 * the gesture should be swipe.
9888 */
9889TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9890 // The min freeform gesture width is 5units/mm x 30mm = 150
9891 // which is greater than fraction of the diagnal length of the touchpad (349).
9892 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
9893 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
9894 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9895 NotifyMotionArgs motionArgs;
9896
9897 // Two fingers down at once.
9898 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9899 // Pointer's initial position is used the [0,0] coordinate.
9900 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9901
9902 processId(mapper, FIRST_TRACKING_ID);
9903 processPosition(mapper, x1, y1);
9904 processMTSync(mapper);
9905 processId(mapper, SECOND_TRACKING_ID);
9906 processPosition(mapper, x2, y2);
9907 processMTSync(mapper);
9908 processSync(mapper);
9909
9910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9911 ASSERT_EQ(1U, motionArgs.pointerCount);
9912 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9913 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009914 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009915 ASSERT_NO_FATAL_FAILURE(
9916 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9917
9918 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9919 // and there should be 1 pointer.
9920 int32_t movingDistance = 200;
9921 y1 += movingDistance;
9922 y2 += movingDistance;
9923
9924 processId(mapper, FIRST_TRACKING_ID);
9925 processPosition(mapper, x1, y1);
9926 processMTSync(mapper);
9927 processId(mapper, SECOND_TRACKING_ID);
9928 processPosition(mapper, x2, y2);
9929 processMTSync(mapper);
9930 processSync(mapper);
9931
9932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9933 ASSERT_EQ(1U, motionArgs.pointerCount);
9934 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9935 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009936 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009937 // New coordinate is the scaled relative coordinate from the initial coordinate.
9938 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9939 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9940 0, 0, 0, 0));
9941}
9942
9943/**
9944 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
9945 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
9946 * freeform gestures after two fingers start to move downwards.
9947 */
9948TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
9949 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9950 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9951
9952 NotifyMotionArgs motionArgs;
9953
9954 // Two fingers down at once. Wider than the max swipe width.
9955 // The gesture is expected to be PRESS, then transformed to FREEFORM
9956 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
9957
9958 processId(mapper, FIRST_TRACKING_ID);
9959 processPosition(mapper, x1, y1);
9960 processMTSync(mapper);
9961 processId(mapper, SECOND_TRACKING_ID);
9962 processPosition(mapper, x2, y2);
9963 processMTSync(mapper);
9964 processSync(mapper);
9965
9966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9967 ASSERT_EQ(1U, motionArgs.pointerCount);
9968 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9969 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009970 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009971 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
9972 ASSERT_NO_FATAL_FAILURE(
9973 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9974
9975 int32_t movingDistance = 200;
9976
9977 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
9978 // then two down events for two pointers.
9979 y1 += movingDistance;
9980 y2 += movingDistance;
9981
9982 processId(mapper, FIRST_TRACKING_ID);
9983 processPosition(mapper, x1, y1);
9984 processMTSync(mapper);
9985 processId(mapper, SECOND_TRACKING_ID);
9986 processPosition(mapper, x2, y2);
9987 processMTSync(mapper);
9988 processSync(mapper);
9989
9990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9991 // The previous PRESS gesture is cancelled, because it is transformed to freeform
9992 ASSERT_EQ(1U, motionArgs.pointerCount);
9993 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9995 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9996 ASSERT_EQ(1U, motionArgs.pointerCount);
9997 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9999 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010000 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010001 ASSERT_EQ(2U, motionArgs.pointerCount);
10002 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
10003 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010004 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010005 // Two pointers' scaled relative coordinates from their initial centroid.
10006 // Initial y coordinates are 0 as y1 and y2 have the same value.
10007 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10008 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10009 // When pointers move, the new coordinates equal to the initial coordinates plus
10010 // scaled moving distance.
10011 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10012 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10013 0, 0, 0, 0));
10014 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10015 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10016 0, 0, 0, 0));
10017
10018 // Move two fingers down again, expect one MOVE motion event.
10019 y1 += movingDistance;
10020 y2 += movingDistance;
10021
10022 processId(mapper, FIRST_TRACKING_ID);
10023 processPosition(mapper, x1, y1);
10024 processMTSync(mapper);
10025 processId(mapper, SECOND_TRACKING_ID);
10026 processPosition(mapper, x2, y2);
10027 processMTSync(mapper);
10028 processSync(mapper);
10029
10030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10031 ASSERT_EQ(2U, motionArgs.pointerCount);
10032 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10033 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010034 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010035 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10036 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10037 0, 0, 0, 0, 0));
10038 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10039 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10040 0, 0, 0, 0, 0));
10041}
10042
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010043// --- JoystickInputMapperTest ---
10044
10045class JoystickInputMapperTest : public InputMapperTest {
10046protected:
10047 static const int32_t RAW_X_MIN;
10048 static const int32_t RAW_X_MAX;
10049 static const int32_t RAW_Y_MIN;
10050 static const int32_t RAW_Y_MAX;
10051
10052 void SetUp() override {
10053 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10054 }
10055 void prepareAxes() {
10056 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10057 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10058 }
10059
10060 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10061 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10062 }
10063
10064 void processSync(JoystickInputMapper& mapper) {
10065 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10066 }
10067
10068 void prepareVirtualDisplay(int32_t orientation) {
10069 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10070 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10071 NO_PORT, ViewportType::VIRTUAL);
10072 }
10073};
10074
10075const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10076const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10077const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10078const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10079
10080TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10081 prepareAxes();
10082 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10083
10084 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10085
10086 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
10087
10088 // Send an axis event
10089 processAxis(mapper, ABS_X, 100);
10090 processSync(mapper);
10091
10092 NotifyMotionArgs args;
10093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10094 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10095
10096 // Send another axis event
10097 processAxis(mapper, ABS_Y, 100);
10098 processSync(mapper);
10099
10100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10101 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10102}
10103
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010104// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010105
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010106class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010107protected:
10108 static const char* DEVICE_NAME;
10109 static const char* DEVICE_LOCATION;
10110 static const int32_t DEVICE_ID;
10111 static const int32_t DEVICE_GENERATION;
10112 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010113 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010114 static const int32_t EVENTHUB_ID;
10115
10116 std::shared_ptr<FakeEventHub> mFakeEventHub;
10117 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010118 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010119 std::unique_ptr<InstrumentedInputReader> mReader;
10120 std::shared_ptr<InputDevice> mDevice;
10121
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010122 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010123 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010124 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010125 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010126 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010127 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010128 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10129 }
10130
10131 void SetUp() override { SetUp(DEVICE_CLASSES); }
10132
10133 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010134 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010135 mFakePolicy.clear();
10136 }
10137
10138 void configureDevice(uint32_t changes) {
10139 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10140 mReader->requestRefreshConfiguration(changes);
10141 mReader->loopOnce();
10142 }
10143 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
10144 }
10145
10146 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10147 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010148 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010149 InputDeviceIdentifier identifier;
10150 identifier.name = name;
10151 identifier.location = location;
10152 std::shared_ptr<InputDevice> device =
10153 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10154 identifier);
10155 mReader->pushNextDevice(device);
10156 mFakeEventHub->addDevice(eventHubId, name, classes);
10157 mReader->loopOnce();
10158 return device;
10159 }
10160
10161 template <class T, typename... Args>
10162 T& addControllerAndConfigure(Args... args) {
10163 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10164
10165 return controller;
10166 }
10167};
10168
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010169const char* PeripheralControllerTest::DEVICE_NAME = "device";
10170const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10171const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10172const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10173const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010174const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10175 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010176const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010177
10178// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010179class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010180protected:
10181 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010182 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010183 }
10184};
10185
10186TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010187 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010188
10189 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
10190 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
10191}
10192
10193TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010194 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010195
10196 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
10197 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
10198}
10199
10200// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010201class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010202protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010203 void SetUp() override {
10204 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10205 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010206};
10207
Chris Ye85758332021-05-16 23:05:17 -070010208TEST_F(LightControllerTest, MonoLight) {
10209 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010210 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010211 .maxBrightness = 255,
10212 .flags = InputLightClass::BRIGHTNESS,
10213 .path = ""};
10214 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010215
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010216 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010217 InputDeviceInfo info;
10218 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010219 std::vector<InputDeviceLightInfo> lights = info.getLights();
10220 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010221 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10222 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10223
10224 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10225 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10226}
10227
10228TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10229 RawLightInfo infoMono = {.id = 1,
10230 .name = "mono_keyboard_backlight",
10231 .maxBrightness = 255,
10232 .flags = InputLightClass::BRIGHTNESS |
10233 InputLightClass::KEYBOARD_BACKLIGHT,
10234 .path = ""};
10235 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10236
10237 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10238 InputDeviceInfo info;
10239 controller.populateDeviceInfo(&info);
10240 std::vector<InputDeviceLightInfo> lights = info.getLights();
10241 ASSERT_EQ(1U, lights.size());
10242 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10243 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010244
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010245 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10246 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010247}
10248
10249TEST_F(LightControllerTest, RGBLight) {
10250 RawLightInfo infoRed = {.id = 1,
10251 .name = "red",
10252 .maxBrightness = 255,
10253 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10254 .path = ""};
10255 RawLightInfo infoGreen = {.id = 2,
10256 .name = "green",
10257 .maxBrightness = 255,
10258 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10259 .path = ""};
10260 RawLightInfo infoBlue = {.id = 3,
10261 .name = "blue",
10262 .maxBrightness = 255,
10263 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10264 .path = ""};
10265 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10266 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10267 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10268
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010269 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010270 InputDeviceInfo info;
10271 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010272 std::vector<InputDeviceLightInfo> lights = info.getLights();
10273 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010274 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10275 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10276 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10277
10278 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10279 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10280}
10281
10282TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10283 RawLightInfo infoRed = {.id = 1,
10284 .name = "red_keyboard_backlight",
10285 .maxBrightness = 255,
10286 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10287 InputLightClass::KEYBOARD_BACKLIGHT,
10288 .path = ""};
10289 RawLightInfo infoGreen = {.id = 2,
10290 .name = "green_keyboard_backlight",
10291 .maxBrightness = 255,
10292 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10293 InputLightClass::KEYBOARD_BACKLIGHT,
10294 .path = ""};
10295 RawLightInfo infoBlue = {.id = 3,
10296 .name = "blue_keyboard_backlight",
10297 .maxBrightness = 255,
10298 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10299 InputLightClass::KEYBOARD_BACKLIGHT,
10300 .path = ""};
10301 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10302 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10303 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10304
10305 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10306 InputDeviceInfo info;
10307 controller.populateDeviceInfo(&info);
10308 std::vector<InputDeviceLightInfo> lights = info.getLights();
10309 ASSERT_EQ(1U, lights.size());
10310 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10311 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10312 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10313
10314 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10315 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10316}
10317
10318TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10319 RawLightInfo infoRed = {.id = 1,
10320 .name = "red",
10321 .maxBrightness = 255,
10322 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10323 .path = ""};
10324 RawLightInfo infoGreen = {.id = 2,
10325 .name = "green",
10326 .maxBrightness = 255,
10327 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10328 .path = ""};
10329 RawLightInfo infoBlue = {.id = 3,
10330 .name = "blue",
10331 .maxBrightness = 255,
10332 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10333 .path = ""};
10334 RawLightInfo infoGlobal = {.id = 3,
10335 .name = "global_keyboard_backlight",
10336 .maxBrightness = 255,
10337 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10338 InputLightClass::KEYBOARD_BACKLIGHT,
10339 .path = ""};
10340 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10341 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10342 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10343 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10344
10345 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10346 InputDeviceInfo info;
10347 controller.populateDeviceInfo(&info);
10348 std::vector<InputDeviceLightInfo> lights = info.getLights();
10349 ASSERT_EQ(1U, lights.size());
10350 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10351 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10352 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010353
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010354 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10355 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010356}
10357
10358TEST_F(LightControllerTest, MultiColorRGBLight) {
10359 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010360 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010361 .maxBrightness = 255,
10362 .flags = InputLightClass::BRIGHTNESS |
10363 InputLightClass::MULTI_INTENSITY |
10364 InputLightClass::MULTI_INDEX,
10365 .path = ""};
10366
10367 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10368
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010369 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010370 InputDeviceInfo info;
10371 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010372 std::vector<InputDeviceLightInfo> lights = info.getLights();
10373 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010374 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10375 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10376 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10377
10378 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10379 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10380}
10381
10382TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10383 RawLightInfo infoColor = {.id = 1,
10384 .name = "multi_color_keyboard_backlight",
10385 .maxBrightness = 255,
10386 .flags = InputLightClass::BRIGHTNESS |
10387 InputLightClass::MULTI_INTENSITY |
10388 InputLightClass::MULTI_INDEX |
10389 InputLightClass::KEYBOARD_BACKLIGHT,
10390 .path = ""};
10391
10392 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10393
10394 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10395 InputDeviceInfo info;
10396 controller.populateDeviceInfo(&info);
10397 std::vector<InputDeviceLightInfo> lights = info.getLights();
10398 ASSERT_EQ(1U, lights.size());
10399 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10400 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10401 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010402
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010403 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10404 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010405}
10406
10407TEST_F(LightControllerTest, PlayerIdLight) {
10408 RawLightInfo info1 = {.id = 1,
10409 .name = "player1",
10410 .maxBrightness = 255,
10411 .flags = InputLightClass::BRIGHTNESS,
10412 .path = ""};
10413 RawLightInfo info2 = {.id = 2,
10414 .name = "player2",
10415 .maxBrightness = 255,
10416 .flags = InputLightClass::BRIGHTNESS,
10417 .path = ""};
10418 RawLightInfo info3 = {.id = 3,
10419 .name = "player3",
10420 .maxBrightness = 255,
10421 .flags = InputLightClass::BRIGHTNESS,
10422 .path = ""};
10423 RawLightInfo info4 = {.id = 4,
10424 .name = "player4",
10425 .maxBrightness = 255,
10426 .flags = InputLightClass::BRIGHTNESS,
10427 .path = ""};
10428 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10429 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10430 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10431 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10432
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010433 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010434 InputDeviceInfo info;
10435 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010436 std::vector<InputDeviceLightInfo> lights = info.getLights();
10437 ASSERT_EQ(1U, lights.size());
10438 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010439 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10440 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010441
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010442 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10443 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10444 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010445}
10446
Michael Wrightd02c5b62014-02-10 15:10:22 -080010447} // namespace android