blob: 9d14a862cd83f124573bea462151c561c8ddd6d0 [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;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000247 std::optional<int32_t /*deviceId*/> mStylusGestureNotified GUARDED_BY(mLock){};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800248
249protected:
Chris Yea52ade12020-08-27 16:49:20 -0700250 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800251
252public:
253 FakeInputReaderPolicy() {
254 }
255
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700256 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800257 waitForInputDevices([](bool devicesChanged) {
258 if (!devicesChanged) {
259 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
260 }
261 });
262 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700263
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800264 void assertInputDevicesNotChanged() {
265 waitForInputDevices([](bool devicesChanged) {
266 if (devicesChanged) {
267 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
268 }
269 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700270 }
271
Prabir Pradhanda20b172022-09-26 17:01:18 +0000272 void assertStylusGestureNotified(int32_t deviceId) {
273 std::scoped_lock lock(mLock);
274 ASSERT_TRUE(mStylusGestureNotified);
275 ASSERT_EQ(deviceId, *mStylusGestureNotified);
276 mStylusGestureNotified.reset();
277 }
278
279 void assertStylusGestureNotNotified() {
280 std::scoped_lock lock(mLock);
281 ASSERT_FALSE(mStylusGestureNotified);
282 }
283
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700284 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100285 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100286 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700287 }
288
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700289 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
290 return mConfig.getDisplayViewportByUniqueId(uniqueId);
291 }
292 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
293 return mConfig.getDisplayViewportByType(type);
294 }
295
296 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
297 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700298 }
299
Prabir Pradhan5632d622021-09-06 07:57:20 -0700300 void addDisplayViewport(DisplayViewport viewport) {
301 mViewports.push_back(std::move(viewport));
302 mConfig.setDisplayViewports(mViewports);
303 }
304
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700305 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000306 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700307 std::optional<uint8_t> physicalPort, ViewportType type) {
308 const bool isRotated =
309 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
310 DisplayViewport v;
311 v.displayId = displayId;
312 v.orientation = orientation;
313 v.logicalLeft = 0;
314 v.logicalTop = 0;
315 v.logicalRight = isRotated ? height : width;
316 v.logicalBottom = isRotated ? width : height;
317 v.physicalLeft = 0;
318 v.physicalTop = 0;
319 v.physicalRight = isRotated ? height : width;
320 v.physicalBottom = isRotated ? width : height;
321 v.deviceWidth = isRotated ? height : width;
322 v.deviceHeight = isRotated ? width : height;
323 v.isActive = isActive;
324 v.uniqueId = uniqueId;
325 v.physicalPort = physicalPort;
326 v.type = type;
327
328 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329 }
330
Arthur Hung6cd19a42019-08-30 19:04:12 +0800331 bool updateViewport(const DisplayViewport& viewport) {
332 size_t count = mViewports.size();
333 for (size_t i = 0; i < count; i++) {
334 const DisplayViewport& currentViewport = mViewports[i];
335 if (currentViewport.displayId == viewport.displayId) {
336 mViewports[i] = viewport;
337 mConfig.setDisplayViewports(mViewports);
338 return true;
339 }
340 }
341 // no viewport found.
342 return false;
343 }
344
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100345 void addExcludedDeviceName(const std::string& deviceName) {
346 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800347 }
348
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700349 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
350 mConfig.portAssociations.insert({inputPort, displayPort});
351 }
352
Christine Franks1ba71cc2021-04-07 14:37:42 -0700353 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
354 const std::string& displayUniqueId) {
355 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
356 }
357
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000358 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700359
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000360 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700361
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000362 void setPointerController(std::shared_ptr<FakePointerController> controller) {
363 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800364 }
365
366 const InputReaderConfiguration* getReaderConfiguration() const {
367 return &mConfig;
368 }
369
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800370 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800371 return mInputDevices;
372 }
373
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100374 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700375 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700376 return transform;
377 }
378
379 void setTouchAffineTransformation(const TouchAffineTransformation t) {
380 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800381 }
382
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000383 PointerCaptureRequest setPointerCapture(bool enabled) {
384 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
385 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800386 }
387
Arthur Hung7c645402019-01-25 17:45:42 +0800388 void setShowTouches(bool enabled) {
389 mConfig.showTouches = enabled;
390 }
391
Garfield Tan888a6a42020-01-09 11:39:16 -0800392 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
393 mConfig.defaultPointerDisplayId = pointerDisplayId;
394 }
395
HQ Liue6983c72022-04-19 22:14:56 +0000396 void setPointerGestureEnabled(bool enabled) { mConfig.pointerGesturesEnabled = enabled; }
397
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800398 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
399
HQ Liue6983c72022-04-19 22:14:56 +0000400 float getPointerGestureZoomSpeedRatio() { return mConfig.pointerGestureZoomSpeedRatio; }
401
Prabir Pradhanf99d6e72022-04-21 15:28:35 +0000402 void setVelocityControlParams(const VelocityControlParameters& params) {
403 mConfig.pointerVelocityControlParameters = params;
404 mConfig.wheelVelocityControlParameters = params;
405 }
406
Michael Wrightd02c5b62014-02-10 15:10:22 -0800407private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000408 uint32_t mNextPointerCaptureSequenceNumber = 0;
409
Chris Yea52ade12020-08-27 16:49:20 -0700410 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800411 *outConfig = mConfig;
412 }
413
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000414 std::shared_ptr<PointerControllerInterface> obtainPointerController(
415 int32_t /*deviceId*/) override {
416 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800417 }
418
Chris Yea52ade12020-08-27 16:49:20 -0700419 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700420 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800421 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700422 mInputDevicesChanged = true;
423 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800424 }
425
Chris Yea52ade12020-08-27 16:49:20 -0700426 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
427 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700428 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800429 }
430
Chris Yea52ade12020-08-27 16:49:20 -0700431 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800432
433 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
434 std::unique_lock<std::mutex> lock(mLock);
435 base::ScopedLockAssertion assumeLocked(mLock);
436
437 const bool devicesChanged =
438 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
439 return mInputDevicesChanged;
440 });
441 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
442 mInputDevicesChanged = false;
443 }
Prabir Pradhanda20b172022-09-26 17:01:18 +0000444
445 void notifyStylusGestureStarted(int32_t deviceId, nsecs_t eventTime) override {
446 std::scoped_lock<std::mutex> lock(mLock);
447 mStylusGestureNotified = deviceId;
448 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800449};
450
Michael Wrightd02c5b62014-02-10 15:10:22 -0800451// --- FakeEventHub ---
452
453class FakeEventHub : public EventHubInterface {
454 struct KeyInfo {
455 int32_t keyCode;
456 uint32_t flags;
457 };
458
Chris Yef59a2f42020-10-16 12:55:26 -0700459 struct SensorInfo {
460 InputDeviceSensorType sensorType;
461 int32_t sensorDataIndex;
462 };
463
Michael Wrightd02c5b62014-02-10 15:10:22 -0800464 struct Device {
465 InputDeviceIdentifier identifier;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700466 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800467 PropertyMap configuration;
468 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
469 KeyedVector<int, bool> relativeAxes;
470 KeyedVector<int32_t, int32_t> keyCodeStates;
471 KeyedVector<int32_t, int32_t> scanCodeStates;
472 KeyedVector<int32_t, int32_t> switchStates;
473 KeyedVector<int32_t, int32_t> absoluteAxisValue;
474 KeyedVector<int32_t, KeyInfo> keysByScanCode;
475 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
476 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100477 // fake mapping which would normally come from keyCharacterMap
478 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700479 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
480 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800481 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700482 bool enabled;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000483 InputDeviceCountryCode countryCode;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700484
485 status_t enable() {
486 enabled = true;
487 return OK;
488 }
489
490 status_t disable() {
491 enabled = false;
492 return OK;
493 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800494
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700495 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800496 };
497
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700498 std::mutex mLock;
499 std::condition_variable mEventsCondition;
500
Michael Wrightd02c5b62014-02-10 15:10:22 -0800501 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100502 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000503 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600504 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000505 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800506 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
507 // Simulates a device light brightness, from light id to light brightness.
508 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
509 // Simulates a device light intensities, from light id to light intensities map.
510 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
511 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800512
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700513public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800514 virtual ~FakeEventHub() {
515 for (size_t i = 0; i < mDevices.size(); i++) {
516 delete mDevices.valueAt(i);
517 }
518 }
519
Michael Wrightd02c5b62014-02-10 15:10:22 -0800520 FakeEventHub() { }
521
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700522 void addDevice(int32_t deviceId, const std::string& name,
523 ftl::Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800524 Device* device = new Device(classes);
525 device->identifier.name = name;
526 mDevices.add(deviceId, device);
527
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000528 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800529 }
530
531 void removeDevice(int32_t deviceId) {
532 delete mDevices.valueFor(deviceId);
533 mDevices.removeItem(deviceId);
534
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000535 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800536 }
537
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000538 bool isDeviceEnabled(int32_t deviceId) const override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700539 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700540 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700541 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
542 return false;
543 }
544 return device->enabled;
545 }
546
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000547 status_t enableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700548 status_t result;
549 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700550 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700551 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
552 return BAD_VALUE;
553 }
554 if (device->enabled) {
555 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
556 return OK;
557 }
558 result = device->enable();
559 return result;
560 }
561
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000562 status_t disableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700563 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700564 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700565 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
566 return BAD_VALUE;
567 }
568 if (!device->enabled) {
569 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
570 return OK;
571 }
572 return device->disable();
573 }
574
Michael Wrightd02c5b62014-02-10 15:10:22 -0800575 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000576 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800577 }
578
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700579 void addConfigurationProperty(int32_t deviceId, const char* key, const char* value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800580 Device* device = getDevice(deviceId);
581 device->configuration.addProperty(key, value);
582 }
583
584 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
585 Device* device = getDevice(deviceId);
586 device->configuration.addAll(configuration);
587 }
588
589 void addAbsoluteAxis(int32_t deviceId, int axis,
590 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
591 Device* device = getDevice(deviceId);
592
593 RawAbsoluteAxisInfo info;
594 info.valid = true;
595 info.minValue = minValue;
596 info.maxValue = maxValue;
597 info.flat = flat;
598 info.fuzz = fuzz;
599 info.resolution = resolution;
600 device->absoluteAxes.add(axis, info);
601 }
602
603 void addRelativeAxis(int32_t deviceId, int32_t axis) {
604 Device* device = getDevice(deviceId);
605 device->relativeAxes.add(axis, true);
606 }
607
608 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
609 Device* device = getDevice(deviceId);
610 device->keyCodeStates.replaceValueFor(keyCode, state);
611 }
612
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000613 void setCountryCode(int32_t deviceId, InputDeviceCountryCode countryCode) {
614 Device* device = getDevice(deviceId);
615 device->countryCode = countryCode;
616 }
617
Michael Wrightd02c5b62014-02-10 15:10:22 -0800618 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
619 Device* device = getDevice(deviceId);
620 device->scanCodeStates.replaceValueFor(scanCode, state);
621 }
622
623 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
624 Device* device = getDevice(deviceId);
625 device->switchStates.replaceValueFor(switchCode, state);
626 }
627
628 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
629 Device* device = getDevice(deviceId);
630 device->absoluteAxisValue.replaceValueFor(axis, value);
631 }
632
633 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
634 int32_t keyCode, uint32_t flags) {
635 Device* device = getDevice(deviceId);
636 KeyInfo info;
637 info.keyCode = keyCode;
638 info.flags = flags;
639 if (scanCode) {
640 device->keysByScanCode.add(scanCode, info);
641 }
642 if (usageCode) {
643 device->keysByUsageCode.add(usageCode, info);
644 }
645 }
646
Philip Junker4af3b3d2021-12-14 10:36:55 +0100647 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
648 Device* device = getDevice(deviceId);
649 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
650 }
651
Michael Wrightd02c5b62014-02-10 15:10:22 -0800652 void addLed(int32_t deviceId, int32_t led, bool initialState) {
653 Device* device = getDevice(deviceId);
654 device->leds.add(led, initialState);
655 }
656
Chris Yef59a2f42020-10-16 12:55:26 -0700657 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
658 int32_t sensorDataIndex) {
659 Device* device = getDevice(deviceId);
660 SensorInfo info;
661 info.sensorType = sensorType;
662 info.sensorDataIndex = sensorDataIndex;
663 device->sensorsByAbsCode.emplace(absCode, info);
664 }
665
666 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
667 Device* device = getDevice(deviceId);
668 typename BitArray<MSC_MAX>::Buffer buffer;
669 buffer[mscEvent / 32] = 1 << mscEvent % 32;
670 device->mscBitmask.loadFromBuffer(buffer);
671 }
672
Chris Ye3fdbfef2021-01-06 18:45:18 -0800673 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
674 mRawLightInfos.emplace(rawId, std::move(info));
675 }
676
677 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
678 mLightBrightness.emplace(rawId, brightness);
679 }
680
681 void fakeLightIntensities(int32_t rawId,
682 const std::unordered_map<LightColor, int32_t> intensities) {
683 mLightIntensities.emplace(rawId, std::move(intensities));
684 }
685
Michael Wrightd02c5b62014-02-10 15:10:22 -0800686 bool getLedState(int32_t deviceId, int32_t led) {
687 Device* device = getDevice(deviceId);
688 return device->leds.valueFor(led);
689 }
690
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100691 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800692 return mExcludedDevices;
693 }
694
695 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
696 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800697 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800698 }
699
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000700 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
701 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700702 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800703 RawEvent event;
704 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000705 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800706 event.deviceId = deviceId;
707 event.type = type;
708 event.code = code;
709 event.value = value;
710 mEvents.push_back(event);
711
712 if (type == EV_ABS) {
713 setAbsoluteAxisValue(deviceId, code, value);
714 }
715 }
716
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600717 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
718 std::vector<TouchVideoFrame>> videoFrames) {
719 mVideoFrames = std::move(videoFrames);
720 }
721
Michael Wrightd02c5b62014-02-10 15:10:22 -0800722 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700723 std::unique_lock<std::mutex> lock(mLock);
724 base::ScopedLockAssertion assumeLocked(mLock);
725 const bool queueIsEmpty =
726 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
727 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
728 if (!queueIsEmpty) {
729 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
730 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800731 }
732
733private:
734 Device* getDevice(int32_t deviceId) const {
735 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100736 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800737 }
738
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700739 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800740 Device* device = getDevice(deviceId);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700741 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800742 }
743
Chris Yea52ade12020-08-27 16:49:20 -0700744 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800745 Device* device = getDevice(deviceId);
746 return device ? device->identifier : InputDeviceIdentifier();
747 }
748
Chris Yea52ade12020-08-27 16:49:20 -0700749 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800750
Chris Yea52ade12020-08-27 16:49:20 -0700751 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800752 Device* device = getDevice(deviceId);
753 if (device) {
754 *outConfiguration = device->configuration;
755 }
756 }
757
Chris Yea52ade12020-08-27 16:49:20 -0700758 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
759 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800760 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800761 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800762 ssize_t index = device->absoluteAxes.indexOfKey(axis);
763 if (index >= 0) {
764 *outAxisInfo = device->absoluteAxes.valueAt(index);
765 return OK;
766 }
767 }
768 outAxisInfo->clear();
769 return -1;
770 }
771
Chris Yea52ade12020-08-27 16:49:20 -0700772 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800773 Device* device = getDevice(deviceId);
774 if (device) {
775 return device->relativeAxes.indexOfKey(axis) >= 0;
776 }
777 return false;
778 }
779
Chris Yea52ade12020-08-27 16:49:20 -0700780 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800781
Chris Yef59a2f42020-10-16 12:55:26 -0700782 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
783 Device* device = getDevice(deviceId);
784 if (device) {
785 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
786 }
787 return false;
788 }
789
Chris Yea52ade12020-08-27 16:49:20 -0700790 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
791 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800792 Device* device = getDevice(deviceId);
793 if (device) {
794 const KeyInfo* key = getKey(device, scanCode, usageCode);
795 if (key) {
796 if (outKeycode) {
797 *outKeycode = key->keyCode;
798 }
799 if (outFlags) {
800 *outFlags = key->flags;
801 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700802 if (outMetaState) {
803 *outMetaState = metaState;
804 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800805 return OK;
806 }
807 }
808 return NAME_NOT_FOUND;
809 }
810
811 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
812 if (usageCode) {
813 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
814 if (index >= 0) {
815 return &device->keysByUsageCode.valueAt(index);
816 }
817 }
818 if (scanCode) {
819 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
820 if (index >= 0) {
821 return &device->keysByScanCode.valueAt(index);
822 }
823 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700824 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800825 }
826
Chris Yea52ade12020-08-27 16:49:20 -0700827 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800828
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000829 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
830 int32_t deviceId, int32_t absCode) const override {
Chris Yef59a2f42020-10-16 12:55:26 -0700831 Device* device = getDevice(deviceId);
832 if (!device) {
833 return Errorf("Sensor device not found.");
834 }
835 auto it = device->sensorsByAbsCode.find(absCode);
836 if (it == device->sensorsByAbsCode.end()) {
837 return Errorf("Sensor map not found.");
838 }
839 const SensorInfo& info = it->second;
840 return std::make_pair(info.sensorType, info.sensorDataIndex);
841 }
842
Chris Yea52ade12020-08-27 16:49:20 -0700843 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800844 mExcludedDevices = devices;
845 }
846
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700847 std::vector<RawEvent> getEvents(int) override {
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000848 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800849
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700850 std::vector<RawEvent> buffer;
851 std::swap(buffer, mEvents);
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000852
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700853 mEventsCondition.notify_all();
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700854 return buffer;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800855 }
856
Chris Yea52ade12020-08-27 16:49:20 -0700857 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600858 auto it = mVideoFrames.find(deviceId);
859 if (it != mVideoFrames.end()) {
860 std::vector<TouchVideoFrame> frames = std::move(it->second);
861 mVideoFrames.erase(deviceId);
862 return frames;
863 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800864 return {};
865 }
866
Chris Yea52ade12020-08-27 16:49:20 -0700867 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800868 Device* device = getDevice(deviceId);
869 if (device) {
870 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
871 if (index >= 0) {
872 return device->scanCodeStates.valueAt(index);
873 }
874 }
875 return AKEY_STATE_UNKNOWN;
876 }
877
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000878 InputDeviceCountryCode getCountryCode(int32_t deviceId) const override {
879 Device* device = getDevice(deviceId);
880 if (device) {
881 return device->countryCode;
882 }
883 return InputDeviceCountryCode::INVALID;
884 }
885
Chris Yea52ade12020-08-27 16:49:20 -0700886 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800887 Device* device = getDevice(deviceId);
888 if (device) {
889 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
890 if (index >= 0) {
891 return device->keyCodeStates.valueAt(index);
892 }
893 }
894 return AKEY_STATE_UNKNOWN;
895 }
896
Chris Yea52ade12020-08-27 16:49:20 -0700897 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800898 Device* device = getDevice(deviceId);
899 if (device) {
900 ssize_t index = device->switchStates.indexOfKey(sw);
901 if (index >= 0) {
902 return device->switchStates.valueAt(index);
903 }
904 }
905 return AKEY_STATE_UNKNOWN;
906 }
907
Chris Yea52ade12020-08-27 16:49:20 -0700908 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
909 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800910 Device* device = getDevice(deviceId);
911 if (device) {
912 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
913 if (index >= 0) {
914 *outValue = device->absoluteAxisValue.valueAt(index);
915 return OK;
916 }
917 }
918 *outValue = 0;
919 return -1;
920 }
921
Philip Junker4af3b3d2021-12-14 10:36:55 +0100922 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
923 Device* device = getDevice(deviceId);
924 if (!device) {
925 return AKEYCODE_UNKNOWN;
926 }
927 auto it = device->keyCodeMapping.find(locationKeyCode);
928 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
929 }
930
Chris Yea52ade12020-08-27 16:49:20 -0700931 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700932 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700933 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800934 bool result = false;
935 Device* device = getDevice(deviceId);
936 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700937 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700938 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800939 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
940 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
941 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800942 }
943 }
944 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
945 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
946 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800947 }
948 }
949 }
950 }
951 return result;
952 }
953
Chris Yea52ade12020-08-27 16:49:20 -0700954 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800955 Device* device = getDevice(deviceId);
956 if (device) {
957 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
958 return index >= 0;
959 }
960 return false;
961 }
962
Arthur Hungcb40a002021-08-03 14:31:01 +0000963 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
964 Device* device = getDevice(deviceId);
965 if (!device) {
966 return false;
967 }
968 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
969 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
970 return true;
971 }
972 }
973 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
974 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
975 return true;
976 }
977 }
978 return false;
979 }
980
Chris Yea52ade12020-08-27 16:49:20 -0700981 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800982 Device* device = getDevice(deviceId);
983 return device && device->leds.indexOfKey(led) >= 0;
984 }
985
Chris Yea52ade12020-08-27 16:49:20 -0700986 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800987 Device* device = getDevice(deviceId);
988 if (device) {
989 ssize_t index = device->leds.indexOfKey(led);
990 if (index >= 0) {
991 device->leds.replaceValueAt(led, on);
992 } else {
993 ADD_FAILURE()
994 << "Attempted to set the state of an LED that the EventHub declared "
995 "was not present. led=" << led;
996 }
997 }
998 }
999
Chris Yea52ade12020-08-27 16:49:20 -07001000 void getVirtualKeyDefinitions(
1001 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001002 outVirtualKeys.clear();
1003
1004 Device* device = getDevice(deviceId);
1005 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001006 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001007 }
1008 }
1009
Chris Yea52ade12020-08-27 16:49:20 -07001010 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -07001011 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001012 }
1013
Chris Yea52ade12020-08-27 16:49:20 -07001014 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001015 return false;
1016 }
1017
Chris Yea52ade12020-08-27 16:49:20 -07001018 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001019
Chris Yea52ade12020-08-27 16:49:20 -07001020 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001021
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001022 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override { return mVibrators; };
Chris Ye87143712020-11-10 05:05:58 +00001023
Chris Yee2b1e5c2021-03-10 22:45:12 -08001024 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
1025 return BATTERY_CAPACITY;
1026 }
Kim Low03ea0352020-11-06 12:45:07 -08001027
Chris Yee2b1e5c2021-03-10 22:45:12 -08001028 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
1029 return BATTERY_STATUS;
1030 }
1031
Andy Chenf9f1a022022-08-29 20:07:10 -04001032 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override {
1033 return {DEFAULT_BATTERY};
1034 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001035
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001036 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
1037 int32_t batteryId) const override {
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001038 if (batteryId != DEFAULT_BATTERY) return {};
1039 static const auto BATTERY_INFO = RawBatteryInfo{.id = DEFAULT_BATTERY,
1040 .name = "default battery",
1041 .flags = InputBatteryClass::CAPACITY,
1042 .path = BATTERY_DEVPATH};
1043 return BATTERY_INFO;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001044 }
Kim Low03ea0352020-11-06 12:45:07 -08001045
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001046 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001047 std::vector<int32_t> ids;
1048 for (const auto& [rawId, info] : mRawLightInfos) {
1049 ids.push_back(rawId);
1050 }
1051 return ids;
1052 }
1053
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001054 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001055 auto it = mRawLightInfos.find(lightId);
1056 if (it == mRawLightInfos.end()) {
1057 return std::nullopt;
1058 }
1059 return it->second;
1060 }
1061
1062 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1063 mLightBrightness.emplace(lightId, brightness);
1064 }
1065
1066 void setLightIntensities(int32_t deviceId, int32_t lightId,
1067 std::unordered_map<LightColor, int32_t> intensities) override {
1068 mLightIntensities.emplace(lightId, intensities);
1069 };
1070
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001071 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001072 auto lightIt = mLightBrightness.find(lightId);
1073 if (lightIt == mLightBrightness.end()) {
1074 return std::nullopt;
1075 }
1076 return lightIt->second;
1077 }
1078
1079 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001080 int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001081 auto lightIt = mLightIntensities.find(lightId);
1082 if (lightIt == mLightIntensities.end()) {
1083 return std::nullopt;
1084 }
1085 return lightIt->second;
1086 };
1087
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001088 void dump(std::string&) const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001089
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001090 void monitor() const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001091
Chris Yea52ade12020-08-27 16:49:20 -07001092 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001093
Chris Yea52ade12020-08-27 16:49:20 -07001094 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001095};
1096
Michael Wrightd02c5b62014-02-10 15:10:22 -08001097// --- FakeInputMapper ---
1098
1099class FakeInputMapper : public InputMapper {
1100 uint32_t mSources;
1101 int32_t mKeyboardType;
1102 int32_t mMetaState;
1103 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1104 KeyedVector<int32_t, int32_t> mScanCodeStates;
1105 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001106 // fake mapping which would normally come from keyCharacterMap
1107 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001108 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001109
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001110 std::mutex mLock;
1111 std::condition_variable mStateChangedCondition;
1112 bool mConfigureWasCalled GUARDED_BY(mLock);
1113 bool mResetWasCalled GUARDED_BY(mLock);
1114 bool mProcessWasCalled GUARDED_BY(mLock);
1115 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001116
Arthur Hungc23540e2018-11-29 20:42:11 +08001117 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001118public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001119 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1120 : InputMapper(deviceContext),
1121 mSources(sources),
1122 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001123 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001124 mConfigureWasCalled(false),
1125 mResetWasCalled(false),
1126 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001127
Chris Yea52ade12020-08-27 16:49:20 -07001128 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001129
1130 void setKeyboardType(int32_t keyboardType) {
1131 mKeyboardType = keyboardType;
1132 }
1133
1134 void setMetaState(int32_t metaState) {
1135 mMetaState = metaState;
1136 }
1137
1138 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001139 std::unique_lock<std::mutex> lock(mLock);
1140 base::ScopedLockAssertion assumeLocked(mLock);
1141 const bool configureCalled =
1142 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1143 return mConfigureWasCalled;
1144 });
1145 if (!configureCalled) {
1146 FAIL() << "Expected configure() to have been called.";
1147 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001148 mConfigureWasCalled = false;
1149 }
1150
1151 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001152 std::unique_lock<std::mutex> lock(mLock);
1153 base::ScopedLockAssertion assumeLocked(mLock);
1154 const bool resetCalled =
1155 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1156 return mResetWasCalled;
1157 });
1158 if (!resetCalled) {
1159 FAIL() << "Expected reset() to have been called.";
1160 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001161 mResetWasCalled = false;
1162 }
1163
Yi Kong9b14ac62018-07-17 13:48:38 -07001164 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001165 std::unique_lock<std::mutex> lock(mLock);
1166 base::ScopedLockAssertion assumeLocked(mLock);
1167 const bool processCalled =
1168 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1169 return mProcessWasCalled;
1170 });
1171 if (!processCalled) {
1172 FAIL() << "Expected process() to have been called.";
1173 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001174 if (outLastEvent) {
1175 *outLastEvent = mLastEvent;
1176 }
1177 mProcessWasCalled = false;
1178 }
1179
1180 void setKeyCodeState(int32_t keyCode, int32_t state) {
1181 mKeyCodeStates.replaceValueFor(keyCode, state);
1182 }
1183
1184 void setScanCodeState(int32_t scanCode, int32_t state) {
1185 mScanCodeStates.replaceValueFor(scanCode, state);
1186 }
1187
1188 void setSwitchState(int32_t switchCode, int32_t state) {
1189 mSwitchStates.replaceValueFor(switchCode, state);
1190 }
1191
1192 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001193 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001194 }
1195
Philip Junker4af3b3d2021-12-14 10:36:55 +01001196 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1197 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1198 }
1199
Michael Wrightd02c5b62014-02-10 15:10:22 -08001200private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001201 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001202
Chris Yea52ade12020-08-27 16:49:20 -07001203 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001204 InputMapper::populateDeviceInfo(deviceInfo);
1205
1206 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1207 deviceInfo->setKeyboardType(mKeyboardType);
1208 }
1209 }
1210
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001211 std::list<NotifyArgs> configure(nsecs_t, const InputReaderConfiguration* config,
1212 uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001213 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001214 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001215
1216 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001217 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001218 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1219 mViewport = config->getDisplayViewportByPort(*displayPort);
1220 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001221
1222 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001223 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001224 }
1225
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001226 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001227 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001228 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001229 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001230 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001231 }
1232
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001233 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001234 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001235 mLastEvent = *rawEvent;
1236 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001237 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001238 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001239 }
1240
Chris Yea52ade12020-08-27 16:49:20 -07001241 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001242 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1243 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1244 }
1245
Philip Junker4af3b3d2021-12-14 10:36:55 +01001246 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1247 auto it = mKeyCodeMapping.find(locationKeyCode);
1248 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1249 }
1250
Chris Yea52ade12020-08-27 16:49:20 -07001251 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001252 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1253 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1254 }
1255
Chris Yea52ade12020-08-27 16:49:20 -07001256 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001257 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1258 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1259 }
1260
Chris Yea52ade12020-08-27 16:49:20 -07001261 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001262 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -07001263 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001264 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001265 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1266 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1267 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001268 }
1269 }
1270 }
Chris Yea52ade12020-08-27 16:49:20 -07001271 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001272 return result;
1273 }
1274
1275 virtual int32_t getMetaState() {
1276 return mMetaState;
1277 }
1278
1279 virtual void fadePointer() {
1280 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001281
1282 virtual std::optional<int32_t> getAssociatedDisplay() {
1283 if (mViewport) {
1284 return std::make_optional(mViewport->displayId);
1285 }
1286 return std::nullopt;
1287 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001288};
1289
1290
1291// --- InstrumentedInputReader ---
1292
1293class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001294 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001295
1296public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001297 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1298 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001299 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001300 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001301
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001302 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001303
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001304 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001305
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001306 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001307 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001308 InputDeviceIdentifier identifier;
1309 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001310 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001311 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001312 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001313 }
1314
Prabir Pradhan28efc192019-11-05 01:10:04 +00001315 // Make the protected loopOnce method accessible to tests.
1316 using InputReader::loopOnce;
1317
Michael Wrightd02c5b62014-02-10 15:10:22 -08001318protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001319 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1320 const InputDeviceIdentifier& identifier)
1321 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001322 if (!mNextDevices.empty()) {
1323 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1324 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001325 return device;
1326 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001327 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001328 }
1329
arthurhungdcef2dc2020-08-11 14:47:50 +08001330 // --- FakeInputReaderContext ---
1331 class FakeInputReaderContext : public ContextImpl {
1332 int32_t mGlobalMetaState;
1333 bool mUpdateGlobalMetaStateWasCalled;
1334 int32_t mGeneration;
1335
1336 public:
1337 FakeInputReaderContext(InputReader* reader)
1338 : ContextImpl(reader),
1339 mGlobalMetaState(0),
1340 mUpdateGlobalMetaStateWasCalled(false),
1341 mGeneration(1) {}
1342
1343 virtual ~FakeInputReaderContext() {}
1344
1345 void assertUpdateGlobalMetaStateWasCalled() {
1346 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1347 << "Expected updateGlobalMetaState() to have been called.";
1348 mUpdateGlobalMetaStateWasCalled = false;
1349 }
1350
1351 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1352
1353 uint32_t getGeneration() { return mGeneration; }
1354
1355 void updateGlobalMetaState() override {
1356 mUpdateGlobalMetaStateWasCalled = true;
1357 ContextImpl::updateGlobalMetaState();
1358 }
1359
1360 int32_t getGlobalMetaState() override {
1361 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1362 }
1363
1364 int32_t bumpGeneration() override {
1365 mGeneration = ContextImpl::bumpGeneration();
1366 return mGeneration;
1367 }
1368 } mFakeContext;
1369
Michael Wrightd02c5b62014-02-10 15:10:22 -08001370 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001371
1372public:
1373 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001374};
1375
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001376// --- InputReaderPolicyTest ---
1377class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001378protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001379 sp<FakeInputReaderPolicy> mFakePolicy;
1380
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001381 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -07001382 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001383};
1384
1385/**
1386 * Check that empty set of viewports is an acceptable configuration.
1387 * Also try to get internal viewport two different ways - by type and by uniqueId.
1388 *
1389 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1390 * Such configuration is not currently allowed.
1391 */
1392TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001393 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001394
1395 // We didn't add any viewports yet, so there shouldn't be any.
1396 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001397 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001398 ASSERT_FALSE(internalViewport);
1399
1400 // Add an internal viewport, then clear it
1401 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001402 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001403 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001404
1405 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001406 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001407 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001408 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001409
1410 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001411 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001412 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001413 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001414
1415 mFakePolicy->clearViewports();
1416 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001417 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001418 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001419 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001420 ASSERT_FALSE(internalViewport);
1421}
1422
1423TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1424 const std::string internalUniqueId = "local:0";
1425 const std::string externalUniqueId = "local:1";
1426 const std::string virtualUniqueId1 = "virtual:2";
1427 const std::string virtualUniqueId2 = "virtual:3";
1428 constexpr int32_t virtualDisplayId1 = 2;
1429 constexpr int32_t virtualDisplayId2 = 3;
1430
1431 // Add an internal viewport
1432 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001433 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1434 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001435 // Add an external viewport
1436 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001437 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1438 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001439 // Add an virtual viewport
1440 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001441 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1442 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001443 // Add another virtual viewport
1444 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001445 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1446 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001447
1448 // Check matching by type for internal
1449 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001450 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001451 ASSERT_TRUE(internalViewport);
1452 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1453
1454 // Check matching by type for external
1455 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001456 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001457 ASSERT_TRUE(externalViewport);
1458 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1459
1460 // Check matching by uniqueId for virtual viewport #1
1461 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001462 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001463 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001464 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001465 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1466 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1467
1468 // Check matching by uniqueId for virtual viewport #2
1469 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001470 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001471 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001472 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001473 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1474 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1475}
1476
1477
1478/**
1479 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1480 * that lookup works by checking display id.
1481 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1482 */
1483TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1484 const std::string uniqueId1 = "uniqueId1";
1485 const std::string uniqueId2 = "uniqueId2";
1486 constexpr int32_t displayId1 = 2;
1487 constexpr int32_t displayId2 = 3;
1488
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001489 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1490 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001491 for (const ViewportType& type : types) {
1492 mFakePolicy->clearViewports();
1493 // Add a viewport
1494 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001495 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1496 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001497 // Add another viewport
1498 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001499 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1500 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001501
1502 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001503 std::optional<DisplayViewport> viewport1 =
1504 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001505 ASSERT_TRUE(viewport1);
1506 ASSERT_EQ(displayId1, viewport1->displayId);
1507 ASSERT_EQ(type, viewport1->type);
1508
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001509 std::optional<DisplayViewport> viewport2 =
1510 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001511 ASSERT_TRUE(viewport2);
1512 ASSERT_EQ(displayId2, viewport2->displayId);
1513 ASSERT_EQ(type, viewport2->type);
1514
1515 // When there are multiple viewports of the same kind, and uniqueId is not specified
1516 // in the call to getDisplayViewport, then that situation is not supported.
1517 // The viewports can be stored in any order, so we cannot rely on the order, since that
1518 // is just implementation detail.
1519 // However, we can check that it still returns *a* viewport, we just cannot assert
1520 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001521 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001522 ASSERT_TRUE(someViewport);
1523 }
1524}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001525
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001526/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001527 * When we have multiple internal displays make sure we always return the default display when
1528 * querying by type.
1529 */
1530TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1531 const std::string uniqueId1 = "uniqueId1";
1532 const std::string uniqueId2 = "uniqueId2";
1533 constexpr int32_t nonDefaultDisplayId = 2;
1534 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1535 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1536
1537 // Add the default display first and ensure it gets returned.
1538 mFakePolicy->clearViewports();
1539 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001540 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001541 ViewportType::INTERNAL);
1542 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001543 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001544 ViewportType::INTERNAL);
1545
1546 std::optional<DisplayViewport> viewport =
1547 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1548 ASSERT_TRUE(viewport);
1549 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1550 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1551
1552 // Add the default display second to make sure order doesn't matter.
1553 mFakePolicy->clearViewports();
1554 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001555 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001556 ViewportType::INTERNAL);
1557 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001558 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001559 ViewportType::INTERNAL);
1560
1561 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1562 ASSERT_TRUE(viewport);
1563 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1564 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1565}
1566
1567/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001568 * Check getDisplayViewportByPort
1569 */
1570TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001571 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001572 const std::string uniqueId1 = "uniqueId1";
1573 const std::string uniqueId2 = "uniqueId2";
1574 constexpr int32_t displayId1 = 1;
1575 constexpr int32_t displayId2 = 2;
1576 const uint8_t hdmi1 = 0;
1577 const uint8_t hdmi2 = 1;
1578 const uint8_t hdmi3 = 2;
1579
1580 mFakePolicy->clearViewports();
1581 // Add a viewport that's associated with some display port that's not of interest.
1582 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001583 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1584 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001585 // Add another viewport, connected to HDMI1 port
1586 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001587 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1588 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001589
1590 // Check that correct display viewport was returned by comparing the display ports.
1591 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1592 ASSERT_TRUE(hdmi1Viewport);
1593 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1594 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1595
1596 // Check that we can still get the same viewport using the uniqueId
1597 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1598 ASSERT_TRUE(hdmi1Viewport);
1599 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1600 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1601 ASSERT_EQ(type, hdmi1Viewport->type);
1602
1603 // Check that we cannot find a port with "HDMI2", because we never added one
1604 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1605 ASSERT_FALSE(hdmi2Viewport);
1606}
1607
Michael Wrightd02c5b62014-02-10 15:10:22 -08001608// --- InputReaderTest ---
1609
1610class InputReaderTest : public testing::Test {
1611protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001612 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001613 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001614 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001615 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001616
Chris Yea52ade12020-08-27 16:49:20 -07001617 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001618 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001619 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001620 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001621
Prabir Pradhan28efc192019-11-05 01:10:04 +00001622 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001623 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001624 }
1625
Chris Yea52ade12020-08-27 16:49:20 -07001626 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001627 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001628 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001629 }
1630
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001631 void addDevice(int32_t eventHubId, const std::string& name,
1632 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001633 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001634
1635 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001636 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001637 }
1638 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001639 mReader->loopOnce();
1640 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001641 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1642 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001643 }
1644
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001645 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001646 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001647 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001648 }
1649
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001650 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001651 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001652 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001653 }
1654
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001655 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001656 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001657 ftl::Flags<InputDeviceClass> classes,
1658 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001659 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001660 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1661 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001662 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001663 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001664 return mapper;
1665 }
1666};
1667
Chris Ye98d3f532020-10-01 21:48:59 -07001668TEST_F(InputReaderTest, PolicyGetInputDevices) {
1669 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001670 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001671 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001672
1673 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001674 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001675 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001676 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001677 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001678 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1679 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001680 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001681}
1682
Chris Yee7310032020-09-22 15:36:28 -07001683TEST_F(InputReaderTest, GetMergedInputDevices) {
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 ASSERT_NO_FATAL_FAILURE(
1696 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1697 ASSERT_NO_FATAL_FAILURE(
1698 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1699
1700 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001701 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001702}
1703
Chris Yee14523a2020-12-19 13:46:00 -08001704TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1705 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1706 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1707 // Add two subdevices to device
1708 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1709 // Must add at least one mapper or the device will be ignored!
1710 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1711 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1712
1713 // Push same device instance for next device to be added, so they'll have same identifier.
1714 mReader->pushNextDevice(device);
1715 mReader->pushNextDevice(device);
1716 // Sensor device is initially disabled
1717 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1718 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1719 nullptr));
1720 // Device is disabled because the only sub device is a sensor device and disabled initially.
1721 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1722 ASSERT_FALSE(device->isEnabled());
1723 ASSERT_NO_FATAL_FAILURE(
1724 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1725 // The merged device is enabled if any sub device is enabled
1726 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1727 ASSERT_TRUE(device->isEnabled());
1728}
1729
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001730TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001731 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001732 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001733 constexpr int32_t eventHubId = 1;
1734 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001735 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001736 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001737 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001738 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001739
Yi Kong9b14ac62018-07-17 13:48:38 -07001740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001741
1742 NotifyDeviceResetArgs resetArgs;
1743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001744 ASSERT_EQ(deviceId, resetArgs.deviceId);
1745
1746 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001747 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001748 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001749
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001751 ASSERT_EQ(deviceId, resetArgs.deviceId);
1752 ASSERT_EQ(device->isEnabled(), false);
1753
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001754 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001755 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001758 ASSERT_EQ(device->isEnabled(), false);
1759
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001760 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001761 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001763 ASSERT_EQ(deviceId, resetArgs.deviceId);
1764 ASSERT_EQ(device->isEnabled(), true);
1765}
1766
Michael Wrightd02c5b62014-02-10 15:10:22 -08001767TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001768 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001769 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001770 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001771 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001772 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001773 AINPUT_SOURCE_KEYBOARD, nullptr);
1774 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001775
1776 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1777 AINPUT_SOURCE_ANY, AKEYCODE_A))
1778 << "Should return unknown when the device id is >= 0 but unknown.";
1779
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001780 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1781 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1782 << "Should return unknown when the device id is valid but the sources are not "
1783 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001784
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001785 ASSERT_EQ(AKEY_STATE_DOWN,
1786 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1787 AKEYCODE_A))
1788 << "Should return value provided by mapper when device id is valid and the device "
1789 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001790
1791 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1792 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1793 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1794
1795 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1796 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1797 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1798}
1799
Philip Junker4af3b3d2021-12-14 10:36:55 +01001800TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1801 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1802 constexpr int32_t eventHubId = 1;
1803 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1804 InputDeviceClass::KEYBOARD,
1805 AINPUT_SOURCE_KEYBOARD, nullptr);
1806 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1807
1808 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1809 << "Should return unknown when the device with the specified id is not found.";
1810
1811 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1812 << "Should return correct mapping when device id is valid and mapping exists.";
1813
1814 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1815 << "Should return the location key code when device id is valid and there's no "
1816 "mapping.";
1817}
1818
1819TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1820 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1821 constexpr int32_t eventHubId = 1;
1822 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1823 InputDeviceClass::JOYSTICK,
1824 AINPUT_SOURCE_GAMEPAD, nullptr);
1825 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1826
1827 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1828 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1829}
1830
Michael Wrightd02c5b62014-02-10 15:10:22 -08001831TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001832 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001833 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001834 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001835 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001836 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001837 AINPUT_SOURCE_KEYBOARD, nullptr);
1838 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001839
1840 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1841 AINPUT_SOURCE_ANY, KEY_A))
1842 << "Should return unknown when the device id is >= 0 but unknown.";
1843
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001844 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1845 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1846 << "Should return unknown when the device id is valid but the sources are not "
1847 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001848
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001849 ASSERT_EQ(AKEY_STATE_DOWN,
1850 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1851 KEY_A))
1852 << "Should return value provided by mapper when device id is valid and the device "
1853 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001854
1855 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1856 AINPUT_SOURCE_TRACKBALL, KEY_A))
1857 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1858
1859 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1860 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1861 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1862}
1863
1864TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001865 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001866 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001867 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001868 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001869 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001870 AINPUT_SOURCE_KEYBOARD, nullptr);
1871 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001872
1873 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1874 AINPUT_SOURCE_ANY, SW_LID))
1875 << "Should return unknown when the device id is >= 0 but unknown.";
1876
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001877 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1878 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1879 << "Should return unknown when the device id is valid but the sources are not "
1880 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001881
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001882 ASSERT_EQ(AKEY_STATE_DOWN,
1883 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1884 SW_LID))
1885 << "Should return value provided by mapper when device id is valid and the device "
1886 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001887
1888 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1889 AINPUT_SOURCE_TRACKBALL, SW_LID))
1890 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1891
1892 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1893 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1894 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1895}
1896
1897TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001898 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001899 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001900 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001901 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001902 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001903 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001904
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001905 mapper.addSupportedKeyCode(AKEYCODE_A);
1906 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001907
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001908 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001909 uint8_t flags[4] = { 0, 0, 0, 1 };
1910
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001911 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08001912 << "Should return false when device id is >= 0 but unknown.";
1913 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1914
1915 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001916 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001917 << "Should return false when device id is valid but the sources are not supported by "
1918 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001919 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1920
1921 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001922 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001923 keyCodes, flags))
1924 << "Should return value provided by mapper when device id is valid and the device "
1925 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001926 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1927
1928 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001929 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1930 << "Should return false when the device id is < 0 but the sources are not supported by "
1931 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001932 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1933
1934 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001935 ASSERT_TRUE(
1936 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1937 << "Should return value provided by mapper when device id is < 0 and one of the "
1938 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001939 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1940}
1941
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001942TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001943 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001944 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001945
1946 NotifyConfigurationChangedArgs args;
1947
1948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1949 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1950}
1951
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001952TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001953 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001954 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001955 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001956 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001957 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001958 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001959 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001960 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001961
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001962 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001963 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001964 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1965
1966 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001967 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001968 ASSERT_EQ(when, event.when);
1969 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001970 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001971 ASSERT_EQ(EV_KEY, event.type);
1972 ASSERT_EQ(KEY_A, event.code);
1973 ASSERT_EQ(1, event.value);
1974}
1975
Garfield Tan1c7bc862020-01-28 13:24:04 -08001976TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001977 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001978 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001979 constexpr int32_t eventHubId = 1;
1980 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001981 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001982 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001983 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001984 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001985
1986 NotifyDeviceResetArgs resetArgs;
1987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001988 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001989
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001990 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001991 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001993 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001994 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001995
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001996 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001997 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001999 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002000 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002001
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002002 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002003 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08002005 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002006 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002007}
2008
Garfield Tan1c7bc862020-01-28 13:24:04 -08002009TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
2010 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002011 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002012 constexpr int32_t eventHubId = 1;
2013 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2014 // Must add at least one mapper or the device will be ignored!
2015 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002016 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002017 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
2018
2019 NotifyDeviceResetArgs resetArgs;
2020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2021 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
2022}
2023
Arthur Hungc23540e2018-11-29 20:42:11 +08002024TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002025 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002026 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002027 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08002028 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002029 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2030 FakeInputMapper& mapper =
2031 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002032 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08002033
2034 const uint8_t hdmi1 = 1;
2035
2036 // Associated touch screen with second display.
2037 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2038
2039 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00002040 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08002041 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002042 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002043 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002044 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002045 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002046 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002047 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002048 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002049
2050 // Add the device, and make sure all of the callbacks are triggered.
2051 // The device is added after the input port associations are processed since
2052 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002053 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002056 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002057
Arthur Hung2c9a3342019-07-23 14:18:59 +08002058 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002059 ASSERT_EQ(deviceId, device->getId());
2060 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2061 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002062
2063 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002064 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002065 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002066 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002067}
2068
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002069TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2070 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002071 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002072 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2073 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2074 // Must add at least one mapper or the device will be ignored!
2075 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2076 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2077 mReader->pushNextDevice(device);
2078 mReader->pushNextDevice(device);
2079 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2080 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2081
2082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2083
2084 NotifyDeviceResetArgs resetArgs;
2085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2086 ASSERT_EQ(deviceId, resetArgs.deviceId);
2087 ASSERT_TRUE(device->isEnabled());
2088 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2089 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2090
2091 disableDevice(deviceId);
2092 mReader->loopOnce();
2093
2094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2095 ASSERT_EQ(deviceId, resetArgs.deviceId);
2096 ASSERT_FALSE(device->isEnabled());
2097 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2098 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2099
2100 enableDevice(deviceId);
2101 mReader->loopOnce();
2102
2103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2104 ASSERT_EQ(deviceId, resetArgs.deviceId);
2105 ASSERT_TRUE(device->isEnabled());
2106 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2107 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2108}
2109
2110TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2111 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002112 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002113 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2114 // Add two subdevices to device
2115 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2116 FakeInputMapper& mapperDevice1 =
2117 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2118 FakeInputMapper& mapperDevice2 =
2119 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2120 mReader->pushNextDevice(device);
2121 mReader->pushNextDevice(device);
2122 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2123 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2124
2125 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2126 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2127
2128 ASSERT_EQ(AKEY_STATE_DOWN,
2129 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2130 ASSERT_EQ(AKEY_STATE_DOWN,
2131 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2132 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2133 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2134}
2135
Prabir Pradhan7e186182020-11-10 13:56:45 -08002136TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2137 NotifyPointerCaptureChangedArgs args;
2138
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002139 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002140 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2141 mReader->loopOnce();
2142 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002143 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2144 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002145
2146 mFakePolicy->setPointerCapture(false);
2147 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2148 mReader->loopOnce();
2149 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002150 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002151
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002152 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002153 // does not change.
2154 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2155 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002156 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002157}
2158
Chris Ye87143712020-11-10 05:05:58 +00002159class FakeVibratorInputMapper : public FakeInputMapper {
2160public:
2161 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2162 : FakeInputMapper(deviceContext, sources) {}
2163
2164 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2165};
2166
2167TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2168 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002169 ftl::Flags<InputDeviceClass> deviceClass =
2170 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002171 constexpr int32_t eventHubId = 1;
2172 const char* DEVICE_LOCATION = "BLUETOOTH";
2173 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2174 FakeVibratorInputMapper& mapper =
2175 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2176 mReader->pushNextDevice(device);
2177
2178 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2179 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2180
2181 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2182 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2183}
2184
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002185// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002186
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002187class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002188public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002189 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002190
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002191 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002192
Andy Chenf9f1a022022-08-29 20:07:10 -04002193 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
2194
Chris Yee2b1e5c2021-03-10 22:45:12 -08002195 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2196
2197 void dump(std::string& dump) override {}
2198
2199 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2200 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002201 }
2202
Chris Yee2b1e5c2021-03-10 22:45:12 -08002203 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2204 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002205 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002206
2207 bool setLightColor(int32_t lightId, int32_t color) override {
2208 getDeviceContext().setLightBrightness(lightId, color >> 24);
2209 return true;
2210 }
2211
2212 std::optional<int32_t> getLightColor(int32_t lightId) override {
2213 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2214 if (!result.has_value()) {
2215 return std::nullopt;
2216 }
2217 return result.value() << 24;
2218 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002219
2220 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2221
2222 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2223
2224private:
2225 InputDeviceContext& mDeviceContext;
2226 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2227 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04002228 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002229};
2230
Chris Yee2b1e5c2021-03-10 22:45:12 -08002231TEST_F(InputReaderTest, BatteryGetCapacity) {
2232 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002233 ftl::Flags<InputDeviceClass> deviceClass =
2234 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002235 constexpr int32_t eventHubId = 1;
2236 const char* DEVICE_LOCATION = "BLUETOOTH";
2237 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002238 FakePeripheralController& controller =
2239 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002240 mReader->pushNextDevice(device);
2241
2242 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2243
2244 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2245 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2246}
2247
2248TEST_F(InputReaderTest, BatteryGetStatus) {
2249 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002250 ftl::Flags<InputDeviceClass> deviceClass =
2251 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002252 constexpr int32_t eventHubId = 1;
2253 const char* DEVICE_LOCATION = "BLUETOOTH";
2254 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002255 FakePeripheralController& controller =
2256 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002257 mReader->pushNextDevice(device);
2258
2259 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2260
2261 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2262 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2263}
2264
Prabir Pradhane287ecd2022-09-07 21:18:05 +00002265TEST_F(InputReaderTest, BatteryGetDevicePath) {
2266 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2267 ftl::Flags<InputDeviceClass> deviceClass =
2268 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2269 constexpr int32_t eventHubId = 1;
2270 const char* DEVICE_LOCATION = "BLUETOOTH";
2271 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2272 device->addController<FakePeripheralController>(eventHubId);
2273 mReader->pushNextDevice(device);
2274
2275 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2276
2277 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), BATTERY_DEVPATH);
2278}
2279
Chris Ye3fdbfef2021-01-06 18:45:18 -08002280TEST_F(InputReaderTest, LightGetColor) {
2281 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002282 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002283 constexpr int32_t eventHubId = 1;
2284 const char* DEVICE_LOCATION = "BLUETOOTH";
2285 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002286 FakePeripheralController& controller =
2287 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002288 mReader->pushNextDevice(device);
2289 RawLightInfo info = {.id = 1,
2290 .name = "Mono",
2291 .maxBrightness = 255,
2292 .flags = InputLightClass::BRIGHTNESS,
2293 .path = ""};
2294 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2295 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2296
2297 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002298
Chris Yee2b1e5c2021-03-10 22:45:12 -08002299 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2300 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002301 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2302 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2303}
2304
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002305// --- InputReaderIntegrationTest ---
2306
2307// These tests create and interact with the InputReader only through its interface.
2308// The InputReader is started during SetUp(), which starts its processing in its own
2309// thread. The tests use linux uinput to emulate input devices.
2310// NOTE: Interacting with the physical device while these tests are running may cause
2311// the tests to fail.
2312class InputReaderIntegrationTest : public testing::Test {
2313protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002314 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002315 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002316 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002317
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002318 std::shared_ptr<FakePointerController> mFakePointerController;
2319
Chris Yea52ade12020-08-27 16:49:20 -07002320 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002321#if !defined(__ANDROID__)
2322 GTEST_SKIP();
2323#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002324 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002325 mFakePointerController = std::make_shared<FakePointerController>();
2326 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002327 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2328 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002329
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002330 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2331 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002332 ASSERT_EQ(mReader->start(), OK);
2333
2334 // Since this test is run on a real device, all the input devices connected
2335 // to the test device will show up in mReader. We wait for those input devices to
2336 // show up before beginning the tests.
2337 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2338 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2339 }
2340
Chris Yea52ade12020-08-27 16:49:20 -07002341 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002342#if !defined(__ANDROID__)
2343 return;
2344#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002345 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002346 mReader.reset();
2347 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002348 mFakePolicy.clear();
2349 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00002350
2351 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
2352 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
2353 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
2354 [&name](const InputDeviceInfo& info) {
2355 return info.getIdentifier().name == name;
2356 });
2357 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
2358 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002359};
2360
2361TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2362 // An invalid input device that is only used for this test.
2363 class InvalidUinputDevice : public UinputDevice {
2364 public:
2365 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2366
2367 private:
2368 void configureDevice(int fd, uinput_user_dev* device) override {}
2369 };
2370
2371 const size_t numDevices = mFakePolicy->getInputDevices().size();
2372
2373 // UinputDevice does not set any event or key bits, so InputReader should not
2374 // consider it as a valid device.
2375 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2376 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2377 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2378 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2379
2380 invalidDevice.reset();
2381 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2382 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2383 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2384}
2385
2386TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2387 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2388
2389 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2390 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2391 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2392 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2393
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002394 const auto device = findDeviceByName(keyboard->getName());
2395 ASSERT_TRUE(device.has_value());
2396 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
2397 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
2398 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002399
2400 keyboard.reset();
2401 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2402 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2403 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2404}
2405
2406TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2407 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2408 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2409
2410 NotifyConfigurationChangedArgs configChangedArgs;
2411 ASSERT_NO_FATAL_FAILURE(
2412 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002413 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002414 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2415
2416 NotifyKeyArgs keyArgs;
2417 keyboard->pressAndReleaseHomeKey();
2418 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2419 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002420 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002421 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002422 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002423 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002424 prevTimestamp = keyArgs.eventTime;
2425
2426 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2427 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002428 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002429 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002430 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002431}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002432
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002433TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
2434 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2435 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2436
2437 const auto device = findDeviceByName(stylus->getName());
2438 ASSERT_TRUE(device.has_value());
2439
2440 // An external stylus with buttons should be recognized as a keyboard.
2441 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
2442 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
2443 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
2444
2445 const auto DOWN =
2446 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
2447 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
2448
2449 stylus->pressAndReleaseKey(BTN_STYLUS);
2450 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2451 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2452 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2453 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2454
2455 stylus->pressAndReleaseKey(BTN_STYLUS2);
2456 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2457 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
2458 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2459 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
2460
2461 stylus->pressAndReleaseKey(BTN_STYLUS3);
2462 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2463 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
2464 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2465 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
2466}
2467
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002468/**
2469 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2470 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2471 * are passed to the listener.
2472 */
2473static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2474TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2475 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2476 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2477 NotifyKeyArgs keyArgs;
2478
2479 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2480 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2481 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2482 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2483
2484 controller->pressAndReleaseKey(BTN_GEAR_UP);
2485 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2486 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2487 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2488}
2489
Arthur Hungaab25622020-01-16 11:22:11 +08002490// --- TouchProcessTest ---
2491class TouchIntegrationTest : public InputReaderIntegrationTest {
2492protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002493 const std::string UNIQUE_ID = "local:0";
2494
Chris Yea52ade12020-08-27 16:49:20 -07002495 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002496#if !defined(__ANDROID__)
2497 GTEST_SKIP();
2498#endif
Arthur Hungaab25622020-01-16 11:22:11 +08002499 InputReaderIntegrationTest::SetUp();
2500 // At least add an internal display.
2501 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2502 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002503 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002504
2505 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2506 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2507 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00002508 const auto info = findDeviceByName(mDevice->getName());
2509 ASSERT_TRUE(info);
2510 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08002511 }
2512
2513 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2514 int32_t orientation, const std::string& uniqueId,
2515 std::optional<uint8_t> physicalPort,
2516 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002517 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2518 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002519 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2520 }
2521
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002522 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2523 NotifyMotionArgs args;
2524 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2525 EXPECT_EQ(action, args.action);
2526 ASSERT_EQ(points.size(), args.pointerCount);
2527 for (size_t i = 0; i < args.pointerCount; i++) {
2528 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2529 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2530 }
2531 }
2532
Arthur Hungaab25622020-01-16 11:22:11 +08002533 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00002534 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08002535};
2536
2537TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2538 NotifyMotionArgs args;
2539 const Point centerPoint = mDevice->getCenterPoint();
2540
2541 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002542 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002543 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002544 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002545 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2546 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2547
2548 // ACTION_MOVE
2549 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002550 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002551 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2552 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2553
2554 // ACTION_UP
2555 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002556 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002557 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2558 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2559}
2560
2561TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2562 NotifyMotionArgs args;
2563 const Point centerPoint = mDevice->getCenterPoint();
2564
2565 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002566 mDevice->sendSlot(FIRST_SLOT);
2567 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002568 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002569 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002570 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2571 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2572
2573 // ACTION_POINTER_DOWN (Second slot)
2574 const Point secondPoint = centerPoint + Point(100, 100);
2575 mDevice->sendSlot(SECOND_SLOT);
2576 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002577 mDevice->sendDown(secondPoint);
2578 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002579 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002580 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002581
2582 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002583 mDevice->sendMove(secondPoint + Point(1, 1));
2584 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002585 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2586 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2587
2588 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002589 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002590 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002591 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002592 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002593
2594 // ACTION_UP
2595 mDevice->sendSlot(FIRST_SLOT);
2596 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002597 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002598 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2599 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2600}
2601
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002602/**
2603 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2604 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2605 * data?
2606 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2607 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2608 * for Pointer 0 only is generated after.
2609 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2610 * events, we will not miss any information.
2611 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2612 * event generated afterwards that contains the newest movement of pointer 0.
2613 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2614 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2615 * losing information about non-palm pointers.
2616 */
2617TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2618 NotifyMotionArgs args;
2619 const Point centerPoint = mDevice->getCenterPoint();
2620
2621 // ACTION_DOWN
2622 mDevice->sendSlot(FIRST_SLOT);
2623 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2624 mDevice->sendDown(centerPoint);
2625 mDevice->sendSync();
2626 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2627
2628 // ACTION_POINTER_DOWN (Second slot)
2629 const Point secondPoint = centerPoint + Point(100, 100);
2630 mDevice->sendSlot(SECOND_SLOT);
2631 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2632 mDevice->sendDown(secondPoint);
2633 mDevice->sendSync();
2634 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2635
2636 // ACTION_MOVE (First slot)
2637 mDevice->sendSlot(FIRST_SLOT);
2638 mDevice->sendMove(centerPoint + Point(5, 5));
2639 // ACTION_POINTER_UP (Second slot)
2640 mDevice->sendSlot(SECOND_SLOT);
2641 mDevice->sendPointerUp();
2642 // Send a single sync for the above 2 pointer updates
2643 mDevice->sendSync();
2644
2645 // First, we should get POINTER_UP for the second pointer
2646 assertReceivedMotion(ACTION_POINTER_1_UP,
2647 {/*first pointer */ centerPoint + Point(5, 5),
2648 /*second pointer*/ secondPoint});
2649
2650 // Next, the MOVE event for the first pointer
2651 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2652}
2653
2654/**
2655 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2656 * move, and then it will go up, all in the same frame.
2657 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2658 * gets sent to the listener.
2659 */
2660TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2661 NotifyMotionArgs args;
2662 const Point centerPoint = mDevice->getCenterPoint();
2663
2664 // ACTION_DOWN
2665 mDevice->sendSlot(FIRST_SLOT);
2666 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2667 mDevice->sendDown(centerPoint);
2668 mDevice->sendSync();
2669 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2670
2671 // ACTION_POINTER_DOWN (Second slot)
2672 const Point secondPoint = centerPoint + Point(100, 100);
2673 mDevice->sendSlot(SECOND_SLOT);
2674 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2675 mDevice->sendDown(secondPoint);
2676 mDevice->sendSync();
2677 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2678
2679 // ACTION_MOVE (First slot)
2680 mDevice->sendSlot(FIRST_SLOT);
2681 mDevice->sendMove(centerPoint + Point(5, 5));
2682 // ACTION_POINTER_UP (Second slot)
2683 mDevice->sendSlot(SECOND_SLOT);
2684 mDevice->sendMove(secondPoint + Point(6, 6));
2685 mDevice->sendPointerUp();
2686 // Send a single sync for the above 2 pointer updates
2687 mDevice->sendSync();
2688
2689 // First, we should get POINTER_UP for the second pointer
2690 // The movement of the second pointer during the liftoff frame is ignored.
2691 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2692 assertReceivedMotion(ACTION_POINTER_1_UP,
2693 {/*first pointer */ centerPoint + Point(5, 5),
2694 /*second pointer*/ secondPoint});
2695
2696 // Next, the MOVE event for the first pointer
2697 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2698}
2699
Arthur Hungaab25622020-01-16 11:22:11 +08002700TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2701 NotifyMotionArgs args;
2702 const Point centerPoint = mDevice->getCenterPoint();
2703
2704 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002705 mDevice->sendSlot(FIRST_SLOT);
2706 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002707 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002708 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002709 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2710 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2711
arthurhungcc7f9802020-04-30 17:55:40 +08002712 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002713 const Point secondPoint = centerPoint + Point(100, 100);
2714 mDevice->sendSlot(SECOND_SLOT);
2715 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2716 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002717 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002718 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002719 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002720
arthurhungcc7f9802020-04-30 17:55:40 +08002721 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002722 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002723 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002724 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2725 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2726
arthurhungcc7f9802020-04-30 17:55:40 +08002727 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2728 // a palm event.
2729 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002730 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002731 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002732 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002733 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002734 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002735
arthurhungcc7f9802020-04-30 17:55:40 +08002736 // Send up to second slot, expect first slot send moving.
2737 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002738 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002739 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2740 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002741
arthurhungcc7f9802020-04-30 17:55:40 +08002742 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002743 mDevice->sendSlot(FIRST_SLOT);
2744 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002745 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002746
arthurhungcc7f9802020-04-30 17:55:40 +08002747 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2748 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002749}
2750
Prabir Pradhanda20b172022-09-26 17:01:18 +00002751TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
2752 const Point centerPoint = mDevice->getCenterPoint();
2753
2754 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
2755 mDevice->sendSlot(FIRST_SLOT);
2756 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2757 mDevice->sendToolType(MT_TOOL_PEN);
2758 mDevice->sendDown(centerPoint);
2759 mDevice->sendSync();
2760 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2761 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2762 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
2763
2764 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2765
2766 // Release the stylus touch.
2767 mDevice->sendUp();
2768 mDevice->sendSync();
2769 ASSERT_NO_FATAL_FAILURE(
2770 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2771
2772 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2773
2774 // Touch down with the finger, without the pen tool selected. The policy is not notified.
2775 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2776 mDevice->sendToolType(MT_TOOL_FINGER);
2777 mDevice->sendDown(centerPoint);
2778 mDevice->sendSync();
2779 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2780 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2781 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
2782
2783 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2784
2785 mDevice->sendUp();
2786 mDevice->sendSync();
2787 ASSERT_NO_FATAL_FAILURE(
2788 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2789
2790 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
2791 // The policy should be notified of the stylus presence.
2792 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2793 mDevice->sendToolType(MT_TOOL_PEN);
2794 mDevice->sendMove(centerPoint);
2795 mDevice->sendSync();
2796 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2797 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2798 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
2799
2800 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2801}
2802
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002803TEST_F(TouchIntegrationTest, StylusButtonsGenerateKeyEvents) {
2804 mDevice->sendKey(BTN_STYLUS, 1);
2805 mDevice->sendSync();
2806 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2807 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2808 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2809
2810 mDevice->sendKey(BTN_STYLUS, 0);
2811 mDevice->sendSync();
2812 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2813 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2814 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2815}
2816
Michael Wrightd02c5b62014-02-10 15:10:22 -08002817// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002818class InputDeviceTest : public testing::Test {
2819protected:
2820 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002821 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002822 static const int32_t DEVICE_ID;
2823 static const int32_t DEVICE_GENERATION;
2824 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002825 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002826 static const int32_t EVENTHUB_ID;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002827 static const std::string DEVICE_BLUETOOTH_ADDRESS;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002828
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002829 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002830 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002831 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002832 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002833 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002834
Chris Yea52ade12020-08-27 16:49:20 -07002835 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002836 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002837 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002838 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002839 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002840 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002841 InputDeviceIdentifier identifier;
2842 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002843 identifier.location = DEVICE_LOCATION;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002844 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
arthurhungdcef2dc2020-08-11 14:47:50 +08002845 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002846 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002847 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002848 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002849 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002850 }
2851
Chris Yea52ade12020-08-27 16:49:20 -07002852 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002853 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002854 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002855 }
2856};
2857
2858const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002859const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002860const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002861const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2862const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002863const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002864 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002865const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002866const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002867
2868TEST_F(InputDeviceTest, ImmutableProperties) {
2869 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002870 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002871 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002872}
2873
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002874TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
2875 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
2876
2877 // Configuration
2878 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2879 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002880 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002881
2882 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
2883}
2884
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002885TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2886 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002887}
2888
Michael Wrightd02c5b62014-02-10 15:10:22 -08002889TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2890 // Configuration.
2891 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002892 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002893
2894 // Reset.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002895 unused += mDevice->reset(ARBITRARY_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002896
2897 NotifyDeviceResetArgs resetArgs;
2898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2899 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2900 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2901
2902 // Metadata.
2903 ASSERT_TRUE(mDevice->isIgnored());
2904 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2905
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002906 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002907 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002908 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002909 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2910 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2911
2912 // State queries.
2913 ASSERT_EQ(0, mDevice->getMetaState());
2914
2915 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2916 << "Ignored device should return unknown key code state.";
2917 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2918 << "Ignored device should return unknown scan code state.";
2919 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2920 << "Ignored device should return unknown switch state.";
2921
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002922 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002923 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002924 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002925 << "Ignored device should never mark any key codes.";
2926 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2927 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2928}
2929
2930TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2931 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002932 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08002933
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002934 FakeInputMapper& mapper1 =
2935 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002936 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2937 mapper1.setMetaState(AMETA_ALT_ON);
2938 mapper1.addSupportedKeyCode(AKEYCODE_A);
2939 mapper1.addSupportedKeyCode(AKEYCODE_B);
2940 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2941 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2942 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2943 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2944 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002945
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002946 FakeInputMapper& mapper2 =
2947 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002948 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002949
2950 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002951 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002952
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002953 std::string propertyValue;
2954 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002955 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002956 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002957
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002958 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2959 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002960
2961 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002962 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002963 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2964 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002965
2966 NotifyDeviceResetArgs resetArgs;
2967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2968 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2969 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2970
2971 // Metadata.
2972 ASSERT_FALSE(mDevice->isIgnored());
2973 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2974
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002975 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002976 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002977 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002978 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2979 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2980
2981 // State queries.
2982 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2983 << "Should query mappers and combine meta states.";
2984
2985 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2986 << "Should return unknown key code state when source not supported.";
2987 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2988 << "Should return unknown scan code state when source not supported.";
2989 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2990 << "Should return unknown switch state when source not supported.";
2991
2992 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2993 << "Should query mapper when source is supported.";
2994 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2995 << "Should query mapper when source is supported.";
2996 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2997 << "Should query mapper when source is supported.";
2998
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002999 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08003000 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003001 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003002 << "Should do nothing when source is unsupported.";
3003 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
3004 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
3005 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
3006 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
3007
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003008 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003009 << "Should query mapper when source is supported.";
3010 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
3011 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
3012 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
3013 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
3014
3015 // Event handling.
3016 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003017 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003018 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003019
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003020 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
3021 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003022}
3023
Arthur Hung2c9a3342019-07-23 14:18:59 +08003024// A single input device is associated with a specific display. Check that:
3025// 1. Device is disabled if the viewport corresponding to the associated display is not found
3026// 2. Device is disabled when setEnabled API is called
3027TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003028 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003029
3030 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003031 std::list<NotifyArgs> unused =
3032 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003033
3034 // Device should be enabled by default.
3035 ASSERT_TRUE(mDevice->isEnabled());
3036
3037 // Prepare associated info.
3038 constexpr uint8_t hdmi = 1;
3039 const std::string UNIQUE_ID = "local:1";
3040
3041 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003042 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3043 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003044 // Device should be disabled because it is associated with a specific display via
3045 // input port <-> display port association, but the corresponding display is not found
3046 ASSERT_FALSE(mDevice->isEnabled());
3047
3048 // Prepare displays.
3049 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003050 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
3051 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003052 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3053 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003054 ASSERT_TRUE(mDevice->isEnabled());
3055
3056 // Device should be disabled after set disable.
3057 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003058 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3059 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003060 ASSERT_FALSE(mDevice->isEnabled());
3061
3062 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003063 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3064 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003065 ASSERT_FALSE(mDevice->isEnabled());
3066}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003067
Christine Franks1ba71cc2021-04-07 14:37:42 -07003068TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
3069 // Device should be enabled by default.
3070 mFakePolicy->clearViewports();
3071 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003072 std::list<NotifyArgs> unused =
3073 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003074 ASSERT_TRUE(mDevice->isEnabled());
3075
3076 // Device should be disabled because it is associated with a specific display, but the
3077 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08003078 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003079 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3080 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003081 ASSERT_FALSE(mDevice->isEnabled());
3082
3083 // Device should be enabled when a display is found.
3084 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3085 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
3086 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003087 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3088 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003089 ASSERT_TRUE(mDevice->isEnabled());
3090
3091 // Device should be disabled after set disable.
3092 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003093 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3094 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003095 ASSERT_FALSE(mDevice->isEnabled());
3096
3097 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003098 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3099 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003100 ASSERT_FALSE(mDevice->isEnabled());
3101}
3102
Christine Franks2a2293c2022-01-18 11:51:16 -08003103TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
3104 mFakePolicy->clearViewports();
3105 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003106 std::list<NotifyArgs> unused =
3107 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks2a2293c2022-01-18 11:51:16 -08003108
Christine Franks2a2293c2022-01-18 11:51:16 -08003109 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
3110 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3111 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
3112 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003113 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3114 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08003115 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
3116}
3117
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07003118/**
3119 * This test reproduces a crash caused by a dangling reference that remains after device is added
3120 * and removed. The reference is accessed in InputDevice::dump(..);
3121 */
3122TEST_F(InputDeviceTest, DumpDoesNotCrash) {
3123 constexpr int32_t TEST_EVENTHUB_ID = 10;
3124 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
3125
3126 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
3127 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
3128 device.removeEventHubDevice(TEST_EVENTHUB_ID);
3129 std::string dumpStr, eventHubDevStr;
3130 device.dump(dumpStr, eventHubDevStr);
3131}
3132
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003133TEST_F(InputDeviceTest, GetBluetoothAddress) {
3134 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
3135 ASSERT_TRUE(address);
3136 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
3137}
3138
Michael Wrightd02c5b62014-02-10 15:10:22 -08003139// --- InputMapperTest ---
3140
3141class InputMapperTest : public testing::Test {
3142protected:
3143 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003144 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003145 static const int32_t DEVICE_ID;
3146 static const int32_t DEVICE_GENERATION;
3147 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003148 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003149 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003150
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003151 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003152 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003153 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08003154 std::unique_ptr<InstrumentedInputReader> mReader;
3155 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003156
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003157 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003158 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003159 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003160 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08003161 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003162 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08003163 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003164 // Consume the device reset notification generated when adding a new device.
3165 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003166 }
3167
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003168 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003169 SetUp(DEVICE_CLASSES);
3170 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003171
Chris Yea52ade12020-08-27 16:49:20 -07003172 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003173 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003174 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003175 }
3176
3177 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003178 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003179 }
3180
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003181 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00003182 if (!changes ||
3183 (changes &
3184 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
3185 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003186 mReader->requestRefreshConfiguration(changes);
3187 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08003188 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003189 std::list<NotifyArgs> out =
3190 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003191 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003192 for (const NotifyArgs& args : out) {
3193 mFakeListener->notify(args);
3194 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003195 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003196 return out;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003197 }
3198
arthurhungdcef2dc2020-08-11 14:47:50 +08003199 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
3200 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003201 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003202 InputDeviceIdentifier identifier;
3203 identifier.name = name;
3204 identifier.location = location;
3205 std::shared_ptr<InputDevice> device =
3206 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
3207 identifier);
3208 mReader->pushNextDevice(device);
3209 mFakeEventHub->addDevice(eventHubId, name, classes);
3210 mReader->loopOnce();
3211 return device;
3212 }
3213
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003214 template <class T, typename... Args>
3215 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003216 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003217 configureDevice(0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003218 std::list<NotifyArgs> resetArgList = mDevice->reset(ARBITRARY_TIME);
3219 resetArgList += mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003220 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003221 for (const NotifyArgs& loopArgs : resetArgList) {
3222 mFakeListener->notify(loopArgs);
3223 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003224 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003225 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003226 }
3227
3228 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003229 int32_t orientation, const std::string& uniqueId,
3230 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003231 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3232 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003233 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3234 }
3235
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003236 void clearViewports() {
3237 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003238 }
3239
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003240 std::list<NotifyArgs> process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type,
3241 int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003242 RawEvent event;
3243 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003244 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003245 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003246 event.type = type;
3247 event.code = code;
3248 event.value = value;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003249 std::list<NotifyArgs> processArgList = mapper.process(&event);
3250 for (const NotifyArgs& args : processArgList) {
3251 mFakeListener->notify(args);
3252 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003253 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003254 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003255 return processArgList;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003256 }
3257
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00003258 void resetMapper(InputMapper& mapper, nsecs_t when) {
3259 const auto resetArgs = mapper.reset(when);
3260 for (const auto args : resetArgs) {
3261 mFakeListener->notify(args);
3262 }
3263 // Loop the reader to flush the input listener queue.
3264 mReader->loopOnce();
3265 }
3266
Michael Wrightd02c5b62014-02-10 15:10:22 -08003267 static void assertMotionRange(const InputDeviceInfo& info,
3268 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3269 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003270 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003271 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3272 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3273 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3274 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3275 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3276 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3277 }
3278
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003279 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3280 float size, float touchMajor, float touchMinor, float toolMajor,
3281 float toolMinor, float orientation, float distance,
3282 float scaledAxisEpsilon = 1.f) {
3283 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3284 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3286 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003287 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3288 scaledAxisEpsilon);
3289 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3290 scaledAxisEpsilon);
3291 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3292 scaledAxisEpsilon);
3293 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3294 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003295 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3296 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3297 }
3298
Michael Wright17db18e2020-06-26 20:51:44 +01003299 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003300 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003301 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003302 ASSERT_NEAR(x, actualX, 1);
3303 ASSERT_NEAR(y, actualY, 1);
3304 }
3305};
3306
3307const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003308const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003309const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003310const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3311const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003312const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3313 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003314const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003315
3316// --- SwitchInputMapperTest ---
3317
3318class SwitchInputMapperTest : public InputMapperTest {
3319protected:
3320};
3321
3322TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003323 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003324
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003325 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003326}
3327
3328TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003329 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003330
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003331 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003332 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003333
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003334 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003335 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003336}
3337
3338TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003339 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003340 std::list<NotifyArgs> out;
3341 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3342 ASSERT_TRUE(out.empty());
3343 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3344 ASSERT_TRUE(out.empty());
3345 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3346 ASSERT_TRUE(out.empty());
3347 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003348
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003349 ASSERT_EQ(1u, out.size());
3350 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003351 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003352 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3353 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003354 args.switchMask);
3355 ASSERT_EQ(uint32_t(0), args.policyFlags);
3356}
3357
Chris Ye87143712020-11-10 05:05:58 +00003358// --- VibratorInputMapperTest ---
3359class VibratorInputMapperTest : public InputMapperTest {
3360protected:
3361 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3362};
3363
3364TEST_F(VibratorInputMapperTest, GetSources) {
3365 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3366
3367 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3368}
3369
3370TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3371 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3372
3373 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3374}
3375
3376TEST_F(VibratorInputMapperTest, Vibrate) {
3377 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003378 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003379 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3380
3381 VibrationElement pattern(2);
3382 VibrationSequence sequence(2);
3383 pattern.duration = std::chrono::milliseconds(200);
3384 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3385 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3386 sequence.addElement(pattern);
3387 pattern.duration = std::chrono::milliseconds(500);
3388 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3389 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3390 sequence.addElement(pattern);
3391
3392 std::vector<int64_t> timings = {0, 1};
3393 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3394
3395 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003396 // Start vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003397 std::list<NotifyArgs> out = mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003398 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003399 // Verify vibrator state listener was notified.
3400 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003401 ASSERT_EQ(1u, out.size());
3402 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3403 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
3404 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08003405 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003406 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08003407 ASSERT_FALSE(mapper.isVibrating());
3408 // Verify vibrator state listener was notified.
3409 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003410 ASSERT_EQ(1u, out.size());
3411 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3412 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
3413 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003414}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003415
Chris Yef59a2f42020-10-16 12:55:26 -07003416// --- SensorInputMapperTest ---
3417
3418class SensorInputMapperTest : public InputMapperTest {
3419protected:
3420 static const int32_t ACCEL_RAW_MIN;
3421 static const int32_t ACCEL_RAW_MAX;
3422 static const int32_t ACCEL_RAW_FUZZ;
3423 static const int32_t ACCEL_RAW_FLAT;
3424 static const int32_t ACCEL_RAW_RESOLUTION;
3425
3426 static const int32_t GYRO_RAW_MIN;
3427 static const int32_t GYRO_RAW_MAX;
3428 static const int32_t GYRO_RAW_FUZZ;
3429 static const int32_t GYRO_RAW_FLAT;
3430 static const int32_t GYRO_RAW_RESOLUTION;
3431
3432 static const float GRAVITY_MS2_UNIT;
3433 static const float DEGREE_RADIAN_UNIT;
3434
3435 void prepareAccelAxes();
3436 void prepareGyroAxes();
3437 void setAccelProperties();
3438 void setGyroProperties();
3439 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3440};
3441
3442const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3443const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3444const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3445const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3446const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3447
3448const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3449const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3450const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3451const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3452const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3453
3454const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3455const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3456
3457void SensorInputMapperTest::prepareAccelAxes() {
3458 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3459 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3460 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3461 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3462 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3463 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3464}
3465
3466void SensorInputMapperTest::prepareGyroAxes() {
3467 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3468 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3469 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3470 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3471 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3472 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3473}
3474
3475void SensorInputMapperTest::setAccelProperties() {
3476 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3477 /* sensorDataIndex */ 0);
3478 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3479 /* sensorDataIndex */ 1);
3480 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3481 /* sensorDataIndex */ 2);
3482 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3483 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3484 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3485 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3486 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3487}
3488
3489void SensorInputMapperTest::setGyroProperties() {
3490 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3491 /* sensorDataIndex */ 0);
3492 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3493 /* sensorDataIndex */ 1);
3494 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3495 /* sensorDataIndex */ 2);
3496 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3497 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3498 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3499 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3500 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3501}
3502
3503TEST_F(SensorInputMapperTest, GetSources) {
3504 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3505
3506 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3507}
3508
3509TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3510 setAccelProperties();
3511 prepareAccelAxes();
3512 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3513
3514 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3515 std::chrono::microseconds(10000),
3516 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003517 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3520 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3521 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3522 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003523
3524 NotifySensorArgs args;
3525 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3526 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3527 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3528
3529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3530 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3531 ASSERT_EQ(args.deviceId, DEVICE_ID);
3532 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3533 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3534 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3535 ASSERT_EQ(args.values, values);
3536 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3537}
3538
3539TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3540 setGyroProperties();
3541 prepareGyroAxes();
3542 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3543
3544 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3545 std::chrono::microseconds(10000),
3546 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003547 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3549 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3550 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3551 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3552 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003553
3554 NotifySensorArgs args;
3555 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3556 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3557 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3558
3559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3560 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3561 ASSERT_EQ(args.deviceId, DEVICE_ID);
3562 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3563 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3564 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3565 ASSERT_EQ(args.values, values);
3566 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3567}
3568
Michael Wrightd02c5b62014-02-10 15:10:22 -08003569// --- KeyboardInputMapperTest ---
3570
3571class KeyboardInputMapperTest : public InputMapperTest {
3572protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003573 const std::string UNIQUE_ID = "local:0";
3574
3575 void prepareDisplay(int32_t orientation);
3576
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003577 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003578 int32_t originalKeyCode, int32_t rotatedKeyCode,
3579 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003580};
3581
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003582/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3583 * orientation.
3584 */
3585void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003586 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3587 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003588}
3589
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003590void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003591 int32_t originalScanCode, int32_t originalKeyCode,
3592 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003593 NotifyKeyArgs args;
3594
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003595 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3597 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3598 ASSERT_EQ(originalScanCode, args.scanCode);
3599 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003600 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003601
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003602 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3604 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3605 ASSERT_EQ(originalScanCode, args.scanCode);
3606 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003607 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003608}
3609
Michael Wrightd02c5b62014-02-10 15:10:22 -08003610TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003611 KeyboardInputMapper& mapper =
3612 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3613 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003614
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003615 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003616}
3617
3618TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3619 const int32_t USAGE_A = 0x070004;
3620 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003621 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3622 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003623 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3624 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3625 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003626
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003627 KeyboardInputMapper& mapper =
3628 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3629 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003630 // Initial metastate is AMETA_NONE.
3631 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003632
3633 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003634 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003635 NotifyKeyArgs args;
3636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3637 ASSERT_EQ(DEVICE_ID, args.deviceId);
3638 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3639 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3640 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3641 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3642 ASSERT_EQ(KEY_HOME, args.scanCode);
3643 ASSERT_EQ(AMETA_NONE, args.metaState);
3644 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3645 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3646 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3647
3648 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003649 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3651 ASSERT_EQ(DEVICE_ID, args.deviceId);
3652 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3653 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3654 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3655 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3656 ASSERT_EQ(KEY_HOME, args.scanCode);
3657 ASSERT_EQ(AMETA_NONE, args.metaState);
3658 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3659 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3660 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3661
3662 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003663 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3664 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3666 ASSERT_EQ(DEVICE_ID, args.deviceId);
3667 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3668 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3669 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3670 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3671 ASSERT_EQ(0, args.scanCode);
3672 ASSERT_EQ(AMETA_NONE, args.metaState);
3673 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3674 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3675 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3676
3677 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003678 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3679 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3681 ASSERT_EQ(DEVICE_ID, args.deviceId);
3682 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3683 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3684 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3685 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3686 ASSERT_EQ(0, args.scanCode);
3687 ASSERT_EQ(AMETA_NONE, args.metaState);
3688 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3689 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3690 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3691
3692 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003693 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3694 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3696 ASSERT_EQ(DEVICE_ID, args.deviceId);
3697 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3698 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3699 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3700 ASSERT_EQ(0, args.keyCode);
3701 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3702 ASSERT_EQ(AMETA_NONE, args.metaState);
3703 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3704 ASSERT_EQ(0U, args.policyFlags);
3705 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3706
3707 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003708 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3709 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3711 ASSERT_EQ(DEVICE_ID, args.deviceId);
3712 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3713 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3714 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3715 ASSERT_EQ(0, args.keyCode);
3716 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3717 ASSERT_EQ(AMETA_NONE, args.metaState);
3718 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3719 ASSERT_EQ(0U, args.policyFlags);
3720 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3721}
3722
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003723/**
3724 * Ensure that the readTime is set to the time when the EV_KEY is received.
3725 */
3726TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3727 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3728
3729 KeyboardInputMapper& mapper =
3730 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3731 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3732 NotifyKeyArgs args;
3733
3734 // Key down
3735 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3737 ASSERT_EQ(12, args.readTime);
3738
3739 // Key up
3740 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3742 ASSERT_EQ(15, args.readTime);
3743}
3744
Michael Wrightd02c5b62014-02-10 15:10:22 -08003745TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003746 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3747 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003748 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3749 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3750 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003751
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
Arthur Hung95f68612022-04-07 14:08:22 +08003756 // Initial metastate is AMETA_NONE.
3757 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003758
3759 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003760 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003761 NotifyKeyArgs args;
3762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3763 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003764 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003765 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003766
3767 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003768 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3770 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003771 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003772
3773 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003774 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3776 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003777 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003778
3779 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003780 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3782 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003783 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003784 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003785}
3786
3787TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003788 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3789 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3790 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3791 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003792
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003793 KeyboardInputMapper& mapper =
3794 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3795 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003796
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003797 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003798 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3799 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3800 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3801 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3802 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3803 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3804 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3805 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3806}
3807
3808TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003809 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3810 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3811 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3812 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003813
Michael Wrightd02c5b62014-02-10 15:10:22 -08003814 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003815 KeyboardInputMapper& mapper =
3816 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3817 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003818
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003819 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003820 ASSERT_NO_FATAL_FAILURE(
3821 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3822 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3823 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3824 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3825 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3826 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3827 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003828
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003829 clearViewports();
3830 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003831 ASSERT_NO_FATAL_FAILURE(
3832 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3833 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3834 AKEYCODE_DPAD_UP, DISPLAY_ID));
3835 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3836 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3837 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3838 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003839
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003840 clearViewports();
3841 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003842 ASSERT_NO_FATAL_FAILURE(
3843 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3844 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3845 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3846 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3847 AKEYCODE_DPAD_UP, DISPLAY_ID));
3848 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3849 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003850
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003851 clearViewports();
3852 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003853 ASSERT_NO_FATAL_FAILURE(
3854 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3855 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3856 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3857 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3858 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3859 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3860 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003861
3862 // Special case: if orientation changes while key is down, we still emit the same keycode
3863 // in the key up as we did in the key down.
3864 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003865 clearViewports();
3866 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003867 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3869 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3870 ASSERT_EQ(KEY_UP, args.scanCode);
3871 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3872
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003873 clearViewports();
3874 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003875 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3877 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3878 ASSERT_EQ(KEY_UP, args.scanCode);
3879 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3880}
3881
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003882TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3883 // If the keyboard is not orientation aware,
3884 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003885 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003886
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003887 KeyboardInputMapper& mapper =
3888 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3889 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003890 NotifyKeyArgs args;
3891
3892 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003893 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003895 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3897 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3898
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003899 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003900 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003902 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3904 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3905}
3906
3907TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3908 // If the keyboard is orientation aware,
3909 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003910 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003911
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003912 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003913 KeyboardInputMapper& mapper =
3914 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3915 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003916 NotifyKeyArgs args;
3917
3918 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3919 // ^--- already checked by the previous test
3920
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003921 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003922 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003923 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003925 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3927 ASSERT_EQ(DISPLAY_ID, args.displayId);
3928
3929 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003930 clearViewports();
3931 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003932 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003933 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003935 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3937 ASSERT_EQ(newDisplayId, args.displayId);
3938}
3939
Michael Wrightd02c5b62014-02-10 15:10:22 -08003940TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003941 KeyboardInputMapper& mapper =
3942 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3943 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003944
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003945 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003946 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003947
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003948 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003949 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003950}
3951
Philip Junker4af3b3d2021-12-14 10:36:55 +01003952TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3953 KeyboardInputMapper& mapper =
3954 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3955 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3956
3957 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3958 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3959 << "If a mapping is available, the result is equal to the mapping";
3960
3961 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3962 << "If no mapping is available, the result is the key location";
3963}
3964
Michael Wrightd02c5b62014-02-10 15:10:22 -08003965TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003966 KeyboardInputMapper& mapper =
3967 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3968 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003969
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003970 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003971 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003972
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003973 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003974 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003975}
3976
3977TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003978 KeyboardInputMapper& mapper =
3979 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3980 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003981
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003982 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003983
Michael Wrightd02c5b62014-02-10 15:10:22 -08003984 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003985 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003986 ASSERT_TRUE(flags[0]);
3987 ASSERT_FALSE(flags[1]);
3988}
3989
3990TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003991 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3992 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3993 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3994 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3995 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3996 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003997
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003998 KeyboardInputMapper& mapper =
3999 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4000 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004001 // Initial metastate is AMETA_NONE.
4002 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004003
4004 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004005 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4006 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4007 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004008
4009 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004010 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4011 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004012 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4013 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4014 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004015 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004016
4017 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004018 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4019 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004020 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4021 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4022 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004023 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004024
4025 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004026 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4027 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004028 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4029 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4030 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004031 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004032
4033 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004034 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4035 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004036 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4037 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4038 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004039 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004040
4041 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004042 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4043 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004044 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4045 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4046 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004047 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004048
4049 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004050 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4051 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004052 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4053 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4054 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004055 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004056}
4057
Chris Yea52ade12020-08-27 16:49:20 -07004058TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
4059 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
4060 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
4061 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
4062 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
4063
4064 KeyboardInputMapper& mapper =
4065 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4066 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4067
Chris Yea52ade12020-08-27 16:49:20 -07004068 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004069 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07004070 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4071 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
4072 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
4073 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4074
4075 NotifyKeyArgs args;
4076 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004077 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07004078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4079 ASSERT_EQ(AMETA_NONE, args.metaState);
4080 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4081 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4082 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
4083
4084 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004085 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07004086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4087 ASSERT_EQ(AMETA_NONE, args.metaState);
4088 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4089 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4090 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
4091}
4092
Arthur Hung2c9a3342019-07-23 14:18:59 +08004093TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
4094 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004095 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4096 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4097 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4098 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004099
4100 // keyboard 2.
4101 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08004102 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08004103 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004104 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08004105 std::shared_ptr<InputDevice> device2 =
4106 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004107 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08004108
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004109 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4110 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4111 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4112 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004113
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004114 KeyboardInputMapper& mapper =
4115 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4116 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004117
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004118 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004119 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004120 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004121 std::list<NotifyArgs> unused =
4122 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4123 0 /*changes*/);
4124 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004125
4126 // Prepared displays and associated info.
4127 constexpr uint8_t hdmi1 = 0;
4128 constexpr uint8_t hdmi2 = 1;
4129 const std::string SECONDARY_UNIQUE_ID = "local:1";
4130
4131 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
4132 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
4133
4134 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004135 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4136 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004137 ASSERT_FALSE(device2->isEnabled());
4138
4139 // Prepare second display.
4140 constexpr int32_t newDisplayId = 2;
4141 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004142 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004143 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004144 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004145 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004146 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4147 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004148
4149 // Device should be enabled after the associated display is found.
4150 ASSERT_TRUE(mDevice->isEnabled());
4151 ASSERT_TRUE(device2->isEnabled());
4152
4153 // Test pad key events
4154 ASSERT_NO_FATAL_FAILURE(
4155 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
4156 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4157 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4158 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4159 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4160 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4161 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4162
4163 ASSERT_NO_FATAL_FAILURE(
4164 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
4165 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4166 AKEYCODE_DPAD_RIGHT, newDisplayId));
4167 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4168 AKEYCODE_DPAD_DOWN, newDisplayId));
4169 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4170 AKEYCODE_DPAD_LEFT, newDisplayId));
4171}
Michael Wrightd02c5b62014-02-10 15:10:22 -08004172
arthurhungc903df12020-08-11 15:08:42 +08004173TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
4174 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4175 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4176 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4177 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4178 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4179 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4180
4181 KeyboardInputMapper& mapper =
4182 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4183 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004184 // Initial metastate is AMETA_NONE.
4185 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004186
4187 // Initialization should have turned all of the lights off.
4188 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4189 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4190 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4191
4192 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004193 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4194 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004195 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4196 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4197
4198 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004199 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4200 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004201 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4202 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
4203
4204 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004205 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4206 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004207 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4208 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
4209
4210 mFakeEventHub->removeDevice(EVENTHUB_ID);
4211 mReader->loopOnce();
4212
4213 // keyboard 2 should default toggle keys.
4214 const std::string USB2 = "USB2";
4215 const std::string DEVICE_NAME2 = "KEYBOARD2";
4216 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4217 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4218 std::shared_ptr<InputDevice> device2 =
4219 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004220 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08004221 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4222 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4223 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4224 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4225 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4226 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4227
arthurhung6fe95782020-10-05 22:41:16 +08004228 KeyboardInputMapper& mapper2 =
4229 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4230 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004231 std::list<NotifyArgs> unused =
4232 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4233 0 /*changes*/);
4234 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08004235
4236 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4237 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4238 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004239 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4240 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004241}
4242
Arthur Hungcb40a002021-08-03 14:31:01 +00004243TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4244 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4245 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4246 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4247
4248 // Suppose we have two mappers. (DPAD + KEYBOARD)
4249 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4250 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4251 KeyboardInputMapper& mapper =
4252 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4253 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004254 // Initial metastate is AMETA_NONE.
4255 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004256
4257 mReader->toggleCapsLockState(DEVICE_ID);
4258 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4259}
4260
Arthur Hungfb3cc112022-04-13 07:39:50 +00004261TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4262 // keyboard 1.
4263 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4264 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4265 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4266 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4267 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4268 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4269
4270 KeyboardInputMapper& mapper1 =
4271 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4272 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4273
4274 // keyboard 2.
4275 const std::string USB2 = "USB2";
4276 const std::string DEVICE_NAME2 = "KEYBOARD2";
4277 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4278 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4279 std::shared_ptr<InputDevice> device2 =
4280 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4281 ftl::Flags<InputDeviceClass>(0));
4282 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4283 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4284 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4285 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4286 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4287 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4288
4289 KeyboardInputMapper& mapper2 =
4290 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4291 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004292 std::list<NotifyArgs> unused =
4293 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4294 0 /*changes*/);
4295 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004296
Arthur Hung95f68612022-04-07 14:08:22 +08004297 // Initial metastate is AMETA_NONE.
4298 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4299 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4300
4301 // Toggle num lock on and off.
4302 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4303 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004304 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4305 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4306 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4307
4308 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4309 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4310 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4311 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4312 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4313
4314 // Toggle caps lock on and off.
4315 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4316 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4317 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4318 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4319 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4320
4321 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4322 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4323 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4324 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4325 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4326
4327 // Toggle scroll lock on and off.
4328 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4329 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4330 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4331 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4332 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4333
4334 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4335 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4336 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4337 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4338 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4339}
4340
Arthur Hung2141d542022-08-23 07:45:21 +00004341TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4342 const int32_t USAGE_A = 0x070004;
4343 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4344 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4345
4346 KeyboardInputMapper& mapper =
4347 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4348 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4349 // Key down by scan code.
4350 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4351 NotifyKeyArgs args;
4352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4353 ASSERT_EQ(DEVICE_ID, args.deviceId);
4354 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4355 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4356 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4357 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4358 ASSERT_EQ(KEY_HOME, args.scanCode);
4359 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4360
4361 // Disable device, it should synthesize cancellation events for down events.
4362 mFakePolicy->addDisabledDevice(DEVICE_ID);
4363 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
4364
4365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4366 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4367 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4368 ASSERT_EQ(KEY_HOME, args.scanCode);
4369 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4370}
4371
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004372// --- KeyboardInputMapperTest_ExternalDevice ---
4373
4374class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4375protected:
Chris Yea52ade12020-08-27 16:49:20 -07004376 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004377};
4378
4379TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004380 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4381 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004382
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004383 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4384 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4385 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4386 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004387
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004388 KeyboardInputMapper& mapper =
4389 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4390 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004391
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004392 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004393 NotifyKeyArgs args;
4394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4395 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4396
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004397 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4399 ASSERT_EQ(uint32_t(0), args.policyFlags);
4400
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004401 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4403 ASSERT_EQ(uint32_t(0), args.policyFlags);
4404
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004405 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4407 ASSERT_EQ(uint32_t(0), args.policyFlags);
4408
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4411 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4412
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004413 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4415 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4416}
4417
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004418TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004419 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004420
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004421 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4422 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4423 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004424
Powei Fengd041c5d2019-05-03 17:11:33 -07004425 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004426 KeyboardInputMapper& mapper =
4427 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4428 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004429
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004430 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004431 NotifyKeyArgs args;
4432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4433 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4434
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004435 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4437 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4438
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004439 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4441 ASSERT_EQ(uint32_t(0), args.policyFlags);
4442
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004443 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4445 ASSERT_EQ(uint32_t(0), args.policyFlags);
4446
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004447 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4449 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4450
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004451 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4453 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4454}
4455
Michael Wrightd02c5b62014-02-10 15:10:22 -08004456// --- CursorInputMapperTest ---
4457
4458class CursorInputMapperTest : public InputMapperTest {
4459protected:
4460 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4461
Michael Wright17db18e2020-06-26 20:51:44 +01004462 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004463
Chris Yea52ade12020-08-27 16:49:20 -07004464 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004465 InputMapperTest::SetUp();
4466
Michael Wright17db18e2020-06-26 20:51:44 +01004467 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004468 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469 }
4470
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004471 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4472 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004473
4474 void prepareDisplay(int32_t orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004475 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4476 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4477 }
4478
4479 void prepareSecondaryDisplay() {
4480 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4481 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
4482 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004483 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004484
4485 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4486 float pressure) {
4487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4488 0.0f, 0.0f, 0.0f, EPSILON));
4489 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004490};
4491
4492const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4493
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004494void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4495 int32_t originalY, int32_t rotatedX,
4496 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004497 NotifyMotionArgs args;
4498
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004499 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4500 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4501 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4503 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004504 ASSERT_NO_FATAL_FAILURE(
4505 assertCursorPointerCoords(args.pointerCoords[0],
4506 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4507 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004508}
4509
4510TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004511 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004512 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004513
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004514 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004515}
4516
4517TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004518 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004519 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004520
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004521 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004522}
4523
4524TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004525 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004526 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004527
4528 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004529 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004530
4531 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004532 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4533 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004534 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4535 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4536
4537 // When the bounds are set, then there should be a valid motion range.
4538 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4539
4540 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004541 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004542
4543 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4544 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4545 1, 800 - 1, 0.0f, 0.0f));
4546 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4547 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4548 2, 480 - 1, 0.0f, 0.0f));
4549 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4550 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4551 0.0f, 1.0f, 0.0f, 0.0f));
4552}
4553
4554TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004555 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004556 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004557
4558 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004559 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004560
4561 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4562 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4563 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4564 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4565 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4566 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4567 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4568 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4569 0.0f, 1.0f, 0.0f, 0.0f));
4570}
4571
4572TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004573 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004574 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004575
arthurhungdcef2dc2020-08-11 14:47:50 +08004576 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004577
4578 NotifyMotionArgs args;
4579
4580 // Button press.
4581 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004582 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4583 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4585 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4586 ASSERT_EQ(DEVICE_ID, args.deviceId);
4587 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4588 ASSERT_EQ(uint32_t(0), args.policyFlags);
4589 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4590 ASSERT_EQ(0, args.flags);
4591 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4592 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4593 ASSERT_EQ(0, args.edgeFlags);
4594 ASSERT_EQ(uint32_t(1), args.pointerCount);
4595 ASSERT_EQ(0, args.pointerProperties[0].id);
4596 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004597 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004598 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4599 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4600 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4601
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4603 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4604 ASSERT_EQ(DEVICE_ID, args.deviceId);
4605 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4606 ASSERT_EQ(uint32_t(0), args.policyFlags);
4607 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4608 ASSERT_EQ(0, args.flags);
4609 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4610 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4611 ASSERT_EQ(0, args.edgeFlags);
4612 ASSERT_EQ(uint32_t(1), args.pointerCount);
4613 ASSERT_EQ(0, args.pointerProperties[0].id);
4614 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004615 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004616 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4617 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4618 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4619
Michael Wrightd02c5b62014-02-10 15:10:22 -08004620 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004621 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4622 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4624 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4625 ASSERT_EQ(DEVICE_ID, args.deviceId);
4626 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4627 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004628 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4629 ASSERT_EQ(0, args.flags);
4630 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4631 ASSERT_EQ(0, args.buttonState);
4632 ASSERT_EQ(0, args.edgeFlags);
4633 ASSERT_EQ(uint32_t(1), args.pointerCount);
4634 ASSERT_EQ(0, args.pointerProperties[0].id);
4635 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004636 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004637 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4638 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4639 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4640
4641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4642 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4643 ASSERT_EQ(DEVICE_ID, args.deviceId);
4644 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4645 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004646 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4647 ASSERT_EQ(0, args.flags);
4648 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4649 ASSERT_EQ(0, args.buttonState);
4650 ASSERT_EQ(0, args.edgeFlags);
4651 ASSERT_EQ(uint32_t(1), args.pointerCount);
4652 ASSERT_EQ(0, args.pointerProperties[0].id);
4653 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004654 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004655 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4656 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4657 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4658}
4659
4660TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004661 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004662 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004663
4664 NotifyMotionArgs args;
4665
4666 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004667 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4668 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4670 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004671 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4672 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4673 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004674
4675 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004676 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4677 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4679 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004680 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4681 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004682}
4683
4684TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004685 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004686 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004687
4688 NotifyMotionArgs args;
4689
4690 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004691 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4692 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4694 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004695 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004696
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4698 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004699 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004700
Michael Wrightd02c5b62014-02-10 15:10:22 -08004701 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004702 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4703 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004705 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004706 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004707
4708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004709 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004710 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004711}
4712
4713TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004714 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004715 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004716
4717 NotifyMotionArgs args;
4718
4719 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004720 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4721 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4722 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4723 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4725 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004726 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4727 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4728 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004729
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4731 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004732 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4733 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4734 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004735
Michael Wrightd02c5b62014-02-10 15:10:22 -08004736 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004737 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4738 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4739 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4741 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004742 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4743 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4744 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004745
4746 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004747 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4748 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004750 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004751 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004752
4753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004754 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004755 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004756}
4757
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004758TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004759 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004760 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004761 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4762 // need to be rotated.
4763 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004764 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004765
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004766 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004767 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4768 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4769 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4770 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4771 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4772 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4773 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4774 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4775}
4776
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004777TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004778 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004779 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004780 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4781 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004782 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004783
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004784 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004785 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004786 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4787 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4788 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4789 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4790 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4791 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4792 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4793 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4794
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004795 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004796 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004797 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4798 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4799 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4800 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4801 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4802 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4803 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4804 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004805
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004806 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004807 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004808 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4809 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4810 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4811 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4812 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4813 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4814 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4815 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4816
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004817 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004818 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004819 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4820 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4821 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4822 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4823 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4824 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4825 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4826 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004827}
4828
4829TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004830 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004831 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004832
4833 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4834 mFakePointerController->setPosition(100, 200);
4835 mFakePointerController->setButtonState(0);
4836
4837 NotifyMotionArgs motionArgs;
4838 NotifyKeyArgs keyArgs;
4839
4840 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004841 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4842 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4844 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4845 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4846 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004847 ASSERT_NO_FATAL_FAILURE(
4848 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004849
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4851 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4852 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4853 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004854 ASSERT_NO_FATAL_FAILURE(
4855 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004856
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004857 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4858 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004860 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004861 ASSERT_EQ(0, motionArgs.buttonState);
4862 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004863 ASSERT_NO_FATAL_FAILURE(
4864 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004865
4866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004867 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004868 ASSERT_EQ(0, motionArgs.buttonState);
4869 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004870 ASSERT_NO_FATAL_FAILURE(
4871 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004872
4873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004874 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004875 ASSERT_EQ(0, motionArgs.buttonState);
4876 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004877 ASSERT_NO_FATAL_FAILURE(
4878 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004879
4880 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004881 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4882 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4883 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4885 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4886 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4887 motionArgs.buttonState);
4888 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4889 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004890 ASSERT_NO_FATAL_FAILURE(
4891 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004892
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4894 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4895 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4896 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4897 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004898 ASSERT_NO_FATAL_FAILURE(
4899 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004900
4901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4902 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4903 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4904 motionArgs.buttonState);
4905 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4906 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004907 ASSERT_NO_FATAL_FAILURE(
4908 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004909
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004910 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4911 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004913 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004914 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4915 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004916 ASSERT_NO_FATAL_FAILURE(
4917 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004918
4919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004920 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004921 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4922 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004923 ASSERT_NO_FATAL_FAILURE(
4924 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004925
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004926 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4927 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004929 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4930 ASSERT_EQ(0, motionArgs.buttonState);
4931 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004932 ASSERT_NO_FATAL_FAILURE(
4933 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004934 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4935 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004936
4937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004938 ASSERT_EQ(0, motionArgs.buttonState);
4939 ASSERT_EQ(0, mFakePointerController->getButtonState());
4940 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004941 ASSERT_NO_FATAL_FAILURE(
4942 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004943
Michael Wrightd02c5b62014-02-10 15:10:22 -08004944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4945 ASSERT_EQ(0, motionArgs.buttonState);
4946 ASSERT_EQ(0, mFakePointerController->getButtonState());
4947 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004948 ASSERT_NO_FATAL_FAILURE(
4949 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004950
4951 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004952 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4953 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4955 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4956 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004957
Michael Wrightd02c5b62014-02-10 15:10:22 -08004958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004959 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004960 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4961 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004962 ASSERT_NO_FATAL_FAILURE(
4963 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004964
4965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4966 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4967 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4968 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004969 ASSERT_NO_FATAL_FAILURE(
4970 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004971
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004972 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4973 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004975 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004976 ASSERT_EQ(0, motionArgs.buttonState);
4977 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004978 ASSERT_NO_FATAL_FAILURE(
4979 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004980
4981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004982 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004983 ASSERT_EQ(0, motionArgs.buttonState);
4984 ASSERT_EQ(0, mFakePointerController->getButtonState());
4985
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004986 ASSERT_NO_FATAL_FAILURE(
4987 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4989 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4990 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4991
4992 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4994 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4996 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4997 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004998
Michael Wrightd02c5b62014-02-10 15:10:22 -08004999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005000 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005001 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5002 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005003 ASSERT_NO_FATAL_FAILURE(
5004 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005005
5006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5007 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5008 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5009 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005010 ASSERT_NO_FATAL_FAILURE(
5011 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005012
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005013 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
5014 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005016 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005017 ASSERT_EQ(0, motionArgs.buttonState);
5018 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005019 ASSERT_NO_FATAL_FAILURE(
5020 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005021
5022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5023 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5024 ASSERT_EQ(0, motionArgs.buttonState);
5025 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005026 ASSERT_NO_FATAL_FAILURE(
5027 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005028
Michael Wrightd02c5b62014-02-10 15:10:22 -08005029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5030 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5031 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5032
5033 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005034 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
5035 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5037 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5038 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005039
Michael Wrightd02c5b62014-02-10 15:10:22 -08005040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005041 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005042 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5043 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005044 ASSERT_NO_FATAL_FAILURE(
5045 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005046
5047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5048 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5049 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5050 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005051 ASSERT_NO_FATAL_FAILURE(
5052 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005053
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005054 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
5055 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005057 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005058 ASSERT_EQ(0, motionArgs.buttonState);
5059 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005060 ASSERT_NO_FATAL_FAILURE(
5061 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005062
5063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5064 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5065 ASSERT_EQ(0, motionArgs.buttonState);
5066 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005067 ASSERT_NO_FATAL_FAILURE(
5068 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005069
Michael Wrightd02c5b62014-02-10 15:10:22 -08005070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5071 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5072 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5073
5074 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005075 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
5076 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5078 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5079 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005080
Michael Wrightd02c5b62014-02-10 15:10:22 -08005081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005082 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005083 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5084 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005085 ASSERT_NO_FATAL_FAILURE(
5086 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005087
5088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5089 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5090 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5091 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005092 ASSERT_NO_FATAL_FAILURE(
5093 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005094
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005095 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
5096 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005098 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005099 ASSERT_EQ(0, motionArgs.buttonState);
5100 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005101 ASSERT_NO_FATAL_FAILURE(
5102 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005103
5104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5105 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5106 ASSERT_EQ(0, motionArgs.buttonState);
5107 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005108 ASSERT_NO_FATAL_FAILURE(
5109 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005110
Michael Wrightd02c5b62014-02-10 15:10:22 -08005111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5112 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5113 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5114}
5115
5116TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005117 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005118 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005119
5120 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5121 mFakePointerController->setPosition(100, 200);
5122 mFakePointerController->setButtonState(0);
5123
5124 NotifyMotionArgs args;
5125
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005126 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5127 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5128 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005130 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5131 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5132 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5133 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 +01005134 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005135}
5136
5137TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005138 addConfigurationProperty("cursor.mode", "pointer");
5139 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005140 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005141
5142 NotifyDeviceResetArgs resetArgs;
5143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5144 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5145 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5146
5147 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5148 mFakePointerController->setPosition(100, 200);
5149 mFakePointerController->setButtonState(0);
5150
5151 NotifyMotionArgs args;
5152
5153 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005154 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5155 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5156 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5158 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5159 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5160 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5161 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 +01005162 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005163
5164 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005165 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5166 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5168 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5169 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5170 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5171 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5173 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5174 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
5175 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5176 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5177
5178 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005179 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5180 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5182 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5183 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
5184 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5185 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5187 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5188 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
5189 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5190 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5191
5192 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005193 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
5194 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
5195 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5197 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5199 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5200 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 +01005201 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005202
5203 // Disable pointer capture and check that the device generation got bumped
5204 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08005205 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005206 mFakePolicy->setPointerCapture(false);
5207 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08005208 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005209
5210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005211 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5212
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005213 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5214 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5215 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5217 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005218 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5219 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5220 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 +01005221 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005222}
5223
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00005224/**
5225 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
5226 * pointer acceleration or speed processing should not be applied.
5227 */
5228TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
5229 addConfigurationProperty("cursor.mode", "pointer");
5230 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
5231 100.f /*high threshold*/, 10.f /*acceleration*/);
5232 mFakePolicy->setVelocityControlParams(testParams);
5233 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5234
5235 NotifyDeviceResetArgs resetArgs;
5236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5237 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5238 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5239
5240 NotifyMotionArgs args;
5241
5242 // Move and verify scale is applied.
5243 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5244 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5245 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5247 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5248 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5249 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5250 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5251 ASSERT_GT(relX, 10);
5252 ASSERT_GT(relY, 20);
5253
5254 // Enable Pointer Capture
5255 mFakePolicy->setPointerCapture(true);
5256 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5257 NotifyPointerCaptureChangedArgs captureArgs;
5258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5259 ASSERT_TRUE(captureArgs.request.enable);
5260
5261 // Move and verify scale is not applied.
5262 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5264 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5266 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5267 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5268 ASSERT_EQ(10, args.pointerCoords[0].getX());
5269 ASSERT_EQ(20, args.pointerCoords[0].getY());
5270}
5271
Prabir Pradhan208360b2022-06-24 18:37:04 +00005272TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5273 addConfigurationProperty("cursor.mode", "pointer");
5274 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5275
5276 NotifyDeviceResetArgs resetArgs;
5277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5278 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5279 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5280
5281 // Ensure the display is rotated.
5282 prepareDisplay(DISPLAY_ORIENTATION_90);
5283
5284 NotifyMotionArgs args;
5285
5286 // Verify that the coordinates are rotated.
5287 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5288 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5289 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5291 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5292 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5293 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5294 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5295
5296 // Enable Pointer Capture.
5297 mFakePolicy->setPointerCapture(true);
5298 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5299 NotifyPointerCaptureChangedArgs captureArgs;
5300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5301 ASSERT_TRUE(captureArgs.request.enable);
5302
5303 // Move and verify rotation is not applied.
5304 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5305 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5306 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5308 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5309 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5310 ASSERT_EQ(10, args.pointerCoords[0].getX());
5311 ASSERT_EQ(20, args.pointerCoords[0].getY());
5312}
5313
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005314TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005315 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005316
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005317 // Set up the default display.
5318 prepareDisplay(DISPLAY_ORIENTATION_90);
5319
5320 // Set up the secondary display as the display on which the pointer should be shown.
5321 // The InputDevice is not associated with any display.
5322 prepareSecondaryDisplay();
5323 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08005324 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5325
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005326 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005327 mFakePointerController->setPosition(100, 200);
5328 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005329
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005330 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005331 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5332 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5333 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005335 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5336 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5337 WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01005338 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005339}
5340
5341TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
5342 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5343
5344 // Set up the default display.
5345 prepareDisplay(DISPLAY_ORIENTATION_90);
5346
5347 // Set up the secondary display as the display on which the pointer should be shown,
5348 // and associate the InputDevice with the secondary display.
5349 prepareSecondaryDisplay();
5350 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5351 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5352 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5353
5354 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5355 mFakePointerController->setPosition(100, 200);
5356 mFakePointerController->setButtonState(0);
5357
5358 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5359 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5360 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005362 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5363 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5364 WithCoords(110.0f, 220.0f))));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005365 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
5366}
5367
5368TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
5369 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5370
5371 // Set up the default display as the display on which the pointer should be shown.
5372 prepareDisplay(DISPLAY_ORIENTATION_90);
5373 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5374
5375 // Associate the InputDevice with the secondary display.
5376 prepareSecondaryDisplay();
5377 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5378 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5379
5380 // The mapper should not generate any events because it is associated with a display that is
5381 // different from the pointer display.
5382 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5383 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5384 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005386}
5387
Michael Wrightd02c5b62014-02-10 15:10:22 -08005388// --- TouchInputMapperTest ---
5389
5390class TouchInputMapperTest : public InputMapperTest {
5391protected:
5392 static const int32_t RAW_X_MIN;
5393 static const int32_t RAW_X_MAX;
5394 static const int32_t RAW_Y_MIN;
5395 static const int32_t RAW_Y_MAX;
5396 static const int32_t RAW_TOUCH_MIN;
5397 static const int32_t RAW_TOUCH_MAX;
5398 static const int32_t RAW_TOOL_MIN;
5399 static const int32_t RAW_TOOL_MAX;
5400 static const int32_t RAW_PRESSURE_MIN;
5401 static const int32_t RAW_PRESSURE_MAX;
5402 static const int32_t RAW_ORIENTATION_MIN;
5403 static const int32_t RAW_ORIENTATION_MAX;
5404 static const int32_t RAW_DISTANCE_MIN;
5405 static const int32_t RAW_DISTANCE_MAX;
5406 static const int32_t RAW_TILT_MIN;
5407 static const int32_t RAW_TILT_MAX;
5408 static const int32_t RAW_ID_MIN;
5409 static const int32_t RAW_ID_MAX;
5410 static const int32_t RAW_SLOT_MIN;
5411 static const int32_t RAW_SLOT_MAX;
5412 static const float X_PRECISION;
5413 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005414 static const float X_PRECISION_VIRTUAL;
5415 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005416
5417 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005418 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005419
5420 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5421
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005422 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005423 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005424
Michael Wrightd02c5b62014-02-10 15:10:22 -08005425 enum Axes {
5426 POSITION = 1 << 0,
5427 TOUCH = 1 << 1,
5428 TOOL = 1 << 2,
5429 PRESSURE = 1 << 3,
5430 ORIENTATION = 1 << 4,
5431 MINOR = 1 << 5,
5432 ID = 1 << 6,
5433 DISTANCE = 1 << 7,
5434 TILT = 1 << 8,
5435 SLOT = 1 << 9,
5436 TOOL_TYPE = 1 << 10,
5437 };
5438
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005439 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5440 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005441 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005442 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005443 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005444 int32_t toRawX(float displayX);
5445 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005446 int32_t toRotatedRawX(float displayX);
5447 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005448 float toCookedX(float rawX, float rawY);
5449 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005450 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005451 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005452 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005453 float toDisplayY(int32_t rawY, int32_t displayHeight);
5454
Michael Wrightd02c5b62014-02-10 15:10:22 -08005455};
5456
5457const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5458const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5459const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5460const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5461const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5462const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5463const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5464const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005465const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5466const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005467const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5468const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5469const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5470const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5471const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5472const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5473const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5474const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5475const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5476const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5477const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5478const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005479const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5480 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5481const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5482 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005483const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5484 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005485
5486const float TouchInputMapperTest::GEOMETRIC_SCALE =
5487 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5488 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5489
5490const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5491 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5492 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5493};
5494
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005495void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005496 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5497 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005498}
5499
5500void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5501 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5502 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005503}
5504
Santos Cordonfa5cf462017-04-05 10:37:00 -07005505void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005506 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5507 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5508 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005509}
5510
Michael Wrightd02c5b62014-02-10 15:10:22 -08005511void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005512 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5513 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5514 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5515 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005516}
5517
Jason Gerecke489fda82012-09-07 17:19:40 -07005518void TouchInputMapperTest::prepareLocationCalibration() {
5519 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5520}
5521
Michael Wrightd02c5b62014-02-10 15:10:22 -08005522int32_t TouchInputMapperTest::toRawX(float displayX) {
5523 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5524}
5525
5526int32_t TouchInputMapperTest::toRawY(float displayY) {
5527 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5528}
5529
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005530int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5531 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5532}
5533
5534int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5535 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5536}
5537
Jason Gerecke489fda82012-09-07 17:19:40 -07005538float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5539 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5540 return rawX;
5541}
5542
5543float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5544 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5545 return rawY;
5546}
5547
Michael Wrightd02c5b62014-02-10 15:10:22 -08005548float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005549 return toDisplayX(rawX, DISPLAY_WIDTH);
5550}
5551
5552float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5553 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005554}
5555
5556float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005557 return toDisplayY(rawY, DISPLAY_HEIGHT);
5558}
5559
5560float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5561 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005562}
5563
5564
5565// --- SingleTouchInputMapperTest ---
5566
5567class SingleTouchInputMapperTest : public TouchInputMapperTest {
5568protected:
5569 void prepareButtons();
5570 void prepareAxes(int axes);
5571
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005572 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5573 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5574 void processUp(SingleTouchInputMapper& mappery);
5575 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5576 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5577 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5578 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5579 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5580 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005581};
5582
5583void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005584 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005585}
5586
5587void SingleTouchInputMapperTest::prepareAxes(int axes) {
5588 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005589 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5590 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005591 }
5592 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005593 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5594 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005595 }
5596 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005597 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5598 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005599 }
5600 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005601 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5602 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005603 }
5604 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005605 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5606 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005607 }
5608}
5609
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005610void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005611 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5612 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5613 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005614}
5615
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005616void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005617 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5618 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005619}
5620
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005621void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005622 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005623}
5624
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005625void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005626 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005627}
5628
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005629void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5630 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005631 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005632}
5633
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005634void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005635 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005636}
5637
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005638void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5639 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005640 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5641 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005642}
5643
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005644void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5645 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005646 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005647}
5648
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005649void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005650 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005651}
5652
Michael Wrightd02c5b62014-02-10 15:10:22 -08005653TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005654 prepareButtons();
5655 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005656 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005657
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005658 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005659}
5660
Michael Wrightd02c5b62014-02-10 15:10:22 -08005661TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005662 prepareButtons();
5663 prepareAxes(POSITION);
5664 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005665 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005666
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005667 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005668}
5669
5670TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005671 addConfigurationProperty("touch.deviceType", "touchScreen");
5672 prepareDisplay(DISPLAY_ORIENTATION_0);
5673 prepareButtons();
5674 prepareAxes(POSITION);
5675 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005676 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005677
5678 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005679 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005680
5681 // Virtual key is down.
5682 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5683 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5684 processDown(mapper, x, y);
5685 processSync(mapper);
5686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5687
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005688 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005689
5690 // Virtual key is up.
5691 processUp(mapper);
5692 processSync(mapper);
5693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5694
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005695 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005696}
5697
5698TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005699 addConfigurationProperty("touch.deviceType", "touchScreen");
5700 prepareDisplay(DISPLAY_ORIENTATION_0);
5701 prepareButtons();
5702 prepareAxes(POSITION);
5703 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005704 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005705
5706 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005707 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005708
5709 // Virtual key is down.
5710 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5711 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5712 processDown(mapper, x, y);
5713 processSync(mapper);
5714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5715
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005716 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005717
5718 // Virtual key is up.
5719 processUp(mapper);
5720 processSync(mapper);
5721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5722
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005723 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005724}
5725
5726TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005727 addConfigurationProperty("touch.deviceType", "touchScreen");
5728 prepareDisplay(DISPLAY_ORIENTATION_0);
5729 prepareButtons();
5730 prepareAxes(POSITION);
5731 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005732 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005733
Michael Wrightd02c5b62014-02-10 15:10:22 -08005734 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005735 ASSERT_TRUE(
5736 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005737 ASSERT_TRUE(flags[0]);
5738 ASSERT_FALSE(flags[1]);
5739}
5740
5741TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005742 addConfigurationProperty("touch.deviceType", "touchScreen");
5743 prepareDisplay(DISPLAY_ORIENTATION_0);
5744 prepareButtons();
5745 prepareAxes(POSITION);
5746 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005747 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005748
arthurhungdcef2dc2020-08-11 14:47:50 +08005749 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005750
5751 NotifyKeyArgs args;
5752
5753 // Press virtual key.
5754 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5755 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5756 processDown(mapper, x, y);
5757 processSync(mapper);
5758
5759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5760 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5761 ASSERT_EQ(DEVICE_ID, args.deviceId);
5762 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5763 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5764 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5765 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5766 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5767 ASSERT_EQ(KEY_HOME, args.scanCode);
5768 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5769 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5770
5771 // Release virtual key.
5772 processUp(mapper);
5773 processSync(mapper);
5774
5775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5776 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5777 ASSERT_EQ(DEVICE_ID, args.deviceId);
5778 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5779 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5780 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5781 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5782 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5783 ASSERT_EQ(KEY_HOME, args.scanCode);
5784 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5785 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5786
5787 // Should not have sent any motions.
5788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5789}
5790
5791TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005792 addConfigurationProperty("touch.deviceType", "touchScreen");
5793 prepareDisplay(DISPLAY_ORIENTATION_0);
5794 prepareButtons();
5795 prepareAxes(POSITION);
5796 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005797 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005798
arthurhungdcef2dc2020-08-11 14:47:50 +08005799 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005800
5801 NotifyKeyArgs keyArgs;
5802
5803 // Press virtual key.
5804 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5805 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5806 processDown(mapper, x, y);
5807 processSync(mapper);
5808
5809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5810 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5811 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5812 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5813 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5814 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5815 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5816 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5817 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5818 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5819 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5820
5821 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5822 // into the display area.
5823 y -= 100;
5824 processMove(mapper, x, y);
5825 processSync(mapper);
5826
5827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5828 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5829 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5830 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5831 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5832 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5833 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5834 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5835 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5836 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5837 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5838 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5839
5840 NotifyMotionArgs motionArgs;
5841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5842 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5843 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5844 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5845 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5846 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5847 ASSERT_EQ(0, motionArgs.flags);
5848 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5849 ASSERT_EQ(0, motionArgs.buttonState);
5850 ASSERT_EQ(0, motionArgs.edgeFlags);
5851 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5852 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5853 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5854 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5855 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5856 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5857 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5858 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5859
5860 // Keep moving out of bounds. Should generate a pointer move.
5861 y -= 50;
5862 processMove(mapper, x, y);
5863 processSync(mapper);
5864
5865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5866 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5867 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5868 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5869 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5870 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5871 ASSERT_EQ(0, motionArgs.flags);
5872 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5873 ASSERT_EQ(0, motionArgs.buttonState);
5874 ASSERT_EQ(0, motionArgs.edgeFlags);
5875 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5876 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5877 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5878 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5879 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5880 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5881 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5882 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5883
5884 // Release out of bounds. Should generate a pointer up.
5885 processUp(mapper);
5886 processSync(mapper);
5887
5888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5889 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5890 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5891 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5892 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5893 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5894 ASSERT_EQ(0, motionArgs.flags);
5895 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5896 ASSERT_EQ(0, motionArgs.buttonState);
5897 ASSERT_EQ(0, motionArgs.edgeFlags);
5898 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5899 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5900 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5901 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5902 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5903 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5904 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5905 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5906
5907 // Should not have sent any more keys or motions.
5908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5910}
5911
5912TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005913 addConfigurationProperty("touch.deviceType", "touchScreen");
5914 prepareDisplay(DISPLAY_ORIENTATION_0);
5915 prepareButtons();
5916 prepareAxes(POSITION);
5917 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005918 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005919
arthurhungdcef2dc2020-08-11 14:47:50 +08005920 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005921
5922 NotifyMotionArgs motionArgs;
5923
5924 // Initially go down out of bounds.
5925 int32_t x = -10;
5926 int32_t y = -10;
5927 processDown(mapper, x, y);
5928 processSync(mapper);
5929
5930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5931
5932 // Move into the display area. Should generate a pointer down.
5933 x = 50;
5934 y = 75;
5935 processMove(mapper, x, y);
5936 processSync(mapper);
5937
5938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5939 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5940 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5941 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5942 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5943 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5944 ASSERT_EQ(0, motionArgs.flags);
5945 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5946 ASSERT_EQ(0, motionArgs.buttonState);
5947 ASSERT_EQ(0, motionArgs.edgeFlags);
5948 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5949 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5950 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5951 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5952 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5953 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5954 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5955 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5956
5957 // Release. Should generate a pointer up.
5958 processUp(mapper);
5959 processSync(mapper);
5960
5961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5962 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5963 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5964 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5965 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5966 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5967 ASSERT_EQ(0, motionArgs.flags);
5968 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5969 ASSERT_EQ(0, motionArgs.buttonState);
5970 ASSERT_EQ(0, motionArgs.edgeFlags);
5971 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5972 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5973 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5974 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5975 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5976 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5977 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5978 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5979
5980 // Should not have sent any more keys or motions.
5981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5983}
5984
Santos Cordonfa5cf462017-04-05 10:37:00 -07005985TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005986 addConfigurationProperty("touch.deviceType", "touchScreen");
5987 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5988
5989 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5990 prepareButtons();
5991 prepareAxes(POSITION);
5992 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005993 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005994
arthurhungdcef2dc2020-08-11 14:47:50 +08005995 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005996
5997 NotifyMotionArgs motionArgs;
5998
5999 // Down.
6000 int32_t x = 100;
6001 int32_t y = 125;
6002 processDown(mapper, x, y);
6003 processSync(mapper);
6004
6005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6006 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6007 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6008 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6009 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6010 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6011 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6012 ASSERT_EQ(0, motionArgs.flags);
6013 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6014 ASSERT_EQ(0, motionArgs.buttonState);
6015 ASSERT_EQ(0, motionArgs.edgeFlags);
6016 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6017 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6018 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6019 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6020 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6021 1, 0, 0, 0, 0, 0, 0, 0));
6022 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6023 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6024 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6025
6026 // Move.
6027 x += 50;
6028 y += 75;
6029 processMove(mapper, x, y);
6030 processSync(mapper);
6031
6032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6033 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6034 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6035 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6036 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6037 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6038 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6039 ASSERT_EQ(0, motionArgs.flags);
6040 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6041 ASSERT_EQ(0, motionArgs.buttonState);
6042 ASSERT_EQ(0, motionArgs.edgeFlags);
6043 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6044 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6045 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6046 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6047 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6048 1, 0, 0, 0, 0, 0, 0, 0));
6049 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6050 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6051 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6052
6053 // Up.
6054 processUp(mapper);
6055 processSync(mapper);
6056
6057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6058 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6059 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6060 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6061 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6062 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6063 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6064 ASSERT_EQ(0, motionArgs.flags);
6065 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6066 ASSERT_EQ(0, motionArgs.buttonState);
6067 ASSERT_EQ(0, motionArgs.edgeFlags);
6068 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6069 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6070 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6071 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6072 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6073 1, 0, 0, 0, 0, 0, 0, 0));
6074 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6075 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6076 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6077
6078 // Should not have sent any more keys or motions.
6079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6081}
6082
Michael Wrightd02c5b62014-02-10 15:10:22 -08006083TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006084 addConfigurationProperty("touch.deviceType", "touchScreen");
6085 prepareDisplay(DISPLAY_ORIENTATION_0);
6086 prepareButtons();
6087 prepareAxes(POSITION);
6088 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006089 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006090
arthurhungdcef2dc2020-08-11 14:47:50 +08006091 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006092
6093 NotifyMotionArgs motionArgs;
6094
6095 // Down.
6096 int32_t x = 100;
6097 int32_t y = 125;
6098 processDown(mapper, x, y);
6099 processSync(mapper);
6100
6101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6102 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6103 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6104 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6105 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6106 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6107 ASSERT_EQ(0, motionArgs.flags);
6108 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6109 ASSERT_EQ(0, motionArgs.buttonState);
6110 ASSERT_EQ(0, motionArgs.edgeFlags);
6111 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6112 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6113 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6114 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6115 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6116 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6117 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6118 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6119
6120 // Move.
6121 x += 50;
6122 y += 75;
6123 processMove(mapper, x, y);
6124 processSync(mapper);
6125
6126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6127 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6128 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6129 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6130 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6131 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6132 ASSERT_EQ(0, motionArgs.flags);
6133 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6134 ASSERT_EQ(0, motionArgs.buttonState);
6135 ASSERT_EQ(0, motionArgs.edgeFlags);
6136 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6137 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6138 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6139 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6140 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6141 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6142 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6143 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6144
6145 // Up.
6146 processUp(mapper);
6147 processSync(mapper);
6148
6149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6150 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6151 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6152 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6153 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6154 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6155 ASSERT_EQ(0, motionArgs.flags);
6156 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6157 ASSERT_EQ(0, motionArgs.buttonState);
6158 ASSERT_EQ(0, motionArgs.edgeFlags);
6159 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6160 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6161 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6162 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6163 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6164 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6165 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6166 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6167
6168 // Should not have sent any more keys or motions.
6169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6171}
6172
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006173TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006174 addConfigurationProperty("touch.deviceType", "touchScreen");
6175 prepareButtons();
6176 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006177 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
6178 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006179 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006180
6181 NotifyMotionArgs args;
6182
6183 // Rotation 90.
6184 prepareDisplay(DISPLAY_ORIENTATION_90);
6185 processDown(mapper, toRawX(50), toRawY(75));
6186 processSync(mapper);
6187
6188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6189 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6190 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6191
6192 processUp(mapper);
6193 processSync(mapper);
6194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6195}
6196
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006197TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006198 addConfigurationProperty("touch.deviceType", "touchScreen");
6199 prepareButtons();
6200 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006201 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6202 // orientation-aware are affected by display rotation.
6203 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006204 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006205
6206 NotifyMotionArgs args;
6207
6208 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006209 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006210 prepareDisplay(DISPLAY_ORIENTATION_0);
6211 processDown(mapper, toRawX(50), toRawY(75));
6212 processSync(mapper);
6213
6214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6215 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6216 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6217
6218 processUp(mapper);
6219 processSync(mapper);
6220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6221
6222 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006223 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006224 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006225 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006226 processSync(mapper);
6227
6228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6229 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6230 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6231
6232 processUp(mapper);
6233 processSync(mapper);
6234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6235
6236 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006237 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006238 prepareDisplay(DISPLAY_ORIENTATION_180);
6239 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6240 processSync(mapper);
6241
6242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6243 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6244 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6245
6246 processUp(mapper);
6247 processSync(mapper);
6248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6249
6250 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006251 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006252 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006253 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006254 processSync(mapper);
6255
6256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6257 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6258 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6259
6260 processUp(mapper);
6261 processSync(mapper);
6262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6263}
6264
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006265TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6266 addConfigurationProperty("touch.deviceType", "touchScreen");
6267 prepareButtons();
6268 prepareAxes(POSITION);
6269 addConfigurationProperty("touch.orientationAware", "1");
6270 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6271 clearViewports();
6272 prepareDisplay(DISPLAY_ORIENTATION_0);
6273 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6274 NotifyMotionArgs args;
6275
6276 // Orientation 0.
6277 processDown(mapper, toRawX(50), toRawY(75));
6278 processSync(mapper);
6279
6280 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6281 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6282 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6283
6284 processUp(mapper);
6285 processSync(mapper);
6286 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6287}
6288
6289TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6290 addConfigurationProperty("touch.deviceType", "touchScreen");
6291 prepareButtons();
6292 prepareAxes(POSITION);
6293 addConfigurationProperty("touch.orientationAware", "1");
6294 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6295 clearViewports();
6296 prepareDisplay(DISPLAY_ORIENTATION_0);
6297 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6298 NotifyMotionArgs args;
6299
6300 // Orientation 90.
6301 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6302 processSync(mapper);
6303
6304 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6305 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6306 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6307
6308 processUp(mapper);
6309 processSync(mapper);
6310 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6311}
6312
6313TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6314 addConfigurationProperty("touch.deviceType", "touchScreen");
6315 prepareButtons();
6316 prepareAxes(POSITION);
6317 addConfigurationProperty("touch.orientationAware", "1");
6318 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6319 clearViewports();
6320 prepareDisplay(DISPLAY_ORIENTATION_0);
6321 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6322 NotifyMotionArgs args;
6323
6324 // Orientation 180.
6325 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6326 processSync(mapper);
6327
6328 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6329 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6330 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6331
6332 processUp(mapper);
6333 processSync(mapper);
6334 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6335}
6336
6337TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6338 addConfigurationProperty("touch.deviceType", "touchScreen");
6339 prepareButtons();
6340 prepareAxes(POSITION);
6341 addConfigurationProperty("touch.orientationAware", "1");
6342 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6343 clearViewports();
6344 prepareDisplay(DISPLAY_ORIENTATION_0);
6345 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6346 NotifyMotionArgs args;
6347
6348 // Orientation 270.
6349 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6350 processSync(mapper);
6351
6352 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6353 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6354 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6355
6356 processUp(mapper);
6357 processSync(mapper);
6358 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6359}
6360
6361TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6362 addConfigurationProperty("touch.deviceType", "touchScreen");
6363 prepareButtons();
6364 prepareAxes(POSITION);
6365 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6366 // orientation-aware are affected by display rotation.
6367 addConfigurationProperty("touch.orientationAware", "0");
6368 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6369 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6370
6371 NotifyMotionArgs args;
6372
6373 // Orientation 90, Rotation 0.
6374 clearViewports();
6375 prepareDisplay(DISPLAY_ORIENTATION_0);
6376 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6377 processSync(mapper);
6378
6379 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6380 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6381 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6382
6383 processUp(mapper);
6384 processSync(mapper);
6385 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6386
6387 // Orientation 90, Rotation 90.
6388 clearViewports();
6389 prepareDisplay(DISPLAY_ORIENTATION_90);
6390 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6391 processSync(mapper);
6392
6393 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6394 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6395 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6396
6397 processUp(mapper);
6398 processSync(mapper);
6399 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6400
6401 // Orientation 90, Rotation 180.
6402 clearViewports();
6403 prepareDisplay(DISPLAY_ORIENTATION_180);
6404 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6405 processSync(mapper);
6406
6407 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6408 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6409 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6410
6411 processUp(mapper);
6412 processSync(mapper);
6413 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6414
6415 // Orientation 90, Rotation 270.
6416 clearViewports();
6417 prepareDisplay(DISPLAY_ORIENTATION_270);
6418 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6419 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6420 processSync(mapper);
6421
6422 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6423 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6424 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6425
6426 processUp(mapper);
6427 processSync(mapper);
6428 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6429}
6430
Michael Wrightd02c5b62014-02-10 15:10:22 -08006431TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006432 addConfigurationProperty("touch.deviceType", "touchScreen");
6433 prepareDisplay(DISPLAY_ORIENTATION_0);
6434 prepareButtons();
6435 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006436 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006437
6438 // These calculations are based on the input device calibration documentation.
6439 int32_t rawX = 100;
6440 int32_t rawY = 200;
6441 int32_t rawPressure = 10;
6442 int32_t rawToolMajor = 12;
6443 int32_t rawDistance = 2;
6444 int32_t rawTiltX = 30;
6445 int32_t rawTiltY = 110;
6446
6447 float x = toDisplayX(rawX);
6448 float y = toDisplayY(rawY);
6449 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6450 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6451 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6452 float distance = float(rawDistance);
6453
6454 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6455 float tiltScale = M_PI / 180;
6456 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6457 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6458 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6459 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6460
6461 processDown(mapper, rawX, rawY);
6462 processPressure(mapper, rawPressure);
6463 processToolMajor(mapper, rawToolMajor);
6464 processDistance(mapper, rawDistance);
6465 processTilt(mapper, rawTiltX, rawTiltY);
6466 processSync(mapper);
6467
6468 NotifyMotionArgs args;
6469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6471 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6472 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6473}
6474
Jason Gerecke489fda82012-09-07 17:19:40 -07006475TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006476 addConfigurationProperty("touch.deviceType", "touchScreen");
6477 prepareDisplay(DISPLAY_ORIENTATION_0);
6478 prepareLocationCalibration();
6479 prepareButtons();
6480 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006481 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006482
6483 int32_t rawX = 100;
6484 int32_t rawY = 200;
6485
6486 float x = toDisplayX(toCookedX(rawX, rawY));
6487 float y = toDisplayY(toCookedY(rawX, rawY));
6488
6489 processDown(mapper, rawX, rawY);
6490 processSync(mapper);
6491
6492 NotifyMotionArgs args;
6493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6495 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6496}
6497
Michael Wrightd02c5b62014-02-10 15:10:22 -08006498TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006499 addConfigurationProperty("touch.deviceType", "touchScreen");
6500 prepareDisplay(DISPLAY_ORIENTATION_0);
6501 prepareButtons();
6502 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006503 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006504
6505 NotifyMotionArgs motionArgs;
6506 NotifyKeyArgs keyArgs;
6507
6508 processDown(mapper, 100, 200);
6509 processSync(mapper);
6510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6511 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6512 ASSERT_EQ(0, motionArgs.buttonState);
6513
6514 // press BTN_LEFT, release BTN_LEFT
6515 processKey(mapper, BTN_LEFT, 1);
6516 processSync(mapper);
6517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6518 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6519 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6520
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6522 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6523 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6524
Michael Wrightd02c5b62014-02-10 15:10:22 -08006525 processKey(mapper, BTN_LEFT, 0);
6526 processSync(mapper);
6527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006528 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006529 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006530
6531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006532 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006533 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006534
6535 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6536 processKey(mapper, BTN_RIGHT, 1);
6537 processKey(mapper, BTN_MIDDLE, 1);
6538 processSync(mapper);
6539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6540 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6541 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6542 motionArgs.buttonState);
6543
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6545 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6546 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6547
6548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6549 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6550 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6551 motionArgs.buttonState);
6552
Michael Wrightd02c5b62014-02-10 15:10:22 -08006553 processKey(mapper, BTN_RIGHT, 0);
6554 processSync(mapper);
6555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006556 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006557 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006558
6559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006560 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006561 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006562
6563 processKey(mapper, BTN_MIDDLE, 0);
6564 processSync(mapper);
6565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006566 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006567 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006568
6569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006570 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006571 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006572
6573 // press BTN_BACK, release BTN_BACK
6574 processKey(mapper, BTN_BACK, 1);
6575 processSync(mapper);
6576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6577 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6578 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006579
Michael Wrightd02c5b62014-02-10 15:10:22 -08006580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006581 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006582 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6583
6584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6585 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6586 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006587
6588 processKey(mapper, BTN_BACK, 0);
6589 processSync(mapper);
6590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006591 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006592 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006593
6594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006595 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006596 ASSERT_EQ(0, motionArgs.buttonState);
6597
Michael Wrightd02c5b62014-02-10 15:10:22 -08006598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6599 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6600 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6601
6602 // press BTN_SIDE, release BTN_SIDE
6603 processKey(mapper, BTN_SIDE, 1);
6604 processSync(mapper);
6605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6606 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6607 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006608
Michael Wrightd02c5b62014-02-10 15:10:22 -08006609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006610 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006611 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6612
6613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6614 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6615 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006616
6617 processKey(mapper, BTN_SIDE, 0);
6618 processSync(mapper);
6619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006620 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006621 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006622
6623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006624 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006625 ASSERT_EQ(0, motionArgs.buttonState);
6626
Michael Wrightd02c5b62014-02-10 15:10:22 -08006627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6628 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6629 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6630
6631 // press BTN_FORWARD, release BTN_FORWARD
6632 processKey(mapper, BTN_FORWARD, 1);
6633 processSync(mapper);
6634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6635 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6636 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006637
Michael Wrightd02c5b62014-02-10 15:10:22 -08006638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006639 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006640 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6641
6642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6643 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6644 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006645
6646 processKey(mapper, BTN_FORWARD, 0);
6647 processSync(mapper);
6648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006649 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006650 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006651
6652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006653 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006654 ASSERT_EQ(0, motionArgs.buttonState);
6655
Michael Wrightd02c5b62014-02-10 15:10:22 -08006656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6657 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6658 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6659
6660 // press BTN_EXTRA, release BTN_EXTRA
6661 processKey(mapper, BTN_EXTRA, 1);
6662 processSync(mapper);
6663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6664 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6665 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006666
Michael Wrightd02c5b62014-02-10 15:10:22 -08006667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006668 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006669 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6670
6671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6672 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6673 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006674
6675 processKey(mapper, BTN_EXTRA, 0);
6676 processSync(mapper);
6677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006678 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006679 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006680
6681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006682 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006683 ASSERT_EQ(0, motionArgs.buttonState);
6684
Michael Wrightd02c5b62014-02-10 15:10:22 -08006685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6686 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6687 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6688
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6690
Michael Wrightd02c5b62014-02-10 15:10:22 -08006691 // press BTN_STYLUS, release BTN_STYLUS
6692 processKey(mapper, BTN_STYLUS, 1);
6693 processSync(mapper);
6694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6695 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006696 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6697
6698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6699 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6700 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006701
6702 processKey(mapper, BTN_STYLUS, 0);
6703 processSync(mapper);
6704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006705 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006706 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006707
6708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006709 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006710 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006711
6712 // press BTN_STYLUS2, release BTN_STYLUS2
6713 processKey(mapper, BTN_STYLUS2, 1);
6714 processSync(mapper);
6715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6716 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006717 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6718
6719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6720 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6721 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006722
6723 processKey(mapper, BTN_STYLUS2, 0);
6724 processSync(mapper);
6725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006726 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006727 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006728
6729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006730 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006731 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006732
6733 // release touch
6734 processUp(mapper);
6735 processSync(mapper);
6736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6737 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6738 ASSERT_EQ(0, motionArgs.buttonState);
6739}
6740
6741TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006742 addConfigurationProperty("touch.deviceType", "touchScreen");
6743 prepareDisplay(DISPLAY_ORIENTATION_0);
6744 prepareButtons();
6745 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006746 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006747
6748 NotifyMotionArgs motionArgs;
6749
6750 // default tool type is finger
6751 processDown(mapper, 100, 200);
6752 processSync(mapper);
6753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6754 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6755 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6756
6757 // eraser
6758 processKey(mapper, BTN_TOOL_RUBBER, 1);
6759 processSync(mapper);
6760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6761 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6762 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6763
6764 // stylus
6765 processKey(mapper, BTN_TOOL_RUBBER, 0);
6766 processKey(mapper, BTN_TOOL_PEN, 1);
6767 processSync(mapper);
6768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6769 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6770 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6771
6772 // brush
6773 processKey(mapper, BTN_TOOL_PEN, 0);
6774 processKey(mapper, BTN_TOOL_BRUSH, 1);
6775 processSync(mapper);
6776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6777 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6778 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6779
6780 // pencil
6781 processKey(mapper, BTN_TOOL_BRUSH, 0);
6782 processKey(mapper, BTN_TOOL_PENCIL, 1);
6783 processSync(mapper);
6784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6785 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6786 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6787
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006788 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006789 processKey(mapper, BTN_TOOL_PENCIL, 0);
6790 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6791 processSync(mapper);
6792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6793 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6794 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6795
6796 // mouse
6797 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6798 processKey(mapper, BTN_TOOL_MOUSE, 1);
6799 processSync(mapper);
6800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6801 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6802 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6803
6804 // lens
6805 processKey(mapper, BTN_TOOL_MOUSE, 0);
6806 processKey(mapper, BTN_TOOL_LENS, 1);
6807 processSync(mapper);
6808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6809 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6810 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6811
6812 // double-tap
6813 processKey(mapper, BTN_TOOL_LENS, 0);
6814 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6815 processSync(mapper);
6816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6817 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6818 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6819
6820 // triple-tap
6821 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6822 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6823 processSync(mapper);
6824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6825 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6826 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6827
6828 // quad-tap
6829 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6830 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6831 processSync(mapper);
6832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6833 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6834 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6835
6836 // finger
6837 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6838 processKey(mapper, BTN_TOOL_FINGER, 1);
6839 processSync(mapper);
6840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6841 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6842 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6843
6844 // stylus trumps finger
6845 processKey(mapper, BTN_TOOL_PEN, 1);
6846 processSync(mapper);
6847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6848 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6849 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6850
6851 // eraser trumps stylus
6852 processKey(mapper, BTN_TOOL_RUBBER, 1);
6853 processSync(mapper);
6854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6855 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6856 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6857
6858 // mouse trumps eraser
6859 processKey(mapper, BTN_TOOL_MOUSE, 1);
6860 processSync(mapper);
6861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6862 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6863 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6864
6865 // back to default tool type
6866 processKey(mapper, BTN_TOOL_MOUSE, 0);
6867 processKey(mapper, BTN_TOOL_RUBBER, 0);
6868 processKey(mapper, BTN_TOOL_PEN, 0);
6869 processKey(mapper, BTN_TOOL_FINGER, 0);
6870 processSync(mapper);
6871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6872 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6873 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6874}
6875
6876TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006877 addConfigurationProperty("touch.deviceType", "touchScreen");
6878 prepareDisplay(DISPLAY_ORIENTATION_0);
6879 prepareButtons();
6880 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006881 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006882 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006883
6884 NotifyMotionArgs motionArgs;
6885
6886 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6887 processKey(mapper, BTN_TOOL_FINGER, 1);
6888 processMove(mapper, 100, 200);
6889 processSync(mapper);
6890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6891 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6892 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6893 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6894
6895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6896 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6897 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6898 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6899
6900 // move a little
6901 processMove(mapper, 150, 250);
6902 processSync(mapper);
6903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6904 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6905 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6906 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6907
6908 // down when BTN_TOUCH is pressed, pressure defaults to 1
6909 processKey(mapper, BTN_TOUCH, 1);
6910 processSync(mapper);
6911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6912 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6913 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6914 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6915
6916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6917 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6918 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6919 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6920
6921 // up when BTN_TOUCH is released, hover restored
6922 processKey(mapper, BTN_TOUCH, 0);
6923 processSync(mapper);
6924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6925 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6926 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6927 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6928
6929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6930 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6931 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6932 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6933
6934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6935 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6936 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6937 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6938
6939 // exit hover when pointer goes away
6940 processKey(mapper, BTN_TOOL_FINGER, 0);
6941 processSync(mapper);
6942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6943 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6944 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6945 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6946}
6947
6948TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006949 addConfigurationProperty("touch.deviceType", "touchScreen");
6950 prepareDisplay(DISPLAY_ORIENTATION_0);
6951 prepareButtons();
6952 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006953 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006954
6955 NotifyMotionArgs motionArgs;
6956
6957 // initially hovering because pressure is 0
6958 processDown(mapper, 100, 200);
6959 processPressure(mapper, 0);
6960 processSync(mapper);
6961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6962 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6963 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6964 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6965
6966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6967 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6968 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6969 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6970
6971 // move a little
6972 processMove(mapper, 150, 250);
6973 processSync(mapper);
6974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6975 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6976 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6977 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6978
6979 // down when pressure is non-zero
6980 processPressure(mapper, RAW_PRESSURE_MAX);
6981 processSync(mapper);
6982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6983 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6984 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6985 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6986
6987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6988 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6989 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6990 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6991
6992 // up when pressure becomes 0, hover restored
6993 processPressure(mapper, 0);
6994 processSync(mapper);
6995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6996 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6997 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6998 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6999
7000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7001 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7002 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7003 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7004
7005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7006 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7007 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7008 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7009
7010 // exit hover when pointer goes away
7011 processUp(mapper);
7012 processSync(mapper);
7013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7014 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7015 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7016 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7017}
7018
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007019TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
7020 addConfigurationProperty("touch.deviceType", "touchScreen");
7021 prepareDisplay(DISPLAY_ORIENTATION_0);
7022 prepareButtons();
7023 prepareAxes(POSITION | PRESSURE);
7024 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7025
7026 // Touch down.
7027 processDown(mapper, 100, 200);
7028 processPressure(mapper, 1);
7029 processSync(mapper);
7030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7031 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
7032
7033 // Reset the mapper. This should cancel the ongoing gesture.
7034 resetMapper(mapper, ARBITRARY_TIME);
7035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7036 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
7037
7038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7039}
7040
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007041TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
7042 addConfigurationProperty("touch.deviceType", "touchScreen");
7043 prepareDisplay(DISPLAY_ORIENTATION_0);
7044 prepareButtons();
7045 prepareAxes(POSITION | PRESSURE);
7046 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7047
7048 // Set the initial state for the touch pointer.
7049 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
7050 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
7051 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
7052 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7053
7054 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007055 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
7056 // does not generate any events.
7057 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007058
7059 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
7060 // the recreated touch state to generate a down event.
7061 processSync(mapper);
7062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7063 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
7064
7065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7066}
7067
lilinnan687e58f2022-07-19 16:00:50 +08007068TEST_F(SingleTouchInputMapperTest,
7069 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
7070 addConfigurationProperty("touch.deviceType", "touchScreen");
7071 prepareDisplay(DISPLAY_ORIENTATION_0);
7072 prepareButtons();
7073 prepareAxes(POSITION);
7074 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7075 NotifyMotionArgs motionArgs;
7076
7077 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00007078 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08007079 processSync(mapper);
7080
7081 // We should receive a down event
7082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7083 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7084
7085 // Change display id
7086 clearViewports();
7087 prepareSecondaryDisplay(ViewportType::INTERNAL);
7088
7089 // We should receive a cancel event
7090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7091 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7092 // Then receive reset called
7093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7094}
7095
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007096TEST_F(SingleTouchInputMapperTest,
7097 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
7098 addConfigurationProperty("touch.deviceType", "touchScreen");
7099 prepareDisplay(DISPLAY_ORIENTATION_0);
7100 prepareButtons();
7101 prepareAxes(POSITION);
7102 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7104 NotifyMotionArgs motionArgs;
7105
7106 // Start a new gesture.
7107 processDown(mapper, 100, 200);
7108 processSync(mapper);
7109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7110 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7111
7112 // Make the viewport inactive. This will put the device in disabled mode.
7113 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7114 viewport->isActive = false;
7115 mFakePolicy->updateViewport(*viewport);
7116 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7117
7118 // We should receive a cancel event for the ongoing gesture.
7119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7120 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7121 // Then we should be notified that the device was reset.
7122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7123
7124 // No events are generated while the viewport is inactive.
7125 processMove(mapper, 101, 201);
7126 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007127 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007128 processSync(mapper);
7129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7130
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007131 // Start a new gesture while the viewport is still inactive.
7132 processDown(mapper, 300, 400);
7133 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
7134 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
7135 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7136 processSync(mapper);
7137
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007138 // Make the viewport active again. The device should resume processing events.
7139 viewport->isActive = true;
7140 mFakePolicy->updateViewport(*viewport);
7141 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7142
7143 // The device is reset because it changes back to direct mode, without generating any events.
7144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7146
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007147 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007148 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7150 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007151
7152 // No more events.
7153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
7155}
7156
Prabir Pradhan5632d622021-09-06 07:57:20 -07007157// --- TouchDisplayProjectionTest ---
7158
7159class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
7160public:
7161 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
7162 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
7163 // rotated equivalent of the given un-rotated physical display bounds.
7164 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
7165 uint32_t inverseRotationFlags;
7166 auto width = DISPLAY_WIDTH;
7167 auto height = DISPLAY_HEIGHT;
7168 switch (orientation) {
7169 case DISPLAY_ORIENTATION_90:
7170 inverseRotationFlags = ui::Transform::ROT_270;
7171 std::swap(width, height);
7172 break;
7173 case DISPLAY_ORIENTATION_180:
7174 inverseRotationFlags = ui::Transform::ROT_180;
7175 break;
7176 case DISPLAY_ORIENTATION_270:
7177 inverseRotationFlags = ui::Transform::ROT_90;
7178 std::swap(width, height);
7179 break;
7180 case DISPLAY_ORIENTATION_0:
7181 inverseRotationFlags = ui::Transform::ROT_0;
7182 break;
7183 default:
7184 FAIL() << "Invalid orientation: " << orientation;
7185 }
7186
7187 const ui::Transform rotation(inverseRotationFlags, width, height);
7188 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
7189
7190 std::optional<DisplayViewport> internalViewport =
7191 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7192 DisplayViewport& v = *internalViewport;
7193 v.displayId = DISPLAY_ID;
7194 v.orientation = orientation;
7195
7196 v.logicalLeft = 0;
7197 v.logicalTop = 0;
7198 v.logicalRight = 100;
7199 v.logicalBottom = 100;
7200
7201 v.physicalLeft = rotatedPhysicalDisplay.left;
7202 v.physicalTop = rotatedPhysicalDisplay.top;
7203 v.physicalRight = rotatedPhysicalDisplay.right;
7204 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7205
7206 v.deviceWidth = width;
7207 v.deviceHeight = height;
7208
7209 v.isActive = true;
7210 v.uniqueId = UNIQUE_ID;
7211 v.type = ViewportType::INTERNAL;
7212 mFakePolicy->updateViewport(v);
7213 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7214 }
7215
7216 void assertReceivedMove(const Point& point) {
7217 NotifyMotionArgs motionArgs;
7218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7219 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7220 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7221 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7222 1, 0, 0, 0, 0, 0, 0, 0));
7223 }
7224};
7225
7226TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7227 addConfigurationProperty("touch.deviceType", "touchScreen");
7228 prepareDisplay(DISPLAY_ORIENTATION_0);
7229
7230 prepareButtons();
7231 prepareAxes(POSITION);
7232 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7233
7234 NotifyMotionArgs motionArgs;
7235
7236 // Configure the DisplayViewport such that the logical display maps to a subsection of
7237 // the display panel called the physical display. Here, the physical display is bounded by the
7238 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7239 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7240 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7241 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7242
7243 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7244 DISPLAY_ORIENTATION_270}) {
7245 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7246
7247 // Touches outside the physical display should be ignored, and should not generate any
7248 // events. Ensure touches at the following points that lie outside of the physical display
7249 // area do not generate any events.
7250 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7251 processDown(mapper, toRawX(point.x), toRawY(point.y));
7252 processSync(mapper);
7253 processUp(mapper);
7254 processSync(mapper);
7255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7256 << "Unexpected event generated for touch outside physical display at point: "
7257 << point.x << ", " << point.y;
7258 }
7259 }
7260}
7261
7262TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7263 addConfigurationProperty("touch.deviceType", "touchScreen");
7264 prepareDisplay(DISPLAY_ORIENTATION_0);
7265
7266 prepareButtons();
7267 prepareAxes(POSITION);
7268 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7269
7270 NotifyMotionArgs motionArgs;
7271
7272 // Configure the DisplayViewport such that the logical display maps to a subsection of
7273 // the display panel called the physical display. Here, the physical display is bounded by the
7274 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7275 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7276
7277 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7278 DISPLAY_ORIENTATION_270}) {
7279 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7280
7281 // Touches that start outside the physical display should be ignored until it enters the
7282 // physical display bounds, at which point it should generate a down event. Start a touch at
7283 // the point (5, 100), which is outside the physical display bounds.
7284 static const Point kOutsidePoint{5, 100};
7285 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7286 processSync(mapper);
7287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7288
7289 // Move the touch into the physical display area. This should generate a pointer down.
7290 processMove(mapper, toRawX(11), toRawY(21));
7291 processSync(mapper);
7292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7293 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7294 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7295 ASSERT_NO_FATAL_FAILURE(
7296 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7297
7298 // Move the touch inside the physical display area. This should generate a pointer move.
7299 processMove(mapper, toRawX(69), toRawY(159));
7300 processSync(mapper);
7301 assertReceivedMove({69, 159});
7302
7303 // Move outside the physical display area. Since the pointer is already down, this should
7304 // now continue generating events.
7305 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7306 processSync(mapper);
7307 assertReceivedMove(kOutsidePoint);
7308
7309 // Release. This should generate a pointer up.
7310 processUp(mapper);
7311 processSync(mapper);
7312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7313 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7314 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7315 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7316
7317 // Ensure no more events were generated.
7318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7320 }
7321}
7322
Michael Wrightd02c5b62014-02-10 15:10:22 -08007323// --- MultiTouchInputMapperTest ---
7324
7325class MultiTouchInputMapperTest : public TouchInputMapperTest {
7326protected:
7327 void prepareAxes(int axes);
7328
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007329 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7330 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7331 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7332 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7333 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7334 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7335 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7336 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7337 void processId(MultiTouchInputMapper& mapper, int32_t id);
7338 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7339 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7340 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
7341 void processMTSync(MultiTouchInputMapper& mapper);
7342 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007343};
7344
7345void MultiTouchInputMapperTest::prepareAxes(int axes) {
7346 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007347 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7348 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007349 }
7350 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007351 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7352 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007353 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007354 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7355 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007356 }
7357 }
7358 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007359 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7360 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007361 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007362 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007363 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007364 }
7365 }
7366 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007367 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7368 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007369 }
7370 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007371 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7372 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007373 }
7374 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007375 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7376 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007377 }
7378 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007379 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7380 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007381 }
7382 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007383 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7384 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007385 }
7386 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007387 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007388 }
7389}
7390
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007391void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7392 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007393 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7394 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007395}
7396
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007397void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7398 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007399 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007400}
7401
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007402void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7403 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007404 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007405}
7406
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007407void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007409}
7410
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007411void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007412 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007413}
7414
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007415void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7416 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007417 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007418}
7419
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007420void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007421 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007422}
7423
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007424void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007425 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007426}
7427
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007428void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007429 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007430}
7431
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007432void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007433 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007434}
7435
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007436void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007437 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007438}
7439
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007440void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7441 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007442 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007443}
7444
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007445void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007447}
7448
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007449void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007451}
7452
Michael Wrightd02c5b62014-02-10 15:10:22 -08007453TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007454 addConfigurationProperty("touch.deviceType", "touchScreen");
7455 prepareDisplay(DISPLAY_ORIENTATION_0);
7456 prepareAxes(POSITION);
7457 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007458 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007459
arthurhungdcef2dc2020-08-11 14:47:50 +08007460 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007461
7462 NotifyMotionArgs motionArgs;
7463
7464 // Two fingers down at once.
7465 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7466 processPosition(mapper, x1, y1);
7467 processMTSync(mapper);
7468 processPosition(mapper, x2, y2);
7469 processMTSync(mapper);
7470 processSync(mapper);
7471
7472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7473 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7474 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7475 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7476 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7477 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7478 ASSERT_EQ(0, motionArgs.flags);
7479 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7480 ASSERT_EQ(0, motionArgs.buttonState);
7481 ASSERT_EQ(0, motionArgs.edgeFlags);
7482 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7483 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7484 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7486 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7487 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7488 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7489 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7490
7491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7492 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7493 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7494 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7495 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007496 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007497 ASSERT_EQ(0, motionArgs.flags);
7498 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7499 ASSERT_EQ(0, motionArgs.buttonState);
7500 ASSERT_EQ(0, motionArgs.edgeFlags);
7501 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7502 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7503 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7504 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7505 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7506 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7507 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7509 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7510 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7511 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7512 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7513
7514 // Move.
7515 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7516 processPosition(mapper, x1, y1);
7517 processMTSync(mapper);
7518 processPosition(mapper, x2, y2);
7519 processMTSync(mapper);
7520 processSync(mapper);
7521
7522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7523 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7524 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7525 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7526 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7527 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7528 ASSERT_EQ(0, motionArgs.flags);
7529 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7530 ASSERT_EQ(0, motionArgs.buttonState);
7531 ASSERT_EQ(0, motionArgs.edgeFlags);
7532 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7533 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7534 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7535 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7536 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7537 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7538 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7540 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7541 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7542 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7543 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7544
7545 // First finger up.
7546 x2 += 15; y2 -= 20;
7547 processPosition(mapper, x2, y2);
7548 processMTSync(mapper);
7549 processSync(mapper);
7550
7551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7552 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7553 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7554 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7555 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007556 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007557 ASSERT_EQ(0, motionArgs.flags);
7558 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7559 ASSERT_EQ(0, motionArgs.buttonState);
7560 ASSERT_EQ(0, motionArgs.edgeFlags);
7561 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7562 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7563 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7564 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7565 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7567 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7568 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7569 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7570 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7571 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7572 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7573
7574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7575 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7576 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7577 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7578 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7579 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7580 ASSERT_EQ(0, motionArgs.flags);
7581 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7582 ASSERT_EQ(0, motionArgs.buttonState);
7583 ASSERT_EQ(0, motionArgs.edgeFlags);
7584 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7585 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7586 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7588 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7589 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7590 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7591 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7592
7593 // Move.
7594 x2 += 20; y2 -= 25;
7595 processPosition(mapper, x2, y2);
7596 processMTSync(mapper);
7597 processSync(mapper);
7598
7599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7600 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7601 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7602 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7603 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7604 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7605 ASSERT_EQ(0, motionArgs.flags);
7606 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7607 ASSERT_EQ(0, motionArgs.buttonState);
7608 ASSERT_EQ(0, motionArgs.edgeFlags);
7609 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7610 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7611 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7613 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7614 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7615 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7616 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7617
7618 // New finger down.
7619 int32_t x3 = 700, y3 = 300;
7620 processPosition(mapper, x2, y2);
7621 processMTSync(mapper);
7622 processPosition(mapper, x3, y3);
7623 processMTSync(mapper);
7624 processSync(mapper);
7625
7626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7627 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7628 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7629 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7630 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007631 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007632 ASSERT_EQ(0, motionArgs.flags);
7633 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7634 ASSERT_EQ(0, motionArgs.buttonState);
7635 ASSERT_EQ(0, motionArgs.edgeFlags);
7636 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7637 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7638 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7639 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7640 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7641 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7642 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7643 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7644 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7645 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7646 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7647 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7648
7649 // Second finger up.
7650 x3 += 30; y3 -= 20;
7651 processPosition(mapper, x3, y3);
7652 processMTSync(mapper);
7653 processSync(mapper);
7654
7655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7656 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7657 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7658 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7659 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007660 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007661 ASSERT_EQ(0, motionArgs.flags);
7662 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7663 ASSERT_EQ(0, motionArgs.buttonState);
7664 ASSERT_EQ(0, motionArgs.edgeFlags);
7665 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7666 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7667 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7668 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7669 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7671 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7672 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7673 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7674 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7675 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7676 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7677
7678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7679 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7680 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7681 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7682 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7683 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7684 ASSERT_EQ(0, motionArgs.flags);
7685 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7686 ASSERT_EQ(0, motionArgs.buttonState);
7687 ASSERT_EQ(0, motionArgs.edgeFlags);
7688 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7689 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7690 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7691 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7692 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7693 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7694 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7695 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7696
7697 // Last finger up.
7698 processMTSync(mapper);
7699 processSync(mapper);
7700
7701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7702 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7703 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7704 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7705 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7706 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7707 ASSERT_EQ(0, motionArgs.flags);
7708 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7709 ASSERT_EQ(0, motionArgs.buttonState);
7710 ASSERT_EQ(0, motionArgs.edgeFlags);
7711 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7712 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7713 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7714 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7715 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7716 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7717 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7718 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7719
7720 // Should not have sent any more keys or motions.
7721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7723}
7724
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007725TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7726 addConfigurationProperty("touch.deviceType", "touchScreen");
7727 prepareDisplay(DISPLAY_ORIENTATION_0);
7728
7729 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7730 /*fuzz*/ 0, /*resolution*/ 10);
7731 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7732 /*fuzz*/ 0, /*resolution*/ 11);
7733 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7734 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7735 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7736 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7737 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7738 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7739 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7740 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7741
7742 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7743
7744 // X and Y axes
7745 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7746 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7747 // Touch major and minor
7748 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7749 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7750 // Tool major and minor
7751 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7752 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7753}
7754
7755TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7756 addConfigurationProperty("touch.deviceType", "touchScreen");
7757 prepareDisplay(DISPLAY_ORIENTATION_0);
7758
7759 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7760 /*fuzz*/ 0, /*resolution*/ 10);
7761 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7762 /*fuzz*/ 0, /*resolution*/ 11);
7763
7764 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7765
7766 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7767
7768 // Touch major and minor
7769 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7770 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7771 // Tool major and minor
7772 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7773 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7774}
7775
Michael Wrightd02c5b62014-02-10 15:10:22 -08007776TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007777 addConfigurationProperty("touch.deviceType", "touchScreen");
7778 prepareDisplay(DISPLAY_ORIENTATION_0);
7779 prepareAxes(POSITION | ID);
7780 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007781 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007782
arthurhungdcef2dc2020-08-11 14:47:50 +08007783 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007784
7785 NotifyMotionArgs motionArgs;
7786
7787 // Two fingers down at once.
7788 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7789 processPosition(mapper, x1, y1);
7790 processId(mapper, 1);
7791 processMTSync(mapper);
7792 processPosition(mapper, x2, y2);
7793 processId(mapper, 2);
7794 processMTSync(mapper);
7795 processSync(mapper);
7796
7797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7798 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7799 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7800 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7801 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7803 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7804
7805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007806 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007807 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7808 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7809 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7810 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7811 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7812 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7813 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7814 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7815 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7816
7817 // Move.
7818 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7819 processPosition(mapper, x1, y1);
7820 processId(mapper, 1);
7821 processMTSync(mapper);
7822 processPosition(mapper, x2, y2);
7823 processId(mapper, 2);
7824 processMTSync(mapper);
7825 processSync(mapper);
7826
7827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7828 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7829 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7830 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7831 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7832 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7833 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7834 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7835 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7836 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7837 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7838
7839 // First finger up.
7840 x2 += 15; y2 -= 20;
7841 processPosition(mapper, x2, y2);
7842 processId(mapper, 2);
7843 processMTSync(mapper);
7844 processSync(mapper);
7845
7846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007847 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007848 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7849 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7850 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7851 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7852 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7853 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7854 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7855 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7856 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7857
7858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7859 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7860 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7861 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7862 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7863 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7864 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7865
7866 // Move.
7867 x2 += 20; y2 -= 25;
7868 processPosition(mapper, x2, y2);
7869 processId(mapper, 2);
7870 processMTSync(mapper);
7871 processSync(mapper);
7872
7873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7874 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7875 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7876 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7877 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7878 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7879 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7880
7881 // New finger down.
7882 int32_t x3 = 700, y3 = 300;
7883 processPosition(mapper, x2, y2);
7884 processId(mapper, 2);
7885 processMTSync(mapper);
7886 processPosition(mapper, x3, y3);
7887 processId(mapper, 3);
7888 processMTSync(mapper);
7889 processSync(mapper);
7890
7891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007892 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007893 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7894 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7895 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7896 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7897 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7899 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7900 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7901 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7902
7903 // Second finger up.
7904 x3 += 30; y3 -= 20;
7905 processPosition(mapper, x3, y3);
7906 processId(mapper, 3);
7907 processMTSync(mapper);
7908 processSync(mapper);
7909
7910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007911 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007912 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7913 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7914 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7915 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7916 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7917 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7918 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7920 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7921
7922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7923 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7924 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7925 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7926 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7927 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7928 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7929
7930 // Last finger up.
7931 processMTSync(mapper);
7932 processSync(mapper);
7933
7934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7935 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7936 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7937 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7938 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7939 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7940 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7941
7942 // Should not have sent any more keys or motions.
7943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7945}
7946
7947TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007948 addConfigurationProperty("touch.deviceType", "touchScreen");
7949 prepareDisplay(DISPLAY_ORIENTATION_0);
7950 prepareAxes(POSITION | ID | SLOT);
7951 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007952 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007953
arthurhungdcef2dc2020-08-11 14:47:50 +08007954 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007955
7956 NotifyMotionArgs motionArgs;
7957
7958 // Two fingers down at once.
7959 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7960 processPosition(mapper, x1, y1);
7961 processId(mapper, 1);
7962 processSlot(mapper, 1);
7963 processPosition(mapper, x2, y2);
7964 processId(mapper, 2);
7965 processSync(mapper);
7966
7967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7968 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7969 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7970 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7971 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7972 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7973 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7974
7975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007976 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007977 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7978 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7979 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7980 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7981 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7982 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7983 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7984 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7985 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7986
7987 // Move.
7988 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7989 processSlot(mapper, 0);
7990 processPosition(mapper, x1, y1);
7991 processSlot(mapper, 1);
7992 processPosition(mapper, x2, y2);
7993 processSync(mapper);
7994
7995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7996 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7997 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7998 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7999 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8000 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8001 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8002 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8003 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8004 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8005 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8006
8007 // First finger up.
8008 x2 += 15; y2 -= 20;
8009 processSlot(mapper, 0);
8010 processId(mapper, -1);
8011 processSlot(mapper, 1);
8012 processPosition(mapper, x2, y2);
8013 processSync(mapper);
8014
8015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008016 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008017 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8018 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8019 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8020 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8021 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8022 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8023 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8024 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8025 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8026
8027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8028 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8029 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8030 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8031 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8033 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8034
8035 // Move.
8036 x2 += 20; y2 -= 25;
8037 processPosition(mapper, x2, y2);
8038 processSync(mapper);
8039
8040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8041 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8042 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8043 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8044 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8045 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8046 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8047
8048 // New finger down.
8049 int32_t x3 = 700, y3 = 300;
8050 processPosition(mapper, x2, y2);
8051 processSlot(mapper, 0);
8052 processId(mapper, 3);
8053 processPosition(mapper, x3, y3);
8054 processSync(mapper);
8055
8056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008057 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008058 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8059 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8060 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8061 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8062 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8063 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8064 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8065 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8066 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8067
8068 // Second finger up.
8069 x3 += 30; y3 -= 20;
8070 processSlot(mapper, 1);
8071 processId(mapper, -1);
8072 processSlot(mapper, 0);
8073 processPosition(mapper, x3, y3);
8074 processSync(mapper);
8075
8076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008077 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008078 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8079 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8080 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8081 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8082 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8083 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8084 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8085 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8086 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8087
8088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8089 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8090 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8091 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8092 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8093 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8094 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8095
8096 // Last finger up.
8097 processId(mapper, -1);
8098 processSync(mapper);
8099
8100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8101 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8102 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8103 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8104 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8105 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8106 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8107
8108 // Should not have sent any more keys or motions.
8109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8111}
8112
8113TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008114 addConfigurationProperty("touch.deviceType", "touchScreen");
8115 prepareDisplay(DISPLAY_ORIENTATION_0);
8116 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008117 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008118
8119 // These calculations are based on the input device calibration documentation.
8120 int32_t rawX = 100;
8121 int32_t rawY = 200;
8122 int32_t rawTouchMajor = 7;
8123 int32_t rawTouchMinor = 6;
8124 int32_t rawToolMajor = 9;
8125 int32_t rawToolMinor = 8;
8126 int32_t rawPressure = 11;
8127 int32_t rawDistance = 0;
8128 int32_t rawOrientation = 3;
8129 int32_t id = 5;
8130
8131 float x = toDisplayX(rawX);
8132 float y = toDisplayY(rawY);
8133 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8134 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8135 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8136 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8137 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8138 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8139 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8140 float distance = float(rawDistance);
8141
8142 processPosition(mapper, rawX, rawY);
8143 processTouchMajor(mapper, rawTouchMajor);
8144 processTouchMinor(mapper, rawTouchMinor);
8145 processToolMajor(mapper, rawToolMajor);
8146 processToolMinor(mapper, rawToolMinor);
8147 processPressure(mapper, rawPressure);
8148 processOrientation(mapper, rawOrientation);
8149 processDistance(mapper, rawDistance);
8150 processId(mapper, id);
8151 processMTSync(mapper);
8152 processSync(mapper);
8153
8154 NotifyMotionArgs args;
8155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8156 ASSERT_EQ(0, args.pointerProperties[0].id);
8157 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8158 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8159 orientation, distance));
8160}
8161
8162TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008163 addConfigurationProperty("touch.deviceType", "touchScreen");
8164 prepareDisplay(DISPLAY_ORIENTATION_0);
8165 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8166 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008167 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008168
8169 // These calculations are based on the input device calibration documentation.
8170 int32_t rawX = 100;
8171 int32_t rawY = 200;
8172 int32_t rawTouchMajor = 140;
8173 int32_t rawTouchMinor = 120;
8174 int32_t rawToolMajor = 180;
8175 int32_t rawToolMinor = 160;
8176
8177 float x = toDisplayX(rawX);
8178 float y = toDisplayY(rawY);
8179 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8180 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8181 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8182 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8183 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8184
8185 processPosition(mapper, rawX, rawY);
8186 processTouchMajor(mapper, rawTouchMajor);
8187 processTouchMinor(mapper, rawTouchMinor);
8188 processToolMajor(mapper, rawToolMajor);
8189 processToolMinor(mapper, rawToolMinor);
8190 processMTSync(mapper);
8191 processSync(mapper);
8192
8193 NotifyMotionArgs args;
8194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8195 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8196 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8197}
8198
8199TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008200 addConfigurationProperty("touch.deviceType", "touchScreen");
8201 prepareDisplay(DISPLAY_ORIENTATION_0);
8202 prepareAxes(POSITION | TOUCH | TOOL);
8203 addConfigurationProperty("touch.size.calibration", "diameter");
8204 addConfigurationProperty("touch.size.scale", "10");
8205 addConfigurationProperty("touch.size.bias", "160");
8206 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008207 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008208
8209 // These calculations are based on the input device calibration documentation.
8210 // Note: We only provide a single common touch/tool value because the device is assumed
8211 // not to emit separate values for each pointer (isSummed = 1).
8212 int32_t rawX = 100;
8213 int32_t rawY = 200;
8214 int32_t rawX2 = 150;
8215 int32_t rawY2 = 250;
8216 int32_t rawTouchMajor = 5;
8217 int32_t rawToolMajor = 8;
8218
8219 float x = toDisplayX(rawX);
8220 float y = toDisplayY(rawY);
8221 float x2 = toDisplayX(rawX2);
8222 float y2 = toDisplayY(rawY2);
8223 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8224 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8225 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8226
8227 processPosition(mapper, rawX, rawY);
8228 processTouchMajor(mapper, rawTouchMajor);
8229 processToolMajor(mapper, rawToolMajor);
8230 processMTSync(mapper);
8231 processPosition(mapper, rawX2, rawY2);
8232 processTouchMajor(mapper, rawTouchMajor);
8233 processToolMajor(mapper, rawToolMajor);
8234 processMTSync(mapper);
8235 processSync(mapper);
8236
8237 NotifyMotionArgs args;
8238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8239 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8240
8241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008242 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008243 ASSERT_EQ(size_t(2), args.pointerCount);
8244 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8245 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8246 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8247 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8248}
8249
8250TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008251 addConfigurationProperty("touch.deviceType", "touchScreen");
8252 prepareDisplay(DISPLAY_ORIENTATION_0);
8253 prepareAxes(POSITION | TOUCH | TOOL);
8254 addConfigurationProperty("touch.size.calibration", "area");
8255 addConfigurationProperty("touch.size.scale", "43");
8256 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008257 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008258
8259 // These calculations are based on the input device calibration documentation.
8260 int32_t rawX = 100;
8261 int32_t rawY = 200;
8262 int32_t rawTouchMajor = 5;
8263 int32_t rawToolMajor = 8;
8264
8265 float x = toDisplayX(rawX);
8266 float y = toDisplayY(rawY);
8267 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8268 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8269 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8270
8271 processPosition(mapper, rawX, rawY);
8272 processTouchMajor(mapper, rawTouchMajor);
8273 processToolMajor(mapper, rawToolMajor);
8274 processMTSync(mapper);
8275 processSync(mapper);
8276
8277 NotifyMotionArgs args;
8278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8279 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8280 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8281}
8282
8283TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008284 addConfigurationProperty("touch.deviceType", "touchScreen");
8285 prepareDisplay(DISPLAY_ORIENTATION_0);
8286 prepareAxes(POSITION | PRESSURE);
8287 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8288 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008289 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008290
Michael Wrightaa449c92017-12-13 21:21:43 +00008291 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008292 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008293 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8294 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8295 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8296
Michael Wrightd02c5b62014-02-10 15:10:22 -08008297 // These calculations are based on the input device calibration documentation.
8298 int32_t rawX = 100;
8299 int32_t rawY = 200;
8300 int32_t rawPressure = 60;
8301
8302 float x = toDisplayX(rawX);
8303 float y = toDisplayY(rawY);
8304 float pressure = float(rawPressure) * 0.01f;
8305
8306 processPosition(mapper, rawX, rawY);
8307 processPressure(mapper, rawPressure);
8308 processMTSync(mapper);
8309 processSync(mapper);
8310
8311 NotifyMotionArgs args;
8312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8313 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8314 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8315}
8316
8317TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008318 addConfigurationProperty("touch.deviceType", "touchScreen");
8319 prepareDisplay(DISPLAY_ORIENTATION_0);
8320 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008321 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008322
8323 NotifyMotionArgs motionArgs;
8324 NotifyKeyArgs keyArgs;
8325
8326 processId(mapper, 1);
8327 processPosition(mapper, 100, 200);
8328 processSync(mapper);
8329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8330 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8331 ASSERT_EQ(0, motionArgs.buttonState);
8332
8333 // press BTN_LEFT, release BTN_LEFT
8334 processKey(mapper, BTN_LEFT, 1);
8335 processSync(mapper);
8336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8337 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8338 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8339
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8341 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8342 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8343
Michael Wrightd02c5b62014-02-10 15:10:22 -08008344 processKey(mapper, BTN_LEFT, 0);
8345 processSync(mapper);
8346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008347 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008348 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008349
8350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008351 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008352 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008353
8354 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8355 processKey(mapper, BTN_RIGHT, 1);
8356 processKey(mapper, BTN_MIDDLE, 1);
8357 processSync(mapper);
8358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8359 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8360 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8361 motionArgs.buttonState);
8362
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8364 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8365 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8366
8367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8368 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8369 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8370 motionArgs.buttonState);
8371
Michael Wrightd02c5b62014-02-10 15:10:22 -08008372 processKey(mapper, BTN_RIGHT, 0);
8373 processSync(mapper);
8374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008375 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008376 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008377
8378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008379 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008380 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008381
8382 processKey(mapper, BTN_MIDDLE, 0);
8383 processSync(mapper);
8384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008385 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008386 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008387
8388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008390 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008391
8392 // press BTN_BACK, release BTN_BACK
8393 processKey(mapper, BTN_BACK, 1);
8394 processSync(mapper);
8395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8396 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8397 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008398
Michael Wrightd02c5b62014-02-10 15:10:22 -08008399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008400 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008401 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8402
8403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8404 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8405 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008406
8407 processKey(mapper, BTN_BACK, 0);
8408 processSync(mapper);
8409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008410 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008411 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008412
8413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008414 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008415 ASSERT_EQ(0, motionArgs.buttonState);
8416
Michael Wrightd02c5b62014-02-10 15:10:22 -08008417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8418 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8419 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8420
8421 // press BTN_SIDE, release BTN_SIDE
8422 processKey(mapper, BTN_SIDE, 1);
8423 processSync(mapper);
8424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8425 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8426 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008427
Michael Wrightd02c5b62014-02-10 15:10:22 -08008428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008429 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008430 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8431
8432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8433 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8434 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008435
8436 processKey(mapper, BTN_SIDE, 0);
8437 processSync(mapper);
8438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008439 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008440 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008441
8442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008443 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008444 ASSERT_EQ(0, motionArgs.buttonState);
8445
Michael Wrightd02c5b62014-02-10 15:10:22 -08008446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8447 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8448 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8449
8450 // press BTN_FORWARD, release BTN_FORWARD
8451 processKey(mapper, BTN_FORWARD, 1);
8452 processSync(mapper);
8453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8454 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8455 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008456
Michael Wrightd02c5b62014-02-10 15:10:22 -08008457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008458 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008459 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8460
8461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8462 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8463 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008464
8465 processKey(mapper, BTN_FORWARD, 0);
8466 processSync(mapper);
8467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008468 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008469 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008470
8471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008472 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008473 ASSERT_EQ(0, motionArgs.buttonState);
8474
Michael Wrightd02c5b62014-02-10 15:10:22 -08008475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8476 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8477 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8478
8479 // press BTN_EXTRA, release BTN_EXTRA
8480 processKey(mapper, BTN_EXTRA, 1);
8481 processSync(mapper);
8482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8483 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8484 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008485
Michael Wrightd02c5b62014-02-10 15:10:22 -08008486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008487 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008488 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8489
8490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8491 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8492 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008493
8494 processKey(mapper, BTN_EXTRA, 0);
8495 processSync(mapper);
8496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008497 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008498 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008499
8500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008501 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008502 ASSERT_EQ(0, motionArgs.buttonState);
8503
Michael Wrightd02c5b62014-02-10 15:10:22 -08008504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8505 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8506 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8507
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8509
Michael Wrightd02c5b62014-02-10 15:10:22 -08008510 // press BTN_STYLUS, release BTN_STYLUS
8511 processKey(mapper, BTN_STYLUS, 1);
8512 processSync(mapper);
8513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8514 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008515 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8516
8517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8518 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8519 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008520
8521 processKey(mapper, BTN_STYLUS, 0);
8522 processSync(mapper);
8523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008524 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008525 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008526
8527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008528 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008529 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008530
8531 // press BTN_STYLUS2, release BTN_STYLUS2
8532 processKey(mapper, BTN_STYLUS2, 1);
8533 processSync(mapper);
8534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8535 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008536 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8537
8538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8539 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8540 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008541
8542 processKey(mapper, BTN_STYLUS2, 0);
8543 processSync(mapper);
8544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008545 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008546 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008547
8548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008549 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008550 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008551
8552 // release touch
8553 processId(mapper, -1);
8554 processSync(mapper);
8555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8556 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8557 ASSERT_EQ(0, motionArgs.buttonState);
8558}
8559
8560TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008561 addConfigurationProperty("touch.deviceType", "touchScreen");
8562 prepareDisplay(DISPLAY_ORIENTATION_0);
8563 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008564 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008565
8566 NotifyMotionArgs motionArgs;
8567
8568 // default tool type is finger
8569 processId(mapper, 1);
8570 processPosition(mapper, 100, 200);
8571 processSync(mapper);
8572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8573 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8574 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8575
8576 // eraser
8577 processKey(mapper, BTN_TOOL_RUBBER, 1);
8578 processSync(mapper);
8579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8580 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8581 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8582
8583 // stylus
8584 processKey(mapper, BTN_TOOL_RUBBER, 0);
8585 processKey(mapper, BTN_TOOL_PEN, 1);
8586 processSync(mapper);
8587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8588 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8589 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8590
8591 // brush
8592 processKey(mapper, BTN_TOOL_PEN, 0);
8593 processKey(mapper, BTN_TOOL_BRUSH, 1);
8594 processSync(mapper);
8595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8596 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8597 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8598
8599 // pencil
8600 processKey(mapper, BTN_TOOL_BRUSH, 0);
8601 processKey(mapper, BTN_TOOL_PENCIL, 1);
8602 processSync(mapper);
8603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8604 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8605 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8606
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008607 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008608 processKey(mapper, BTN_TOOL_PENCIL, 0);
8609 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8610 processSync(mapper);
8611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8612 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8613 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8614
8615 // mouse
8616 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8617 processKey(mapper, BTN_TOOL_MOUSE, 1);
8618 processSync(mapper);
8619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8620 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8621 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8622
8623 // lens
8624 processKey(mapper, BTN_TOOL_MOUSE, 0);
8625 processKey(mapper, BTN_TOOL_LENS, 1);
8626 processSync(mapper);
8627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8628 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8629 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8630
8631 // double-tap
8632 processKey(mapper, BTN_TOOL_LENS, 0);
8633 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8634 processSync(mapper);
8635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8636 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8637 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8638
8639 // triple-tap
8640 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8641 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8642 processSync(mapper);
8643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8645 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8646
8647 // quad-tap
8648 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8649 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8650 processSync(mapper);
8651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8652 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8653 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8654
8655 // finger
8656 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8657 processKey(mapper, BTN_TOOL_FINGER, 1);
8658 processSync(mapper);
8659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8660 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8661 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8662
8663 // stylus trumps finger
8664 processKey(mapper, BTN_TOOL_PEN, 1);
8665 processSync(mapper);
8666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8667 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8668 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8669
8670 // eraser trumps stylus
8671 processKey(mapper, BTN_TOOL_RUBBER, 1);
8672 processSync(mapper);
8673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8674 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8675 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8676
8677 // mouse trumps eraser
8678 processKey(mapper, BTN_TOOL_MOUSE, 1);
8679 processSync(mapper);
8680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8681 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8682 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8683
8684 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8685 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8686 processSync(mapper);
8687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8688 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8689 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8690
8691 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8692 processToolType(mapper, MT_TOOL_PEN);
8693 processSync(mapper);
8694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8695 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8696 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8697
8698 // back to default tool type
8699 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8700 processKey(mapper, BTN_TOOL_MOUSE, 0);
8701 processKey(mapper, BTN_TOOL_RUBBER, 0);
8702 processKey(mapper, BTN_TOOL_PEN, 0);
8703 processKey(mapper, BTN_TOOL_FINGER, 0);
8704 processSync(mapper);
8705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8706 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8707 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8708}
8709
8710TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008711 addConfigurationProperty("touch.deviceType", "touchScreen");
8712 prepareDisplay(DISPLAY_ORIENTATION_0);
8713 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008714 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008715 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008716
8717 NotifyMotionArgs motionArgs;
8718
8719 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8720 processId(mapper, 1);
8721 processPosition(mapper, 100, 200);
8722 processSync(mapper);
8723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8724 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8725 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8726 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8727
8728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8729 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8730 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8731 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8732
8733 // move a little
8734 processPosition(mapper, 150, 250);
8735 processSync(mapper);
8736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8737 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8738 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8739 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8740
8741 // down when BTN_TOUCH is pressed, pressure defaults to 1
8742 processKey(mapper, BTN_TOUCH, 1);
8743 processSync(mapper);
8744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8745 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8746 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8747 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8748
8749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8750 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8752 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8753
8754 // up when BTN_TOUCH is released, hover restored
8755 processKey(mapper, BTN_TOUCH, 0);
8756 processSync(mapper);
8757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8758 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8760 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8761
8762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8763 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8764 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8765 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8766
8767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8768 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8769 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8770 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8771
8772 // exit hover when pointer goes away
8773 processId(mapper, -1);
8774 processSync(mapper);
8775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8776 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8778 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8779}
8780
8781TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008782 addConfigurationProperty("touch.deviceType", "touchScreen");
8783 prepareDisplay(DISPLAY_ORIENTATION_0);
8784 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008785 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008786
8787 NotifyMotionArgs motionArgs;
8788
8789 // initially hovering because pressure is 0
8790 processId(mapper, 1);
8791 processPosition(mapper, 100, 200);
8792 processPressure(mapper, 0);
8793 processSync(mapper);
8794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8795 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8796 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8797 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8798
8799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8800 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8801 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8802 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8803
8804 // move a little
8805 processPosition(mapper, 150, 250);
8806 processSync(mapper);
8807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8808 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8809 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8810 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8811
8812 // down when pressure becomes non-zero
8813 processPressure(mapper, RAW_PRESSURE_MAX);
8814 processSync(mapper);
8815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8816 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8817 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8818 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8819
8820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8821 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8822 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8823 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8824
8825 // up when pressure becomes 0, hover restored
8826 processPressure(mapper, 0);
8827 processSync(mapper);
8828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8829 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8830 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8831 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8832
8833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8834 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8836 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8837
8838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8839 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8840 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8841 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8842
8843 // exit hover when pointer goes away
8844 processId(mapper, -1);
8845 processSync(mapper);
8846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8847 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8848 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8849 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8850}
8851
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008852/**
8853 * Set the input device port <--> display port associations, and check that the
8854 * events are routed to the display that matches the display port.
8855 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8856 */
8857TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008858 const std::string usb2 = "USB2";
8859 const uint8_t hdmi1 = 0;
8860 const uint8_t hdmi2 = 1;
8861 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008862 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008863
8864 addConfigurationProperty("touch.deviceType", "touchScreen");
8865 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008866 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008867
8868 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8869 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8870
8871 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8872 // for this input device is specified, and the matching viewport is not present,
8873 // the input device should be disabled (at the mapper level).
8874
8875 // Add viewport for display 2 on hdmi2
8876 prepareSecondaryDisplay(type, hdmi2);
8877 // Send a touch event
8878 processPosition(mapper, 100, 100);
8879 processSync(mapper);
8880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8881
8882 // Add viewport for display 1 on hdmi1
8883 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8884 // Send a touch event again
8885 processPosition(mapper, 100, 100);
8886 processSync(mapper);
8887
8888 NotifyMotionArgs args;
8889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8890 ASSERT_EQ(DISPLAY_ID, args.displayId);
8891}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008892
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008893TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8894 addConfigurationProperty("touch.deviceType", "touchScreen");
8895 prepareAxes(POSITION);
8896 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8897
8898 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8899
8900 prepareDisplay(DISPLAY_ORIENTATION_0);
8901 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8902
8903 // Send a touch event
8904 processPosition(mapper, 100, 100);
8905 processSync(mapper);
8906
8907 NotifyMotionArgs args;
8908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8909 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8910}
8911
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008912TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008913 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008914 std::shared_ptr<FakePointerController> fakePointerController =
8915 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008916 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008917 fakePointerController->setPosition(100, 200);
8918 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008919 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008920
Garfield Tan888a6a42020-01-09 11:39:16 -08008921 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008922 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008923
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008924 prepareDisplay(DISPLAY_ORIENTATION_0);
8925 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008926 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008927
8928 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008929 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008930
8931 NotifyMotionArgs motionArgs;
8932 processPosition(mapper, 100, 100);
8933 processSync(mapper);
8934
8935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8936 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8937 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8938}
8939
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008940/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008941 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8942 */
8943TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8944 addConfigurationProperty("touch.deviceType", "touchScreen");
8945 prepareAxes(POSITION);
8946 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8947
8948 prepareDisplay(DISPLAY_ORIENTATION_0);
8949 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8950 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8951 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8952 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8953
8954 NotifyMotionArgs args;
8955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8956 ASSERT_EQ(26, args.readTime);
8957
8958 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8959 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8960 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8961
8962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8963 ASSERT_EQ(33, args.readTime);
8964}
8965
8966/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008967 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8968 * events should not be delivered to the listener.
8969 */
8970TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8971 addConfigurationProperty("touch.deviceType", "touchScreen");
8972 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8973 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8974 ViewportType::INTERNAL);
8975 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8976 prepareAxes(POSITION);
8977 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8978
8979 NotifyMotionArgs motionArgs;
8980 processPosition(mapper, 100, 100);
8981 processSync(mapper);
8982
8983 mFakeListener->assertNotifyMotionWasNotCalled();
8984}
8985
Garfield Tanc734e4f2021-01-15 20:01:39 -08008986TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8987 addConfigurationProperty("touch.deviceType", "touchScreen");
8988 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8989 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8990 ViewportType::INTERNAL);
8991 std::optional<DisplayViewport> optionalDisplayViewport =
8992 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8993 ASSERT_TRUE(optionalDisplayViewport.has_value());
8994 DisplayViewport displayViewport = *optionalDisplayViewport;
8995
8996 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8997 prepareAxes(POSITION);
8998 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8999
9000 // Finger down
9001 int32_t x = 100, y = 100;
9002 processPosition(mapper, x, y);
9003 processSync(mapper);
9004
9005 NotifyMotionArgs motionArgs;
9006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9007 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9008
9009 // Deactivate display viewport
9010 displayViewport.isActive = false;
9011 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9012 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9013
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009014 // The ongoing touch should be canceled immediately
9015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9016 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9017
9018 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009019 x += 10, y += 10;
9020 processPosition(mapper, x, y);
9021 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009023
9024 // Reactivate display viewport
9025 displayViewport.isActive = true;
9026 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9027 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9028
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009029 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009030 x += 10, y += 10;
9031 processPosition(mapper, x, y);
9032 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9034 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009035}
9036
Arthur Hung7c645402019-01-25 17:45:42 +08009037TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9038 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009039 prepareAxes(POSITION | ID | SLOT);
9040 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009041 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009042
9043 // Create the second touch screen device, and enable multi fingers.
9044 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009045 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009046 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009047 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009048 std::shared_ptr<InputDevice> device2 =
9049 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009050 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009051
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009052 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9053 0 /*flat*/, 0 /*fuzz*/);
9054 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9055 0 /*flat*/, 0 /*fuzz*/);
9056 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
9057 0 /*flat*/, 0 /*fuzz*/);
9058 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
9059 0 /*flat*/, 0 /*fuzz*/);
9060 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
9061 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9062 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009063
9064 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009065 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009066 std::list<NotifyArgs> unused =
9067 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9068 0 /*changes*/);
9069 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009070
9071 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009072 std::shared_ptr<FakePointerController> fakePointerController =
9073 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009074 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009075
9076 // Setup policy for associated displays and show touches.
9077 const uint8_t hdmi1 = 0;
9078 const uint8_t hdmi2 = 1;
9079 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9080 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9081 mFakePolicy->setShowTouches(true);
9082
9083 // Create displays.
9084 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009085 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009086
9087 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009088 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9089 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
9090 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009091
9092 // Two fingers down at default display.
9093 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9094 processPosition(mapper, x1, y1);
9095 processId(mapper, 1);
9096 processSlot(mapper, 1);
9097 processPosition(mapper, x2, y2);
9098 processId(mapper, 2);
9099 processSync(mapper);
9100
9101 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9102 fakePointerController->getSpots().find(DISPLAY_ID);
9103 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9104 ASSERT_EQ(size_t(2), iter->second.size());
9105
9106 // Two fingers down at second display.
9107 processPosition(mapper2, x1, y1);
9108 processId(mapper2, 1);
9109 processSlot(mapper2, 1);
9110 processPosition(mapper2, x2, y2);
9111 processId(mapper2, 2);
9112 processSync(mapper2);
9113
9114 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9115 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9116 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009117
9118 // Disable the show touches configuration and ensure the spots are cleared.
9119 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009120 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9121 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009122
9123 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009124}
9125
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009126TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009127 prepareAxes(POSITION);
9128 addConfigurationProperty("touch.deviceType", "touchScreen");
9129 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009130 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009131
9132 NotifyMotionArgs motionArgs;
9133 // Unrotated video frame
9134 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9135 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009136 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009137 processPosition(mapper, 100, 200);
9138 processSync(mapper);
9139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9140 ASSERT_EQ(frames, motionArgs.videoFrames);
9141
9142 // Subsequent touch events should not have any videoframes
9143 // This is implemented separately in FakeEventHub,
9144 // but that should match the behaviour of TouchVideoDevice.
9145 processPosition(mapper, 200, 200);
9146 processSync(mapper);
9147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9148 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9149}
9150
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009151TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009152 prepareAxes(POSITION);
9153 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009154 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009155 // Unrotated video frame
9156 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9157 NotifyMotionArgs motionArgs;
9158
9159 // Test all 4 orientations
9160 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009161 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
9162 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9163 clearViewports();
9164 prepareDisplay(orientation);
9165 std::vector<TouchVideoFrame> frames{frame};
9166 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9167 processPosition(mapper, 100, 200);
9168 processSync(mapper);
9169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9170 ASSERT_EQ(frames, motionArgs.videoFrames);
9171 }
9172}
9173
9174TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9175 prepareAxes(POSITION);
9176 addConfigurationProperty("touch.deviceType", "touchScreen");
9177 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9178 // orientation-aware are affected by display rotation.
9179 addConfigurationProperty("touch.orientationAware", "0");
9180 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9181 // Unrotated video frame
9182 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9183 NotifyMotionArgs motionArgs;
9184
9185 // Test all 4 orientations
9186 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009187 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
9188 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9189 clearViewports();
9190 prepareDisplay(orientation);
9191 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009192 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009193 processPosition(mapper, 100, 200);
9194 processSync(mapper);
9195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009196 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9197 // compared to the display. This is so that when the window transform (which contains the
9198 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9199 // window's coordinate space.
9200 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009201 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009202
9203 // Release finger.
9204 processSync(mapper);
9205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009206 }
9207}
9208
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009209TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009210 prepareAxes(POSITION);
9211 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009212 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009213 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9214 // so mix these.
9215 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9216 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9217 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9218 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9219 NotifyMotionArgs motionArgs;
9220
9221 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009222 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009223 processPosition(mapper, 100, 200);
9224 processSync(mapper);
9225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009226 ASSERT_EQ(frames, motionArgs.videoFrames);
9227}
9228
9229TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9230 prepareAxes(POSITION);
9231 addConfigurationProperty("touch.deviceType", "touchScreen");
9232 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9233 // orientation-aware are affected by display rotation.
9234 addConfigurationProperty("touch.orientationAware", "0");
9235 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9236 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9237 // so mix these.
9238 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9239 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9240 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9241 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9242 NotifyMotionArgs motionArgs;
9243
9244 prepareDisplay(DISPLAY_ORIENTATION_90);
9245 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9246 processPosition(mapper, 100, 200);
9247 processSync(mapper);
9248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9249 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9250 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9251 // compared to the display. This is so that when the window transform (which contains the
9252 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9253 // window's coordinate space.
9254 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
9255 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009256 ASSERT_EQ(frames, motionArgs.videoFrames);
9257}
9258
Arthur Hung9da14732019-09-02 16:16:58 +08009259/**
9260 * If we had defined port associations, but the viewport is not ready, the touch device would be
9261 * expected to be disabled, and it should be enabled after the viewport has found.
9262 */
9263TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009264 constexpr uint8_t hdmi2 = 1;
9265 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009266 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009267
9268 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9269
9270 addConfigurationProperty("touch.deviceType", "touchScreen");
9271 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009272 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009273
9274 ASSERT_EQ(mDevice->isEnabled(), false);
9275
9276 // Add display on hdmi2, the device should be enabled and can receive touch event.
9277 prepareSecondaryDisplay(type, hdmi2);
9278 ASSERT_EQ(mDevice->isEnabled(), true);
9279
9280 // Send a touch event.
9281 processPosition(mapper, 100, 100);
9282 processSync(mapper);
9283
9284 NotifyMotionArgs args;
9285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9286 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9287}
9288
Arthur Hung421eb1c2020-01-16 00:09:42 +08009289TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009290 addConfigurationProperty("touch.deviceType", "touchScreen");
9291 prepareDisplay(DISPLAY_ORIENTATION_0);
9292 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009293 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009294
9295 NotifyMotionArgs motionArgs;
9296
9297 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9298 // finger down
9299 processId(mapper, 1);
9300 processPosition(mapper, x1, y1);
9301 processSync(mapper);
9302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9303 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9304 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9305
9306 // finger move
9307 processId(mapper, 1);
9308 processPosition(mapper, x2, y2);
9309 processSync(mapper);
9310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9311 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9312 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9313
9314 // finger up.
9315 processId(mapper, -1);
9316 processSync(mapper);
9317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9318 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9319 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9320
9321 // new finger down
9322 processId(mapper, 1);
9323 processPosition(mapper, x3, y3);
9324 processSync(mapper);
9325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9326 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9327 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9328}
9329
9330/**
arthurhungcc7f9802020-04-30 17:55:40 +08009331 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9332 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009333 */
arthurhungcc7f9802020-04-30 17:55:40 +08009334TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009335 addConfigurationProperty("touch.deviceType", "touchScreen");
9336 prepareDisplay(DISPLAY_ORIENTATION_0);
9337 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009338 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009339
9340 NotifyMotionArgs motionArgs;
9341
9342 // default tool type is finger
9343 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009344 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009345 processPosition(mapper, x1, y1);
9346 processSync(mapper);
9347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9348 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9349 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9350
9351 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9352 processToolType(mapper, MT_TOOL_PALM);
9353 processSync(mapper);
9354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9355 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9356
9357 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009358 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009359 processPosition(mapper, x2, y2);
9360 processSync(mapper);
9361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9362
9363 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009364 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009365 processSync(mapper);
9366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9367
9368 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009369 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009370 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009371 processPosition(mapper, x3, y3);
9372 processSync(mapper);
9373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9374 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9375 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9376}
9377
arthurhungbf89a482020-04-17 17:37:55 +08009378/**
arthurhungcc7f9802020-04-30 17:55:40 +08009379 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9380 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009381 */
arthurhungcc7f9802020-04-30 17:55:40 +08009382TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009383 addConfigurationProperty("touch.deviceType", "touchScreen");
9384 prepareDisplay(DISPLAY_ORIENTATION_0);
9385 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9386 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9387
9388 NotifyMotionArgs motionArgs;
9389
9390 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009391 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9392 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009393 processPosition(mapper, x1, y1);
9394 processSync(mapper);
9395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9396 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9397 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9398
9399 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009400 processSlot(mapper, SECOND_SLOT);
9401 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009402 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009403 processSync(mapper);
9404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009405 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009406 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9407
9408 // If the tool type of the first finger changes to MT_TOOL_PALM,
9409 // we expect to receive ACTION_POINTER_UP with cancel flag.
9410 processSlot(mapper, FIRST_SLOT);
9411 processId(mapper, FIRST_TRACKING_ID);
9412 processToolType(mapper, MT_TOOL_PALM);
9413 processSync(mapper);
9414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009415 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009416 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9417
9418 // The following MOVE events of second finger should be processed.
9419 processSlot(mapper, SECOND_SLOT);
9420 processId(mapper, SECOND_TRACKING_ID);
9421 processPosition(mapper, x2 + 1, y2 + 1);
9422 processSync(mapper);
9423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9424 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9425 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9426
9427 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9428 // it. Second finger receive move.
9429 processSlot(mapper, FIRST_SLOT);
9430 processId(mapper, INVALID_TRACKING_ID);
9431 processSync(mapper);
9432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9433 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9434 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9435
9436 // Second finger keeps moving.
9437 processSlot(mapper, SECOND_SLOT);
9438 processId(mapper, SECOND_TRACKING_ID);
9439 processPosition(mapper, x2 + 2, y2 + 2);
9440 processSync(mapper);
9441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9442 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9443 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9444
9445 // Second finger up.
9446 processId(mapper, INVALID_TRACKING_ID);
9447 processSync(mapper);
9448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9449 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9450 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9451}
9452
9453/**
9454 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9455 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9456 */
9457TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9458 addConfigurationProperty("touch.deviceType", "touchScreen");
9459 prepareDisplay(DISPLAY_ORIENTATION_0);
9460 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9461 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9462
9463 NotifyMotionArgs motionArgs;
9464
9465 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9466 // First finger down.
9467 processId(mapper, FIRST_TRACKING_ID);
9468 processPosition(mapper, x1, y1);
9469 processSync(mapper);
9470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9471 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9472 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9473
9474 // Second finger down.
9475 processSlot(mapper, SECOND_SLOT);
9476 processId(mapper, SECOND_TRACKING_ID);
9477 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009478 processSync(mapper);
9479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009480 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009481 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9482
arthurhungcc7f9802020-04-30 17:55:40 +08009483 // If the tool type of the first finger changes to MT_TOOL_PALM,
9484 // we expect to receive ACTION_POINTER_UP with cancel flag.
9485 processSlot(mapper, FIRST_SLOT);
9486 processId(mapper, FIRST_TRACKING_ID);
9487 processToolType(mapper, MT_TOOL_PALM);
9488 processSync(mapper);
9489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009490 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009491 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9492
9493 // Second finger keeps moving.
9494 processSlot(mapper, SECOND_SLOT);
9495 processId(mapper, SECOND_TRACKING_ID);
9496 processPosition(mapper, x2 + 1, y2 + 1);
9497 processSync(mapper);
9498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9499 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9500
9501 // second finger becomes palm, receive cancel due to only 1 finger is active.
9502 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009503 processToolType(mapper, MT_TOOL_PALM);
9504 processSync(mapper);
9505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9506 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9507
arthurhungcc7f9802020-04-30 17:55:40 +08009508 // third finger down.
9509 processSlot(mapper, THIRD_SLOT);
9510 processId(mapper, THIRD_TRACKING_ID);
9511 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009512 processPosition(mapper, x3, y3);
9513 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9515 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9516 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009517 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9518
9519 // third finger move
9520 processId(mapper, THIRD_TRACKING_ID);
9521 processPosition(mapper, x3 + 1, y3 + 1);
9522 processSync(mapper);
9523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9524 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9525
9526 // first finger up, third finger receive move.
9527 processSlot(mapper, FIRST_SLOT);
9528 processId(mapper, INVALID_TRACKING_ID);
9529 processSync(mapper);
9530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9531 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9532 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9533
9534 // second finger up, third finger receive move.
9535 processSlot(mapper, SECOND_SLOT);
9536 processId(mapper, INVALID_TRACKING_ID);
9537 processSync(mapper);
9538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9539 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9540 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9541
9542 // third finger up.
9543 processSlot(mapper, THIRD_SLOT);
9544 processId(mapper, INVALID_TRACKING_ID);
9545 processSync(mapper);
9546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9547 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9548 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9549}
9550
9551/**
9552 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9553 * and the active finger could still be allowed to receive the events
9554 */
9555TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9556 addConfigurationProperty("touch.deviceType", "touchScreen");
9557 prepareDisplay(DISPLAY_ORIENTATION_0);
9558 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9559 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9560
9561 NotifyMotionArgs motionArgs;
9562
9563 // default tool type is finger
9564 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9565 processId(mapper, FIRST_TRACKING_ID);
9566 processPosition(mapper, x1, y1);
9567 processSync(mapper);
9568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9569 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9570 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9571
9572 // Second finger down.
9573 processSlot(mapper, SECOND_SLOT);
9574 processId(mapper, SECOND_TRACKING_ID);
9575 processPosition(mapper, x2, y2);
9576 processSync(mapper);
9577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009578 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009579 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9580
9581 // If the tool type of the second finger changes to MT_TOOL_PALM,
9582 // we expect to receive ACTION_POINTER_UP with cancel flag.
9583 processId(mapper, SECOND_TRACKING_ID);
9584 processToolType(mapper, MT_TOOL_PALM);
9585 processSync(mapper);
9586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009587 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009588 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9589
9590 // The following MOVE event should be processed.
9591 processSlot(mapper, FIRST_SLOT);
9592 processId(mapper, FIRST_TRACKING_ID);
9593 processPosition(mapper, x1 + 1, y1 + 1);
9594 processSync(mapper);
9595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9596 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9597 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9598
9599 // second finger up.
9600 processSlot(mapper, SECOND_SLOT);
9601 processId(mapper, INVALID_TRACKING_ID);
9602 processSync(mapper);
9603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9604 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9605
9606 // first finger keep moving
9607 processSlot(mapper, FIRST_SLOT);
9608 processId(mapper, FIRST_TRACKING_ID);
9609 processPosition(mapper, x1 + 2, y1 + 2);
9610 processSync(mapper);
9611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9612 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9613
9614 // first finger up.
9615 processId(mapper, INVALID_TRACKING_ID);
9616 processSync(mapper);
9617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9618 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9619 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009620}
9621
Arthur Hung9ad18942021-06-19 02:04:46 +00009622/**
9623 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9624 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9625 * cause slot be valid again.
9626 */
9627TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9628 addConfigurationProperty("touch.deviceType", "touchScreen");
9629 prepareDisplay(DISPLAY_ORIENTATION_0);
9630 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9631 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9632
9633 NotifyMotionArgs motionArgs;
9634
9635 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9636 // First finger down.
9637 processId(mapper, FIRST_TRACKING_ID);
9638 processPosition(mapper, x1, y1);
9639 processPressure(mapper, RAW_PRESSURE_MAX);
9640 processSync(mapper);
9641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9642 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9643 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9644
9645 // First finger move.
9646 processId(mapper, FIRST_TRACKING_ID);
9647 processPosition(mapper, x1 + 1, y1 + 1);
9648 processPressure(mapper, RAW_PRESSURE_MAX);
9649 processSync(mapper);
9650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9651 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9652 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9653
9654 // Second finger down.
9655 processSlot(mapper, SECOND_SLOT);
9656 processId(mapper, SECOND_TRACKING_ID);
9657 processPosition(mapper, x2, y2);
9658 processPressure(mapper, RAW_PRESSURE_MAX);
9659 processSync(mapper);
9660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009661 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009662 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9663
9664 // second finger up with some unexpected data.
9665 processSlot(mapper, SECOND_SLOT);
9666 processId(mapper, INVALID_TRACKING_ID);
9667 processPosition(mapper, x2, y2);
9668 processSync(mapper);
9669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009670 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009671 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9672
9673 // first finger up with some unexpected data.
9674 processSlot(mapper, FIRST_SLOT);
9675 processId(mapper, INVALID_TRACKING_ID);
9676 processPosition(mapper, x2, y2);
9677 processPressure(mapper, RAW_PRESSURE_MAX);
9678 processSync(mapper);
9679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9680 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9681 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9682}
9683
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009684TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9685 addConfigurationProperty("touch.deviceType", "touchScreen");
9686 prepareDisplay(DISPLAY_ORIENTATION_0);
9687 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9688 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9689
9690 // First finger down.
9691 processId(mapper, FIRST_TRACKING_ID);
9692 processPosition(mapper, 100, 200);
9693 processPressure(mapper, RAW_PRESSURE_MAX);
9694 processSync(mapper);
9695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9696 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9697
9698 // Second finger down.
9699 processSlot(mapper, SECOND_SLOT);
9700 processId(mapper, SECOND_TRACKING_ID);
9701 processPosition(mapper, 300, 400);
9702 processPressure(mapper, RAW_PRESSURE_MAX);
9703 processSync(mapper);
9704 ASSERT_NO_FATAL_FAILURE(
9705 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9706
9707 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009708 // preserved. Resetting should cancel the ongoing gesture.
9709 resetMapper(mapper, ARBITRARY_TIME);
9710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9711 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009712
9713 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9714 // the existing touch state to generate a down event.
9715 processPosition(mapper, 301, 302);
9716 processSync(mapper);
9717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9718 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
9719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9720 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
9721
9722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9723}
9724
9725TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9726 addConfigurationProperty("touch.deviceType", "touchScreen");
9727 prepareDisplay(DISPLAY_ORIENTATION_0);
9728 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9729 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9730
9731 // First finger touches down and releases.
9732 processId(mapper, FIRST_TRACKING_ID);
9733 processPosition(mapper, 100, 200);
9734 processPressure(mapper, RAW_PRESSURE_MAX);
9735 processSync(mapper);
9736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9737 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9738 processId(mapper, INVALID_TRACKING_ID);
9739 processSync(mapper);
9740 ASSERT_NO_FATAL_FAILURE(
9741 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9742
9743 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9744 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009745 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9747
9748 // Send an empty sync frame. Since there are no pointers, no events are generated.
9749 processSync(mapper);
9750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9751}
9752
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009753// --- MultiTouchInputMapperTest_ExternalDevice ---
9754
9755class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9756protected:
Chris Yea52ade12020-08-27 16:49:20 -07009757 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009758};
9759
9760/**
9761 * Expect fallback to internal viewport if device is external and external viewport is not present.
9762 */
9763TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9764 prepareAxes(POSITION);
9765 addConfigurationProperty("touch.deviceType", "touchScreen");
9766 prepareDisplay(DISPLAY_ORIENTATION_0);
9767 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9768
9769 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9770
9771 NotifyMotionArgs motionArgs;
9772
9773 // Expect the event to be sent to the internal viewport,
9774 // because an external viewport is not present.
9775 processPosition(mapper, 100, 100);
9776 processSync(mapper);
9777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9778 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9779
9780 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009781 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009782 processPosition(mapper, 100, 100);
9783 processSync(mapper);
9784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9785 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9786}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009787
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009788TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9789 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9790 std::shared_ptr<FakePointerController> fakePointerController =
9791 std::make_shared<FakePointerController>();
9792 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9793 fakePointerController->setPosition(0, 0);
9794 fakePointerController->setButtonState(0);
9795
9796 // prepare device and capture
9797 prepareDisplay(DISPLAY_ORIENTATION_0);
9798 prepareAxes(POSITION | ID | SLOT);
9799 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9800 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9801 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009802 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009803 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9804
9805 // captured touchpad should be a touchpad source
9806 NotifyDeviceResetArgs resetArgs;
9807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9808 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9809
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009810 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009811
9812 const InputDeviceInfo::MotionRange* relRangeX =
9813 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9814 ASSERT_NE(relRangeX, nullptr);
9815 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9816 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9817 const InputDeviceInfo::MotionRange* relRangeY =
9818 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9819 ASSERT_NE(relRangeY, nullptr);
9820 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9821 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9822
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009823 // run captured pointer tests - note that this is unscaled, so input listener events should be
9824 // identical to what the hardware sends (accounting for any
9825 // calibration).
9826 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009827 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009828 processId(mapper, 1);
9829 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9830 processKey(mapper, BTN_TOUCH, 1);
9831 processSync(mapper);
9832
9833 // expect coord[0] to contain initial location of touch 0
9834 NotifyMotionArgs args;
9835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9836 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9837 ASSERT_EQ(1U, args.pointerCount);
9838 ASSERT_EQ(0, args.pointerProperties[0].id);
9839 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9840 ASSERT_NO_FATAL_FAILURE(
9841 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9842
9843 // FINGER 1 DOWN
9844 processSlot(mapper, 1);
9845 processId(mapper, 2);
9846 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9847 processSync(mapper);
9848
9849 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009851 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009852 ASSERT_EQ(2U, args.pointerCount);
9853 ASSERT_EQ(0, args.pointerProperties[0].id);
9854 ASSERT_EQ(1, args.pointerProperties[1].id);
9855 ASSERT_NO_FATAL_FAILURE(
9856 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9857 ASSERT_NO_FATAL_FAILURE(
9858 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9859
9860 // FINGER 1 MOVE
9861 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9862 processSync(mapper);
9863
9864 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9865 // from move
9866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9867 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9868 ASSERT_NO_FATAL_FAILURE(
9869 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9870 ASSERT_NO_FATAL_FAILURE(
9871 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9872
9873 // FINGER 0 MOVE
9874 processSlot(mapper, 0);
9875 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9876 processSync(mapper);
9877
9878 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9880 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9881 ASSERT_NO_FATAL_FAILURE(
9882 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9883 ASSERT_NO_FATAL_FAILURE(
9884 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9885
9886 // BUTTON DOWN
9887 processKey(mapper, BTN_LEFT, 1);
9888 processSync(mapper);
9889
9890 // touchinputmapper design sends a move before button press
9891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9892 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9894 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9895
9896 // BUTTON UP
9897 processKey(mapper, BTN_LEFT, 0);
9898 processSync(mapper);
9899
9900 // touchinputmapper design sends a move after button release
9901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9902 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9904 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9905
9906 // FINGER 0 UP
9907 processId(mapper, -1);
9908 processSync(mapper);
9909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9910 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9911
9912 // FINGER 1 MOVE
9913 processSlot(mapper, 1);
9914 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9915 processSync(mapper);
9916
9917 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9919 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9920 ASSERT_EQ(1U, args.pointerCount);
9921 ASSERT_EQ(1, args.pointerProperties[0].id);
9922 ASSERT_NO_FATAL_FAILURE(
9923 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9924
9925 // FINGER 1 UP
9926 processId(mapper, -1);
9927 processKey(mapper, BTN_TOUCH, 0);
9928 processSync(mapper);
9929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9930 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9931
9932 // non captured touchpad should be a mouse source
9933 mFakePolicy->setPointerCapture(false);
9934 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9936 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9937}
9938
9939TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9940 std::shared_ptr<FakePointerController> fakePointerController =
9941 std::make_shared<FakePointerController>();
9942 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9943 fakePointerController->setPosition(0, 0);
9944 fakePointerController->setButtonState(0);
9945
9946 // prepare device and capture
9947 prepareDisplay(DISPLAY_ORIENTATION_0);
9948 prepareAxes(POSITION | ID | SLOT);
9949 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9950 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009951 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009952 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9953 // run uncaptured pointer tests - pushes out generic events
9954 // FINGER 0 DOWN
9955 processId(mapper, 3);
9956 processPosition(mapper, 100, 100);
9957 processKey(mapper, BTN_TOUCH, 1);
9958 processSync(mapper);
9959
9960 // start at (100,100), cursor should be at (0,0) * scale
9961 NotifyMotionArgs args;
9962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9963 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9964 ASSERT_NO_FATAL_FAILURE(
9965 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9966
9967 // FINGER 0 MOVE
9968 processPosition(mapper, 200, 200);
9969 processSync(mapper);
9970
9971 // compute scaling to help with touch position checking
9972 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9973 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9974 float scale =
9975 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9976
9977 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9979 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9980 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9981 0, 0, 0, 0, 0, 0, 0));
9982}
9983
9984TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9985 std::shared_ptr<FakePointerController> fakePointerController =
9986 std::make_shared<FakePointerController>();
9987
9988 prepareDisplay(DISPLAY_ORIENTATION_0);
9989 prepareAxes(POSITION | ID | SLOT);
9990 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009991 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009992 mFakePolicy->setPointerCapture(false);
9993 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9994
9995 // uncaptured touchpad should be a pointer device
9996 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9997
9998 // captured touchpad should be a touchpad device
9999 mFakePolicy->setPointerCapture(true);
10000 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10001 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10002}
10003
HQ Liue6983c72022-04-19 22:14:56 +000010004class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10005protected:
10006 float mPointerMovementScale;
10007 float mPointerXZoomScale;
10008 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10009 addConfigurationProperty("touch.deviceType", "pointer");
10010 std::shared_ptr<FakePointerController> fakePointerController =
10011 std::make_shared<FakePointerController>();
10012 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10013 fakePointerController->setPosition(0, 0);
10014 fakePointerController->setButtonState(0);
10015 prepareDisplay(DISPLAY_ORIENTATION_0);
10016
10017 prepareAxes(POSITION);
10018 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10019 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10020 // needs to be disabled, and the pointer gesture needs to be enabled.
10021 mFakePolicy->setPointerCapture(false);
10022 mFakePolicy->setPointerGestureEnabled(true);
10023 mFakePolicy->setPointerController(fakePointerController);
10024
10025 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10026 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10027 mPointerMovementScale =
10028 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10029 mPointerXZoomScale =
10030 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10031 }
10032
10033 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10034 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10035 /*flat*/ 0,
10036 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10037 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10038 /*flat*/ 0,
10039 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10040 }
10041};
10042
10043/**
10044 * Two fingers down on a pointer mode touch pad. The width
10045 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10046 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10047 * be greater than the both value to be freeform gesture, so that after two
10048 * fingers start to move downwards, the gesture should be swipe.
10049 */
10050TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10051 // The min freeform gesture width is 25units/mm x 30mm = 750
10052 // which is greater than fraction of the diagnal length of the touchpad (349).
10053 // Thus, MaxSwipWidth is 750.
10054 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10055 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10056 NotifyMotionArgs motionArgs;
10057
10058 // Two fingers down at once.
10059 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10060 // Pointer's initial position is used the [0,0] coordinate.
10061 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10062
10063 processId(mapper, FIRST_TRACKING_ID);
10064 processPosition(mapper, x1, y1);
10065 processMTSync(mapper);
10066 processId(mapper, SECOND_TRACKING_ID);
10067 processPosition(mapper, x2, y2);
10068 processMTSync(mapper);
10069 processSync(mapper);
10070
10071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10072 ASSERT_EQ(1U, motionArgs.pointerCount);
10073 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10074 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010075 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010076 ASSERT_NO_FATAL_FAILURE(
10077 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10078
10079 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10080 // that there should be 1 pointer.
10081 int32_t movingDistance = 200;
10082 y1 += movingDistance;
10083 y2 += movingDistance;
10084
10085 processId(mapper, FIRST_TRACKING_ID);
10086 processPosition(mapper, x1, y1);
10087 processMTSync(mapper);
10088 processId(mapper, SECOND_TRACKING_ID);
10089 processPosition(mapper, x2, y2);
10090 processMTSync(mapper);
10091 processSync(mapper);
10092
10093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10094 ASSERT_EQ(1U, motionArgs.pointerCount);
10095 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10096 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010097 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010098 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10099 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10100 0, 0, 0, 0));
10101}
10102
10103/**
10104 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10105 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10106 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10107 * value to be freeform gesture, so that after two fingers start to move downwards,
10108 * the gesture should be swipe.
10109 */
10110TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10111 // The min freeform gesture width is 5units/mm x 30mm = 150
10112 // which is greater than fraction of the diagnal length of the touchpad (349).
10113 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
10114 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
10115 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10116 NotifyMotionArgs motionArgs;
10117
10118 // Two fingers down at once.
10119 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10120 // Pointer's initial position is used the [0,0] coordinate.
10121 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10122
10123 processId(mapper, FIRST_TRACKING_ID);
10124 processPosition(mapper, x1, y1);
10125 processMTSync(mapper);
10126 processId(mapper, SECOND_TRACKING_ID);
10127 processPosition(mapper, x2, y2);
10128 processMTSync(mapper);
10129 processSync(mapper);
10130
10131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10132 ASSERT_EQ(1U, motionArgs.pointerCount);
10133 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10134 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010135 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010136 ASSERT_NO_FATAL_FAILURE(
10137 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10138
10139 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10140 // and there should be 1 pointer.
10141 int32_t movingDistance = 200;
10142 y1 += movingDistance;
10143 y2 += movingDistance;
10144
10145 processId(mapper, FIRST_TRACKING_ID);
10146 processPosition(mapper, x1, y1);
10147 processMTSync(mapper);
10148 processId(mapper, SECOND_TRACKING_ID);
10149 processPosition(mapper, x2, y2);
10150 processMTSync(mapper);
10151 processSync(mapper);
10152
10153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10154 ASSERT_EQ(1U, motionArgs.pointerCount);
10155 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10156 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010157 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010158 // New coordinate is the scaled relative coordinate from the initial coordinate.
10159 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10160 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10161 0, 0, 0, 0));
10162}
10163
10164/**
10165 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10166 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10167 * freeform gestures after two fingers start to move downwards.
10168 */
10169TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
10170 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10171 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10172
10173 NotifyMotionArgs motionArgs;
10174
10175 // Two fingers down at once. Wider than the max swipe width.
10176 // The gesture is expected to be PRESS, then transformed to FREEFORM
10177 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10178
10179 processId(mapper, FIRST_TRACKING_ID);
10180 processPosition(mapper, x1, y1);
10181 processMTSync(mapper);
10182 processId(mapper, SECOND_TRACKING_ID);
10183 processPosition(mapper, x2, y2);
10184 processMTSync(mapper);
10185 processSync(mapper);
10186
10187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10188 ASSERT_EQ(1U, motionArgs.pointerCount);
10189 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10190 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010191 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010192 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10193 ASSERT_NO_FATAL_FAILURE(
10194 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10195
10196 int32_t movingDistance = 200;
10197
10198 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10199 // then two down events for two pointers.
10200 y1 += movingDistance;
10201 y2 += movingDistance;
10202
10203 processId(mapper, FIRST_TRACKING_ID);
10204 processPosition(mapper, x1, y1);
10205 processMTSync(mapper);
10206 processId(mapper, SECOND_TRACKING_ID);
10207 processPosition(mapper, x2, y2);
10208 processMTSync(mapper);
10209 processSync(mapper);
10210
10211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10212 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10213 ASSERT_EQ(1U, motionArgs.pointerCount);
10214 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10216 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10217 ASSERT_EQ(1U, motionArgs.pointerCount);
10218 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10220 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010221 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010222 ASSERT_EQ(2U, motionArgs.pointerCount);
10223 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
10224 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010225 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010226 // Two pointers' scaled relative coordinates from their initial centroid.
10227 // Initial y coordinates are 0 as y1 and y2 have the same value.
10228 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10229 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10230 // When pointers move, the new coordinates equal to the initial coordinates plus
10231 // scaled moving distance.
10232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10233 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10234 0, 0, 0, 0));
10235 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10236 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10237 0, 0, 0, 0));
10238
10239 // Move two fingers down again, expect one MOVE motion event.
10240 y1 += movingDistance;
10241 y2 += movingDistance;
10242
10243 processId(mapper, FIRST_TRACKING_ID);
10244 processPosition(mapper, x1, y1);
10245 processMTSync(mapper);
10246 processId(mapper, SECOND_TRACKING_ID);
10247 processPosition(mapper, x2, y2);
10248 processMTSync(mapper);
10249 processSync(mapper);
10250
10251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10252 ASSERT_EQ(2U, motionArgs.pointerCount);
10253 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10254 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010255 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010256 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10257 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10258 0, 0, 0, 0, 0));
10259 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10260 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10261 0, 0, 0, 0, 0));
10262}
10263
Harry Cutts39b7ca22022-10-05 15:55:48 +000010264TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
10265 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10266 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10267 NotifyMotionArgs motionArgs;
10268
10269 // Place two fingers down.
10270 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10271
10272 processId(mapper, FIRST_TRACKING_ID);
10273 processPosition(mapper, x1, y1);
10274 processMTSync(mapper);
10275 processId(mapper, SECOND_TRACKING_ID);
10276 processPosition(mapper, x2, y2);
10277 processMTSync(mapper);
10278 processSync(mapper);
10279
10280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10281 ASSERT_EQ(1U, motionArgs.pointerCount);
10282 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10283 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10284 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10285 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10286
10287 // Move the two fingers down and to the left.
10288 int32_t movingDistance = 200;
10289 x1 -= movingDistance;
10290 y1 += movingDistance;
10291 x2 -= movingDistance;
10292 y2 += movingDistance;
10293
10294 processId(mapper, FIRST_TRACKING_ID);
10295 processPosition(mapper, x1, y1);
10296 processMTSync(mapper);
10297 processId(mapper, SECOND_TRACKING_ID);
10298 processPosition(mapper, x2, y2);
10299 processMTSync(mapper);
10300 processSync(mapper);
10301
10302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10303 ASSERT_EQ(1U, motionArgs.pointerCount);
10304 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10305 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10306 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10307 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10308}
10309
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010310// --- JoystickInputMapperTest ---
10311
10312class JoystickInputMapperTest : public InputMapperTest {
10313protected:
10314 static const int32_t RAW_X_MIN;
10315 static const int32_t RAW_X_MAX;
10316 static const int32_t RAW_Y_MIN;
10317 static const int32_t RAW_Y_MAX;
10318
10319 void SetUp() override {
10320 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10321 }
10322 void prepareAxes() {
10323 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10324 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10325 }
10326
10327 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10328 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10329 }
10330
10331 void processSync(JoystickInputMapper& mapper) {
10332 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10333 }
10334
10335 void prepareVirtualDisplay(int32_t orientation) {
10336 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10337 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10338 NO_PORT, ViewportType::VIRTUAL);
10339 }
10340};
10341
10342const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10343const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10344const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10345const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10346
10347TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10348 prepareAxes();
10349 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10350
10351 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10352
10353 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
10354
10355 // Send an axis event
10356 processAxis(mapper, ABS_X, 100);
10357 processSync(mapper);
10358
10359 NotifyMotionArgs args;
10360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10361 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10362
10363 // Send another axis event
10364 processAxis(mapper, ABS_Y, 100);
10365 processSync(mapper);
10366
10367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10368 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10369}
10370
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010371// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010372
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010373class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010374protected:
10375 static const char* DEVICE_NAME;
10376 static const char* DEVICE_LOCATION;
10377 static const int32_t DEVICE_ID;
10378 static const int32_t DEVICE_GENERATION;
10379 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010380 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010381 static const int32_t EVENTHUB_ID;
10382
10383 std::shared_ptr<FakeEventHub> mFakeEventHub;
10384 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010385 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010386 std::unique_ptr<InstrumentedInputReader> mReader;
10387 std::shared_ptr<InputDevice> mDevice;
10388
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010389 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010390 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010391 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010392 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010393 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010394 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010395 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10396 }
10397
10398 void SetUp() override { SetUp(DEVICE_CLASSES); }
10399
10400 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010401 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010402 mFakePolicy.clear();
10403 }
10404
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010405 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010406 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10407 mReader->requestRefreshConfiguration(changes);
10408 mReader->loopOnce();
10409 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010410 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010411 }
10412
10413 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10414 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010415 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010416 InputDeviceIdentifier identifier;
10417 identifier.name = name;
10418 identifier.location = location;
10419 std::shared_ptr<InputDevice> device =
10420 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10421 identifier);
10422 mReader->pushNextDevice(device);
10423 mFakeEventHub->addDevice(eventHubId, name, classes);
10424 mReader->loopOnce();
10425 return device;
10426 }
10427
10428 template <class T, typename... Args>
10429 T& addControllerAndConfigure(Args... args) {
10430 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10431
10432 return controller;
10433 }
10434};
10435
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010436const char* PeripheralControllerTest::DEVICE_NAME = "device";
10437const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10438const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10439const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10440const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010441const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10442 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010443const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010444
10445// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010446class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010447protected:
10448 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010449 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010450 }
10451};
10452
10453TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010454 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010455
10456 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
10457 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
10458}
10459
10460TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010461 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010462
10463 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
10464 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
10465}
10466
10467// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010468class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010469protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010470 void SetUp() override {
10471 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10472 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010473};
10474
Chris Ye85758332021-05-16 23:05:17 -070010475TEST_F(LightControllerTest, MonoLight) {
10476 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010477 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010478 .maxBrightness = 255,
10479 .flags = InputLightClass::BRIGHTNESS,
10480 .path = ""};
10481 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010482
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010483 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010484 InputDeviceInfo info;
10485 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010486 std::vector<InputDeviceLightInfo> lights = info.getLights();
10487 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010488 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10489 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10490
10491 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10492 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10493}
10494
10495TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10496 RawLightInfo infoMono = {.id = 1,
10497 .name = "mono_keyboard_backlight",
10498 .maxBrightness = 255,
10499 .flags = InputLightClass::BRIGHTNESS |
10500 InputLightClass::KEYBOARD_BACKLIGHT,
10501 .path = ""};
10502 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10503
10504 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10505 InputDeviceInfo info;
10506 controller.populateDeviceInfo(&info);
10507 std::vector<InputDeviceLightInfo> lights = info.getLights();
10508 ASSERT_EQ(1U, lights.size());
10509 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10510 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010511
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010512 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10513 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010514}
10515
10516TEST_F(LightControllerTest, RGBLight) {
10517 RawLightInfo infoRed = {.id = 1,
10518 .name = "red",
10519 .maxBrightness = 255,
10520 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10521 .path = ""};
10522 RawLightInfo infoGreen = {.id = 2,
10523 .name = "green",
10524 .maxBrightness = 255,
10525 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10526 .path = ""};
10527 RawLightInfo infoBlue = {.id = 3,
10528 .name = "blue",
10529 .maxBrightness = 255,
10530 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10531 .path = ""};
10532 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10533 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10534 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10535
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010536 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010537 InputDeviceInfo info;
10538 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010539 std::vector<InputDeviceLightInfo> lights = info.getLights();
10540 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010541 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10542 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10543 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10544
10545 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10546 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10547}
10548
10549TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10550 RawLightInfo infoRed = {.id = 1,
10551 .name = "red_keyboard_backlight",
10552 .maxBrightness = 255,
10553 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10554 InputLightClass::KEYBOARD_BACKLIGHT,
10555 .path = ""};
10556 RawLightInfo infoGreen = {.id = 2,
10557 .name = "green_keyboard_backlight",
10558 .maxBrightness = 255,
10559 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10560 InputLightClass::KEYBOARD_BACKLIGHT,
10561 .path = ""};
10562 RawLightInfo infoBlue = {.id = 3,
10563 .name = "blue_keyboard_backlight",
10564 .maxBrightness = 255,
10565 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10566 InputLightClass::KEYBOARD_BACKLIGHT,
10567 .path = ""};
10568 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10569 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10570 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10571
10572 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10573 InputDeviceInfo info;
10574 controller.populateDeviceInfo(&info);
10575 std::vector<InputDeviceLightInfo> lights = info.getLights();
10576 ASSERT_EQ(1U, lights.size());
10577 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10578 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10579 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10580
10581 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10582 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10583}
10584
10585TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10586 RawLightInfo infoRed = {.id = 1,
10587 .name = "red",
10588 .maxBrightness = 255,
10589 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10590 .path = ""};
10591 RawLightInfo infoGreen = {.id = 2,
10592 .name = "green",
10593 .maxBrightness = 255,
10594 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10595 .path = ""};
10596 RawLightInfo infoBlue = {.id = 3,
10597 .name = "blue",
10598 .maxBrightness = 255,
10599 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10600 .path = ""};
10601 RawLightInfo infoGlobal = {.id = 3,
10602 .name = "global_keyboard_backlight",
10603 .maxBrightness = 255,
10604 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10605 InputLightClass::KEYBOARD_BACKLIGHT,
10606 .path = ""};
10607 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10608 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10609 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10610 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10611
10612 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10613 InputDeviceInfo info;
10614 controller.populateDeviceInfo(&info);
10615 std::vector<InputDeviceLightInfo> lights = info.getLights();
10616 ASSERT_EQ(1U, lights.size());
10617 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10618 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10619 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010620
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010621 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10622 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010623}
10624
10625TEST_F(LightControllerTest, MultiColorRGBLight) {
10626 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010627 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010628 .maxBrightness = 255,
10629 .flags = InputLightClass::BRIGHTNESS |
10630 InputLightClass::MULTI_INTENSITY |
10631 InputLightClass::MULTI_INDEX,
10632 .path = ""};
10633
10634 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10635
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010636 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010637 InputDeviceInfo info;
10638 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010639 std::vector<InputDeviceLightInfo> lights = info.getLights();
10640 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010641 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10642 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10643 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10644
10645 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10646 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10647}
10648
10649TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10650 RawLightInfo infoColor = {.id = 1,
10651 .name = "multi_color_keyboard_backlight",
10652 .maxBrightness = 255,
10653 .flags = InputLightClass::BRIGHTNESS |
10654 InputLightClass::MULTI_INTENSITY |
10655 InputLightClass::MULTI_INDEX |
10656 InputLightClass::KEYBOARD_BACKLIGHT,
10657 .path = ""};
10658
10659 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10660
10661 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10662 InputDeviceInfo info;
10663 controller.populateDeviceInfo(&info);
10664 std::vector<InputDeviceLightInfo> lights = info.getLights();
10665 ASSERT_EQ(1U, lights.size());
10666 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10667 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10668 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010669
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010670 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10671 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010672}
10673
10674TEST_F(LightControllerTest, PlayerIdLight) {
10675 RawLightInfo info1 = {.id = 1,
10676 .name = "player1",
10677 .maxBrightness = 255,
10678 .flags = InputLightClass::BRIGHTNESS,
10679 .path = ""};
10680 RawLightInfo info2 = {.id = 2,
10681 .name = "player2",
10682 .maxBrightness = 255,
10683 .flags = InputLightClass::BRIGHTNESS,
10684 .path = ""};
10685 RawLightInfo info3 = {.id = 3,
10686 .name = "player3",
10687 .maxBrightness = 255,
10688 .flags = InputLightClass::BRIGHTNESS,
10689 .path = ""};
10690 RawLightInfo info4 = {.id = 4,
10691 .name = "player4",
10692 .maxBrightness = 255,
10693 .flags = InputLightClass::BRIGHTNESS,
10694 .path = ""};
10695 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10696 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10697 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10698 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10699
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010700 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010701 InputDeviceInfo info;
10702 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010703 std::vector<InputDeviceLightInfo> lights = info.getLights();
10704 ASSERT_EQ(1U, lights.size());
10705 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010706 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10707 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010708
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010709 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10710 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10711 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010712}
10713
Michael Wrightd02c5b62014-02-10 15:10:22 -080010714} // namespace android