blob: f333306f261eaab9c274b08c79f01516eaa99e8b [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
2394 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002395 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002396 const auto& it =
2397 std::find_if(inputDevices.begin(), inputDevices.end(),
2398 [&keyboard](const InputDeviceInfo& info) {
2399 return info.getIdentifier().name == keyboard->getName();
2400 });
2401
2402 ASSERT_NE(it, inputDevices.end());
2403 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2404 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2405 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002406
2407 keyboard.reset();
2408 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2409 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2410 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2411}
2412
2413TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2414 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2415 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2416
2417 NotifyConfigurationChangedArgs configChangedArgs;
2418 ASSERT_NO_FATAL_FAILURE(
2419 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002420 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002421 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2422
2423 NotifyKeyArgs keyArgs;
2424 keyboard->pressAndReleaseHomeKey();
2425 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2426 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002427 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002428 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 prevTimestamp = keyArgs.eventTime;
2432
2433 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2434 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002435 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002436 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002437 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002438}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002439
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002440/**
2441 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2442 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2443 * are passed to the listener.
2444 */
2445static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2446TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2447 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2448 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2449 NotifyKeyArgs keyArgs;
2450
2451 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2452 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2453 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2454 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2455
2456 controller->pressAndReleaseKey(BTN_GEAR_UP);
2457 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2458 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2459 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2460}
2461
Arthur Hungaab25622020-01-16 11:22:11 +08002462// --- TouchProcessTest ---
2463class TouchIntegrationTest : public InputReaderIntegrationTest {
2464protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002465 const std::string UNIQUE_ID = "local:0";
2466
Chris Yea52ade12020-08-27 16:49:20 -07002467 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002468#if !defined(__ANDROID__)
2469 GTEST_SKIP();
2470#endif
Arthur Hungaab25622020-01-16 11:22:11 +08002471 InputReaderIntegrationTest::SetUp();
2472 // At least add an internal display.
2473 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2474 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002475 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002476
2477 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2478 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2479 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00002480 const auto info = findDeviceByName(mDevice->getName());
2481 ASSERT_TRUE(info);
2482 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08002483 }
2484
2485 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2486 int32_t orientation, const std::string& uniqueId,
2487 std::optional<uint8_t> physicalPort,
2488 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002489 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2490 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002491 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2492 }
2493
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002494 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2495 NotifyMotionArgs args;
2496 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2497 EXPECT_EQ(action, args.action);
2498 ASSERT_EQ(points.size(), args.pointerCount);
2499 for (size_t i = 0; i < args.pointerCount; i++) {
2500 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2501 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2502 }
2503 }
2504
Arthur Hungaab25622020-01-16 11:22:11 +08002505 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00002506 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08002507};
2508
2509TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2510 NotifyMotionArgs args;
2511 const Point centerPoint = mDevice->getCenterPoint();
2512
2513 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002514 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002515 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002516 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002517 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2518 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2519
2520 // ACTION_MOVE
2521 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002522 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002523 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2524 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2525
2526 // ACTION_UP
2527 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002528 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002529 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2530 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2531}
2532
2533TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2534 NotifyMotionArgs args;
2535 const Point centerPoint = mDevice->getCenterPoint();
2536
2537 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002538 mDevice->sendSlot(FIRST_SLOT);
2539 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002540 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002541 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002542 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2543 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2544
2545 // ACTION_POINTER_DOWN (Second slot)
2546 const Point secondPoint = centerPoint + Point(100, 100);
2547 mDevice->sendSlot(SECOND_SLOT);
2548 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002549 mDevice->sendDown(secondPoint);
2550 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002551 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002552 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002553
2554 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002555 mDevice->sendMove(secondPoint + Point(1, 1));
2556 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002557 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2558 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2559
2560 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002561 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002562 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002563 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002564 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002565
2566 // ACTION_UP
2567 mDevice->sendSlot(FIRST_SLOT);
2568 mDevice->sendUp();
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_UP, args.action);
2572}
2573
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002574/**
2575 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2576 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2577 * data?
2578 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2579 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2580 * for Pointer 0 only is generated after.
2581 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2582 * events, we will not miss any information.
2583 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2584 * event generated afterwards that contains the newest movement of pointer 0.
2585 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2586 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2587 * losing information about non-palm pointers.
2588 */
2589TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2590 NotifyMotionArgs args;
2591 const Point centerPoint = mDevice->getCenterPoint();
2592
2593 // ACTION_DOWN
2594 mDevice->sendSlot(FIRST_SLOT);
2595 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2596 mDevice->sendDown(centerPoint);
2597 mDevice->sendSync();
2598 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2599
2600 // ACTION_POINTER_DOWN (Second slot)
2601 const Point secondPoint = centerPoint + Point(100, 100);
2602 mDevice->sendSlot(SECOND_SLOT);
2603 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2604 mDevice->sendDown(secondPoint);
2605 mDevice->sendSync();
2606 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2607
2608 // ACTION_MOVE (First slot)
2609 mDevice->sendSlot(FIRST_SLOT);
2610 mDevice->sendMove(centerPoint + Point(5, 5));
2611 // ACTION_POINTER_UP (Second slot)
2612 mDevice->sendSlot(SECOND_SLOT);
2613 mDevice->sendPointerUp();
2614 // Send a single sync for the above 2 pointer updates
2615 mDevice->sendSync();
2616
2617 // First, we should get POINTER_UP for the second pointer
2618 assertReceivedMotion(ACTION_POINTER_1_UP,
2619 {/*first pointer */ centerPoint + Point(5, 5),
2620 /*second pointer*/ secondPoint});
2621
2622 // Next, the MOVE event for the first pointer
2623 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2624}
2625
2626/**
2627 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2628 * move, and then it will go up, all in the same frame.
2629 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2630 * gets sent to the listener.
2631 */
2632TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2633 NotifyMotionArgs args;
2634 const Point centerPoint = mDevice->getCenterPoint();
2635
2636 // ACTION_DOWN
2637 mDevice->sendSlot(FIRST_SLOT);
2638 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2639 mDevice->sendDown(centerPoint);
2640 mDevice->sendSync();
2641 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2642
2643 // ACTION_POINTER_DOWN (Second slot)
2644 const Point secondPoint = centerPoint + Point(100, 100);
2645 mDevice->sendSlot(SECOND_SLOT);
2646 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2647 mDevice->sendDown(secondPoint);
2648 mDevice->sendSync();
2649 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2650
2651 // ACTION_MOVE (First slot)
2652 mDevice->sendSlot(FIRST_SLOT);
2653 mDevice->sendMove(centerPoint + Point(5, 5));
2654 // ACTION_POINTER_UP (Second slot)
2655 mDevice->sendSlot(SECOND_SLOT);
2656 mDevice->sendMove(secondPoint + Point(6, 6));
2657 mDevice->sendPointerUp();
2658 // Send a single sync for the above 2 pointer updates
2659 mDevice->sendSync();
2660
2661 // First, we should get POINTER_UP for the second pointer
2662 // The movement of the second pointer during the liftoff frame is ignored.
2663 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2664 assertReceivedMotion(ACTION_POINTER_1_UP,
2665 {/*first pointer */ centerPoint + Point(5, 5),
2666 /*second pointer*/ secondPoint});
2667
2668 // Next, the MOVE event for the first pointer
2669 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2670}
2671
Arthur Hungaab25622020-01-16 11:22:11 +08002672TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2673 NotifyMotionArgs args;
2674 const Point centerPoint = mDevice->getCenterPoint();
2675
2676 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002677 mDevice->sendSlot(FIRST_SLOT);
2678 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002679 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002680 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002681 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2682 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2683
arthurhungcc7f9802020-04-30 17:55:40 +08002684 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002685 const Point secondPoint = centerPoint + Point(100, 100);
2686 mDevice->sendSlot(SECOND_SLOT);
2687 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2688 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002689 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002690 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002691 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002692
arthurhungcc7f9802020-04-30 17:55:40 +08002693 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002694 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002695 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002696 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2697 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2698
arthurhungcc7f9802020-04-30 17:55:40 +08002699 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2700 // a palm event.
2701 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002702 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002703 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002704 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002705 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002706 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002707
arthurhungcc7f9802020-04-30 17:55:40 +08002708 // Send up to second slot, expect first slot send moving.
2709 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002710 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002711 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2712 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002713
arthurhungcc7f9802020-04-30 17:55:40 +08002714 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002715 mDevice->sendSlot(FIRST_SLOT);
2716 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002717 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002718
arthurhungcc7f9802020-04-30 17:55:40 +08002719 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2720 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002721}
2722
Prabir Pradhanda20b172022-09-26 17:01:18 +00002723TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
2724 const Point centerPoint = mDevice->getCenterPoint();
2725
2726 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
2727 mDevice->sendSlot(FIRST_SLOT);
2728 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2729 mDevice->sendToolType(MT_TOOL_PEN);
2730 mDevice->sendDown(centerPoint);
2731 mDevice->sendSync();
2732 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2733 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2734 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
2735
2736 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2737
2738 // Release the stylus touch.
2739 mDevice->sendUp();
2740 mDevice->sendSync();
2741 ASSERT_NO_FATAL_FAILURE(
2742 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2743
2744 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2745
2746 // Touch down with the finger, without the pen tool selected. The policy is not notified.
2747 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2748 mDevice->sendToolType(MT_TOOL_FINGER);
2749 mDevice->sendDown(centerPoint);
2750 mDevice->sendSync();
2751 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2752 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2753 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
2754
2755 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2756
2757 mDevice->sendUp();
2758 mDevice->sendSync();
2759 ASSERT_NO_FATAL_FAILURE(
2760 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2761
2762 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
2763 // The policy should be notified of the stylus presence.
2764 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2765 mDevice->sendToolType(MT_TOOL_PEN);
2766 mDevice->sendMove(centerPoint);
2767 mDevice->sendSync();
2768 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2769 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2770 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
2771
2772 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2773}
2774
Michael Wrightd02c5b62014-02-10 15:10:22 -08002775// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002776class InputDeviceTest : public testing::Test {
2777protected:
2778 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002779 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002780 static const int32_t DEVICE_ID;
2781 static const int32_t DEVICE_GENERATION;
2782 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002783 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002784 static const int32_t EVENTHUB_ID;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002785 static const std::string DEVICE_BLUETOOTH_ADDRESS;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002786
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002787 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002788 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002789 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002790 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002791 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002792
Chris Yea52ade12020-08-27 16:49:20 -07002793 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002794 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002795 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002796 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002797 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002798 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002799 InputDeviceIdentifier identifier;
2800 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002801 identifier.location = DEVICE_LOCATION;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002802 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
arthurhungdcef2dc2020-08-11 14:47:50 +08002803 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002804 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002805 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002806 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002807 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002808 }
2809
Chris Yea52ade12020-08-27 16:49:20 -07002810 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002811 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002812 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002813 }
2814};
2815
2816const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002817const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002818const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002819const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2820const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002821const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002822 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002823const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002824const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002825
2826TEST_F(InputDeviceTest, ImmutableProperties) {
2827 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002828 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002829 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002830}
2831
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002832TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
2833 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
2834
2835 // Configuration
2836 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2837 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002838 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002839
2840 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
2841}
2842
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002843TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2844 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002845}
2846
Michael Wrightd02c5b62014-02-10 15:10:22 -08002847TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2848 // Configuration.
2849 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002850 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002851
2852 // Reset.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002853 unused += mDevice->reset(ARBITRARY_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002854
2855 NotifyDeviceResetArgs resetArgs;
2856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2857 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2858 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2859
2860 // Metadata.
2861 ASSERT_TRUE(mDevice->isIgnored());
2862 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2863
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002864 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002865 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002866 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002867 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2868 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2869
2870 // State queries.
2871 ASSERT_EQ(0, mDevice->getMetaState());
2872
2873 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2874 << "Ignored device should return unknown key code state.";
2875 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2876 << "Ignored device should return unknown scan code state.";
2877 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2878 << "Ignored device should return unknown switch state.";
2879
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002880 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002881 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002882 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002883 << "Ignored device should never mark any key codes.";
2884 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2885 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2886}
2887
2888TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2889 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002890 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08002891
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002892 FakeInputMapper& mapper1 =
2893 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002894 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2895 mapper1.setMetaState(AMETA_ALT_ON);
2896 mapper1.addSupportedKeyCode(AKEYCODE_A);
2897 mapper1.addSupportedKeyCode(AKEYCODE_B);
2898 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2899 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2900 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2901 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2902 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002903
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002904 FakeInputMapper& mapper2 =
2905 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002906 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002907
2908 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002909 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002910
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002911 std::string propertyValue;
2912 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002913 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002914 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002915
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002916 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2917 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002918
2919 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002920 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002921 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2922 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002923
2924 NotifyDeviceResetArgs resetArgs;
2925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2926 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2927 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2928
2929 // Metadata.
2930 ASSERT_FALSE(mDevice->isIgnored());
2931 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2932
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002933 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002934 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002935 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002936 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2937 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2938
2939 // State queries.
2940 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2941 << "Should query mappers and combine meta states.";
2942
2943 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2944 << "Should return unknown key code state when source not supported.";
2945 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2946 << "Should return unknown scan code state when source not supported.";
2947 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2948 << "Should return unknown switch state when source not supported.";
2949
2950 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2951 << "Should query mapper when source is supported.";
2952 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2953 << "Should query mapper when source is supported.";
2954 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2955 << "Should query mapper when source is supported.";
2956
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002957 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002958 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002959 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002960 << "Should do nothing when source is unsupported.";
2961 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2962 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2963 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2964 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2965
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002966 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002967 << "Should query mapper when source is supported.";
2968 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2969 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2970 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2971 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2972
2973 // Event handling.
2974 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002975 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002976 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002977
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002978 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2979 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002980}
2981
Arthur Hung2c9a3342019-07-23 14:18:59 +08002982// A single input device is associated with a specific display. Check that:
2983// 1. Device is disabled if the viewport corresponding to the associated display is not found
2984// 2. Device is disabled when setEnabled API is called
2985TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002986 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002987
2988 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002989 std::list<NotifyArgs> unused =
2990 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002991
2992 // Device should be enabled by default.
2993 ASSERT_TRUE(mDevice->isEnabled());
2994
2995 // Prepare associated info.
2996 constexpr uint8_t hdmi = 1;
2997 const std::string UNIQUE_ID = "local:1";
2998
2999 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003000 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3001 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003002 // Device should be disabled because it is associated with a specific display via
3003 // input port <-> display port association, but the corresponding display is not found
3004 ASSERT_FALSE(mDevice->isEnabled());
3005
3006 // Prepare displays.
3007 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003008 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
3009 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003010 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3011 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003012 ASSERT_TRUE(mDevice->isEnabled());
3013
3014 // Device should be disabled after set disable.
3015 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003016 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3017 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003018 ASSERT_FALSE(mDevice->isEnabled());
3019
3020 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003021 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3022 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003023 ASSERT_FALSE(mDevice->isEnabled());
3024}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003025
Christine Franks1ba71cc2021-04-07 14:37:42 -07003026TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
3027 // Device should be enabled by default.
3028 mFakePolicy->clearViewports();
3029 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003030 std::list<NotifyArgs> unused =
3031 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003032 ASSERT_TRUE(mDevice->isEnabled());
3033
3034 // Device should be disabled because it is associated with a specific display, but the
3035 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08003036 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003037 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3038 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003039 ASSERT_FALSE(mDevice->isEnabled());
3040
3041 // Device should be enabled when a display is found.
3042 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3043 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
3044 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003045 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3046 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003047 ASSERT_TRUE(mDevice->isEnabled());
3048
3049 // Device should be disabled after set disable.
3050 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003051 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3052 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003053 ASSERT_FALSE(mDevice->isEnabled());
3054
3055 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003056 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3057 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003058 ASSERT_FALSE(mDevice->isEnabled());
3059}
3060
Christine Franks2a2293c2022-01-18 11:51:16 -08003061TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
3062 mFakePolicy->clearViewports();
3063 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003064 std::list<NotifyArgs> unused =
3065 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks2a2293c2022-01-18 11:51:16 -08003066
Christine Franks2a2293c2022-01-18 11:51:16 -08003067 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
3068 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3069 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
3070 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003071 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3072 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08003073 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
3074}
3075
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07003076/**
3077 * This test reproduces a crash caused by a dangling reference that remains after device is added
3078 * and removed. The reference is accessed in InputDevice::dump(..);
3079 */
3080TEST_F(InputDeviceTest, DumpDoesNotCrash) {
3081 constexpr int32_t TEST_EVENTHUB_ID = 10;
3082 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
3083
3084 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
3085 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
3086 device.removeEventHubDevice(TEST_EVENTHUB_ID);
3087 std::string dumpStr, eventHubDevStr;
3088 device.dump(dumpStr, eventHubDevStr);
3089}
3090
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003091TEST_F(InputDeviceTest, GetBluetoothAddress) {
3092 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
3093 ASSERT_TRUE(address);
3094 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
3095}
3096
Michael Wrightd02c5b62014-02-10 15:10:22 -08003097// --- InputMapperTest ---
3098
3099class InputMapperTest : public testing::Test {
3100protected:
3101 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003102 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003103 static const int32_t DEVICE_ID;
3104 static const int32_t DEVICE_GENERATION;
3105 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003106 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003107 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003108
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003109 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003110 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003111 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08003112 std::unique_ptr<InstrumentedInputReader> mReader;
3113 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003114
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003115 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003116 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003117 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003118 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08003119 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003120 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08003121 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003122 // Consume the device reset notification generated when adding a new device.
3123 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003124 }
3125
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003126 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003127 SetUp(DEVICE_CLASSES);
3128 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003129
Chris Yea52ade12020-08-27 16:49:20 -07003130 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003131 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003132 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003133 }
3134
3135 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003136 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003137 }
3138
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003139 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00003140 if (!changes ||
3141 (changes &
3142 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
3143 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003144 mReader->requestRefreshConfiguration(changes);
3145 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08003146 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003147 std::list<NotifyArgs> out =
3148 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003149 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003150 for (const NotifyArgs& args : out) {
3151 mFakeListener->notify(args);
3152 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003153 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003154 return out;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003155 }
3156
arthurhungdcef2dc2020-08-11 14:47:50 +08003157 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
3158 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003159 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003160 InputDeviceIdentifier identifier;
3161 identifier.name = name;
3162 identifier.location = location;
3163 std::shared_ptr<InputDevice> device =
3164 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
3165 identifier);
3166 mReader->pushNextDevice(device);
3167 mFakeEventHub->addDevice(eventHubId, name, classes);
3168 mReader->loopOnce();
3169 return device;
3170 }
3171
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003172 template <class T, typename... Args>
3173 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003174 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003175 configureDevice(0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003176 std::list<NotifyArgs> resetArgList = mDevice->reset(ARBITRARY_TIME);
3177 resetArgList += mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003178 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003179 for (const NotifyArgs& loopArgs : resetArgList) {
3180 mFakeListener->notify(loopArgs);
3181 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003182 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003183 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003184 }
3185
3186 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003187 int32_t orientation, const std::string& uniqueId,
3188 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003189 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3190 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003191 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3192 }
3193
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003194 void clearViewports() {
3195 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003196 }
3197
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003198 std::list<NotifyArgs> process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type,
3199 int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003200 RawEvent event;
3201 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003202 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003203 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003204 event.type = type;
3205 event.code = code;
3206 event.value = value;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003207 std::list<NotifyArgs> processArgList = mapper.process(&event);
3208 for (const NotifyArgs& args : processArgList) {
3209 mFakeListener->notify(args);
3210 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003211 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003212 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003213 return processArgList;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003214 }
3215
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00003216 void resetMapper(InputMapper& mapper, nsecs_t when) {
3217 const auto resetArgs = mapper.reset(when);
3218 for (const auto args : resetArgs) {
3219 mFakeListener->notify(args);
3220 }
3221 // Loop the reader to flush the input listener queue.
3222 mReader->loopOnce();
3223 }
3224
Michael Wrightd02c5b62014-02-10 15:10:22 -08003225 static void assertMotionRange(const InputDeviceInfo& info,
3226 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3227 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003228 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003229 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3230 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3231 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3232 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3233 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3234 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3235 }
3236
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003237 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3238 float size, float touchMajor, float touchMinor, float toolMajor,
3239 float toolMinor, float orientation, float distance,
3240 float scaledAxisEpsilon = 1.f) {
3241 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3242 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003243 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3244 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003245 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3246 scaledAxisEpsilon);
3247 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3248 scaledAxisEpsilon);
3249 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3250 scaledAxisEpsilon);
3251 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3252 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003253 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3254 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3255 }
3256
Michael Wright17db18e2020-06-26 20:51:44 +01003257 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003258 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003259 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003260 ASSERT_NEAR(x, actualX, 1);
3261 ASSERT_NEAR(y, actualY, 1);
3262 }
3263};
3264
3265const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003266const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003267const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003268const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3269const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003270const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3271 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003272const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273
3274// --- SwitchInputMapperTest ---
3275
3276class SwitchInputMapperTest : public InputMapperTest {
3277protected:
3278};
3279
3280TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003281 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003283 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003284}
3285
3286TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003287 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003288
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003289 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003290 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003292 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003293 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003294}
3295
3296TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003297 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003298 std::list<NotifyArgs> out;
3299 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3300 ASSERT_TRUE(out.empty());
3301 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3302 ASSERT_TRUE(out.empty());
3303 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3304 ASSERT_TRUE(out.empty());
3305 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003306
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003307 ASSERT_EQ(1u, out.size());
3308 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003309 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003310 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3311 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003312 args.switchMask);
3313 ASSERT_EQ(uint32_t(0), args.policyFlags);
3314}
3315
Chris Ye87143712020-11-10 05:05:58 +00003316// --- VibratorInputMapperTest ---
3317class VibratorInputMapperTest : public InputMapperTest {
3318protected:
3319 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3320};
3321
3322TEST_F(VibratorInputMapperTest, GetSources) {
3323 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3324
3325 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3326}
3327
3328TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3329 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3330
3331 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3332}
3333
3334TEST_F(VibratorInputMapperTest, Vibrate) {
3335 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003336 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003337 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3338
3339 VibrationElement pattern(2);
3340 VibrationSequence sequence(2);
3341 pattern.duration = std::chrono::milliseconds(200);
3342 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3343 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3344 sequence.addElement(pattern);
3345 pattern.duration = std::chrono::milliseconds(500);
3346 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3347 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3348 sequence.addElement(pattern);
3349
3350 std::vector<int64_t> timings = {0, 1};
3351 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3352
3353 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003354 // Start vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003355 std::list<NotifyArgs> out = mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003356 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003357 // Verify vibrator state listener was notified.
3358 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003359 ASSERT_EQ(1u, out.size());
3360 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3361 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
3362 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08003363 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003364 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08003365 ASSERT_FALSE(mapper.isVibrating());
3366 // Verify vibrator state listener was notified.
3367 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003368 ASSERT_EQ(1u, out.size());
3369 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3370 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
3371 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003372}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003373
Chris Yef59a2f42020-10-16 12:55:26 -07003374// --- SensorInputMapperTest ---
3375
3376class SensorInputMapperTest : public InputMapperTest {
3377protected:
3378 static const int32_t ACCEL_RAW_MIN;
3379 static const int32_t ACCEL_RAW_MAX;
3380 static const int32_t ACCEL_RAW_FUZZ;
3381 static const int32_t ACCEL_RAW_FLAT;
3382 static const int32_t ACCEL_RAW_RESOLUTION;
3383
3384 static const int32_t GYRO_RAW_MIN;
3385 static const int32_t GYRO_RAW_MAX;
3386 static const int32_t GYRO_RAW_FUZZ;
3387 static const int32_t GYRO_RAW_FLAT;
3388 static const int32_t GYRO_RAW_RESOLUTION;
3389
3390 static const float GRAVITY_MS2_UNIT;
3391 static const float DEGREE_RADIAN_UNIT;
3392
3393 void prepareAccelAxes();
3394 void prepareGyroAxes();
3395 void setAccelProperties();
3396 void setGyroProperties();
3397 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3398};
3399
3400const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3401const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3402const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3403const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3404const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3405
3406const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3407const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3408const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3409const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3410const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3411
3412const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3413const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3414
3415void SensorInputMapperTest::prepareAccelAxes() {
3416 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3417 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3418 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3419 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3420 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3421 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3422}
3423
3424void SensorInputMapperTest::prepareGyroAxes() {
3425 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3426 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3427 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3428 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3429 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3430 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3431}
3432
3433void SensorInputMapperTest::setAccelProperties() {
3434 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3435 /* sensorDataIndex */ 0);
3436 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3437 /* sensorDataIndex */ 1);
3438 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3439 /* sensorDataIndex */ 2);
3440 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3441 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3442 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3443 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3444 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3445}
3446
3447void SensorInputMapperTest::setGyroProperties() {
3448 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3449 /* sensorDataIndex */ 0);
3450 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3451 /* sensorDataIndex */ 1);
3452 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3453 /* sensorDataIndex */ 2);
3454 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3455 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3456 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3457 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3458 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3459}
3460
3461TEST_F(SensorInputMapperTest, GetSources) {
3462 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3463
3464 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3465}
3466
3467TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3468 setAccelProperties();
3469 prepareAccelAxes();
3470 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3471
3472 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3473 std::chrono::microseconds(10000),
3474 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003475 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3477 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3478 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3479 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3480 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003481
3482 NotifySensorArgs args;
3483 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3484 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3485 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3486
3487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3488 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3489 ASSERT_EQ(args.deviceId, DEVICE_ID);
3490 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3491 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3492 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3493 ASSERT_EQ(args.values, values);
3494 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3495}
3496
3497TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3498 setGyroProperties();
3499 prepareGyroAxes();
3500 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3501
3502 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3503 std::chrono::microseconds(10000),
3504 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003505 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003506 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3507 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3508 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3509 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3510 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003511
3512 NotifySensorArgs args;
3513 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3514 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3515 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3516
3517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3518 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3519 ASSERT_EQ(args.deviceId, DEVICE_ID);
3520 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3521 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3522 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3523 ASSERT_EQ(args.values, values);
3524 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3525}
3526
Michael Wrightd02c5b62014-02-10 15:10:22 -08003527// --- KeyboardInputMapperTest ---
3528
3529class KeyboardInputMapperTest : public InputMapperTest {
3530protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003531 const std::string UNIQUE_ID = "local:0";
3532
3533 void prepareDisplay(int32_t orientation);
3534
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003535 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003536 int32_t originalKeyCode, int32_t rotatedKeyCode,
3537 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003538};
3539
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003540/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3541 * orientation.
3542 */
3543void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003544 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3545 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003546}
3547
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003548void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003549 int32_t originalScanCode, int32_t originalKeyCode,
3550 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003551 NotifyKeyArgs args;
3552
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003553 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3555 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3556 ASSERT_EQ(originalScanCode, args.scanCode);
3557 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003558 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003559
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003560 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3562 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3563 ASSERT_EQ(originalScanCode, args.scanCode);
3564 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003565 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003566}
3567
Michael Wrightd02c5b62014-02-10 15:10:22 -08003568TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003569 KeyboardInputMapper& mapper =
3570 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3571 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003572
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003573 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003574}
3575
3576TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3577 const int32_t USAGE_A = 0x070004;
3578 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003579 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3580 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003581 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3582 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3583 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003584
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003585 KeyboardInputMapper& mapper =
3586 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3587 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003588 // Initial metastate is AMETA_NONE.
3589 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003590
3591 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003592 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003593 NotifyKeyArgs args;
3594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3595 ASSERT_EQ(DEVICE_ID, args.deviceId);
3596 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3597 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3598 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3599 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3600 ASSERT_EQ(KEY_HOME, args.scanCode);
3601 ASSERT_EQ(AMETA_NONE, args.metaState);
3602 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3603 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3604 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3605
3606 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003607 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3609 ASSERT_EQ(DEVICE_ID, args.deviceId);
3610 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3611 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3612 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3613 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3614 ASSERT_EQ(KEY_HOME, args.scanCode);
3615 ASSERT_EQ(AMETA_NONE, args.metaState);
3616 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3617 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3618 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3619
3620 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003621 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3622 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3624 ASSERT_EQ(DEVICE_ID, args.deviceId);
3625 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3626 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3627 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3628 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3629 ASSERT_EQ(0, args.scanCode);
3630 ASSERT_EQ(AMETA_NONE, args.metaState);
3631 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3632 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3633 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3634
3635 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003636 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3637 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3639 ASSERT_EQ(DEVICE_ID, args.deviceId);
3640 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3641 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3642 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3643 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3644 ASSERT_EQ(0, args.scanCode);
3645 ASSERT_EQ(AMETA_NONE, args.metaState);
3646 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3647 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3648 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3649
3650 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003651 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3652 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3654 ASSERT_EQ(DEVICE_ID, args.deviceId);
3655 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3656 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3657 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3658 ASSERT_EQ(0, args.keyCode);
3659 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3660 ASSERT_EQ(AMETA_NONE, args.metaState);
3661 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3662 ASSERT_EQ(0U, args.policyFlags);
3663 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3664
3665 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003666 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3667 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3669 ASSERT_EQ(DEVICE_ID, args.deviceId);
3670 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3671 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3672 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3673 ASSERT_EQ(0, args.keyCode);
3674 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3675 ASSERT_EQ(AMETA_NONE, args.metaState);
3676 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3677 ASSERT_EQ(0U, args.policyFlags);
3678 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3679}
3680
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003681/**
3682 * Ensure that the readTime is set to the time when the EV_KEY is received.
3683 */
3684TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3685 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3686
3687 KeyboardInputMapper& mapper =
3688 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3689 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3690 NotifyKeyArgs args;
3691
3692 // Key down
3693 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3695 ASSERT_EQ(12, args.readTime);
3696
3697 // Key up
3698 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3700 ASSERT_EQ(15, args.readTime);
3701}
3702
Michael Wrightd02c5b62014-02-10 15:10:22 -08003703TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003704 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3705 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003706 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3707 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3708 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003709
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003710 KeyboardInputMapper& mapper =
3711 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3712 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003713
Arthur Hung95f68612022-04-07 14:08:22 +08003714 // Initial metastate is AMETA_NONE.
3715 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003716
3717 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003718 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003719 NotifyKeyArgs args;
3720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3721 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003722 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003723 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003724
3725 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003726 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3728 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003729 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003730
3731 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003732 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3734 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003735 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003736
3737 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003738 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3740 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003741 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003742 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003743}
3744
3745TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003746 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3747 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3748 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3749 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003750
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003751 KeyboardInputMapper& mapper =
3752 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3753 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003754
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003755 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003756 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3757 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3758 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3759 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3760 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3761 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3762 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3763 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3764}
3765
3766TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003767 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3768 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3769 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3770 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003771
Michael Wrightd02c5b62014-02-10 15:10:22 -08003772 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003773 KeyboardInputMapper& mapper =
3774 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3775 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003776
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003777 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003778 ASSERT_NO_FATAL_FAILURE(
3779 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3780 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3781 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3782 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3783 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3784 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3785 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003786
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003787 clearViewports();
3788 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003789 ASSERT_NO_FATAL_FAILURE(
3790 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3791 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3792 AKEYCODE_DPAD_UP, DISPLAY_ID));
3793 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3794 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3795 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3796 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003797
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003798 clearViewports();
3799 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003800 ASSERT_NO_FATAL_FAILURE(
3801 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3802 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3803 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3804 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3805 AKEYCODE_DPAD_UP, DISPLAY_ID));
3806 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3807 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003808
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003809 clearViewports();
3810 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003811 ASSERT_NO_FATAL_FAILURE(
3812 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3813 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3814 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3815 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3816 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3817 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3818 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003819
3820 // Special case: if orientation changes while key is down, we still emit the same keycode
3821 // in the key up as we did in the key down.
3822 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003823 clearViewports();
3824 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003825 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3827 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3828 ASSERT_EQ(KEY_UP, args.scanCode);
3829 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3830
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003831 clearViewports();
3832 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003833 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3835 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3836 ASSERT_EQ(KEY_UP, args.scanCode);
3837 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3838}
3839
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003840TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3841 // If the keyboard is not orientation aware,
3842 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003843 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003844
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003845 KeyboardInputMapper& mapper =
3846 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3847 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003848 NotifyKeyArgs args;
3849
3850 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003851 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003853 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3855 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3856
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003857 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003858 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3862 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3863}
3864
3865TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3866 // If the keyboard is orientation aware,
3867 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003868 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003869
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003870 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003871 KeyboardInputMapper& mapper =
3872 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3873 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003874 NotifyKeyArgs args;
3875
3876 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3877 // ^--- already checked by the previous test
3878
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003879 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003880 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003881 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003883 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3885 ASSERT_EQ(DISPLAY_ID, args.displayId);
3886
3887 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003888 clearViewports();
3889 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003890 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003891 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003893 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3895 ASSERT_EQ(newDisplayId, args.displayId);
3896}
3897
Michael Wrightd02c5b62014-02-10 15:10:22 -08003898TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003899 KeyboardInputMapper& mapper =
3900 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3901 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003902
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003903 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003904 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003905
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003906 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003907 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003908}
3909
Philip Junker4af3b3d2021-12-14 10:36:55 +01003910TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3911 KeyboardInputMapper& mapper =
3912 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3913 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3914
3915 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3916 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3917 << "If a mapping is available, the result is equal to the mapping";
3918
3919 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3920 << "If no mapping is available, the result is the key location";
3921}
3922
Michael Wrightd02c5b62014-02-10 15:10:22 -08003923TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003924 KeyboardInputMapper& mapper =
3925 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3926 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003927
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003928 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003929 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003930
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003931 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003932 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003933}
3934
3935TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003936 KeyboardInputMapper& mapper =
3937 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3938 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003939
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003940 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003941
Michael Wrightd02c5b62014-02-10 15:10:22 -08003942 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003943 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003944 ASSERT_TRUE(flags[0]);
3945 ASSERT_FALSE(flags[1]);
3946}
3947
3948TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003949 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3950 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3951 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3952 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3953 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3954 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003955
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003956 KeyboardInputMapper& mapper =
3957 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3958 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003959 // Initial metastate is AMETA_NONE.
3960 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003961
3962 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003963 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3964 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3965 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003966
3967 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003968 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3969 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003970 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3971 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3972 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003973 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003974
3975 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003976 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3977 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003978 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3979 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3980 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003981 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003982
3983 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003984 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3985 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003986 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3987 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3988 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003989 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003990
3991 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003992 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003994 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3995 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3996 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003997 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003998
3999 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004000 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4001 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004002 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4003 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4004 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004005 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004006
4007 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004008 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4009 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004010 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4011 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4012 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004013 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004014}
4015
Chris Yea52ade12020-08-27 16:49:20 -07004016TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
4017 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
4018 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
4019 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
4020 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
4021
4022 KeyboardInputMapper& mapper =
4023 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4024 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4025
Chris Yea52ade12020-08-27 16:49:20 -07004026 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004027 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07004028 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4029 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
4030 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
4031 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4032
4033 NotifyKeyArgs args;
4034 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004035 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07004036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4037 ASSERT_EQ(AMETA_NONE, args.metaState);
4038 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4039 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4040 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
4041
4042 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004043 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07004044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4045 ASSERT_EQ(AMETA_NONE, args.metaState);
4046 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4047 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4048 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
4049}
4050
Arthur Hung2c9a3342019-07-23 14:18:59 +08004051TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
4052 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004053 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4054 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4055 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4056 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004057
4058 // keyboard 2.
4059 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08004060 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08004061 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004062 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08004063 std::shared_ptr<InputDevice> device2 =
4064 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004065 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08004066
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004067 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4068 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4069 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4070 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004071
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004072 KeyboardInputMapper& mapper =
4073 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4074 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004075
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004076 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004077 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004078 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004079 std::list<NotifyArgs> unused =
4080 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4081 0 /*changes*/);
4082 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004083
4084 // Prepared displays and associated info.
4085 constexpr uint8_t hdmi1 = 0;
4086 constexpr uint8_t hdmi2 = 1;
4087 const std::string SECONDARY_UNIQUE_ID = "local:1";
4088
4089 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
4090 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
4091
4092 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004093 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4094 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004095 ASSERT_FALSE(device2->isEnabled());
4096
4097 // Prepare second display.
4098 constexpr int32_t newDisplayId = 2;
4099 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004100 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004101 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004102 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004103 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004104 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4105 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004106
4107 // Device should be enabled after the associated display is found.
4108 ASSERT_TRUE(mDevice->isEnabled());
4109 ASSERT_TRUE(device2->isEnabled());
4110
4111 // Test pad key events
4112 ASSERT_NO_FATAL_FAILURE(
4113 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
4114 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4115 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4116 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4117 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4118 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4119 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4120
4121 ASSERT_NO_FATAL_FAILURE(
4122 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
4123 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4124 AKEYCODE_DPAD_RIGHT, newDisplayId));
4125 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4126 AKEYCODE_DPAD_DOWN, newDisplayId));
4127 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4128 AKEYCODE_DPAD_LEFT, newDisplayId));
4129}
Michael Wrightd02c5b62014-02-10 15:10:22 -08004130
arthurhungc903df12020-08-11 15:08:42 +08004131TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
4132 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4133 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4134 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4135 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4136 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4137 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4138
4139 KeyboardInputMapper& mapper =
4140 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4141 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004142 // Initial metastate is AMETA_NONE.
4143 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004144
4145 // Initialization should have turned all of the lights off.
4146 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4147 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4148 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4149
4150 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004151 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4152 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004153 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4154 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4155
4156 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004157 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4158 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004159 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4160 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
4161
4162 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004165 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4166 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
4167
4168 mFakeEventHub->removeDevice(EVENTHUB_ID);
4169 mReader->loopOnce();
4170
4171 // keyboard 2 should default toggle keys.
4172 const std::string USB2 = "USB2";
4173 const std::string DEVICE_NAME2 = "KEYBOARD2";
4174 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4175 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4176 std::shared_ptr<InputDevice> device2 =
4177 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004178 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08004179 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4180 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4181 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4182 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4183 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4184 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4185
arthurhung6fe95782020-10-05 22:41:16 +08004186 KeyboardInputMapper& mapper2 =
4187 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4188 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004189 std::list<NotifyArgs> unused =
4190 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4191 0 /*changes*/);
4192 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08004193
4194 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4195 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4196 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004197 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4198 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004199}
4200
Arthur Hungcb40a002021-08-03 14:31:01 +00004201TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4202 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4203 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4204 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4205
4206 // Suppose we have two mappers. (DPAD + KEYBOARD)
4207 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4208 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4209 KeyboardInputMapper& mapper =
4210 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4211 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004212 // Initial metastate is AMETA_NONE.
4213 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004214
4215 mReader->toggleCapsLockState(DEVICE_ID);
4216 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4217}
4218
Arthur Hungfb3cc112022-04-13 07:39:50 +00004219TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4220 // keyboard 1.
4221 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4222 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4223 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4224 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4225 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4226 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4227
4228 KeyboardInputMapper& mapper1 =
4229 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4230 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4231
4232 // keyboard 2.
4233 const std::string USB2 = "USB2";
4234 const std::string DEVICE_NAME2 = "KEYBOARD2";
4235 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4236 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4237 std::shared_ptr<InputDevice> device2 =
4238 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4239 ftl::Flags<InputDeviceClass>(0));
4240 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4241 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4242 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4243 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4244 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4245 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4246
4247 KeyboardInputMapper& mapper2 =
4248 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4249 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004250 std::list<NotifyArgs> unused =
4251 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4252 0 /*changes*/);
4253 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004254
Arthur Hung95f68612022-04-07 14:08:22 +08004255 // Initial metastate is AMETA_NONE.
4256 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4257 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4258
4259 // Toggle num lock on and off.
4260 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4261 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004262 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4263 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4264 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4265
4266 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4267 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4268 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4269 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4270 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4271
4272 // Toggle caps lock on and off.
4273 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4274 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4275 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4276 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4277 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4278
4279 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4280 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4281 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4282 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4283 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4284
4285 // Toggle scroll lock on and off.
4286 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4287 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4288 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4289 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4290 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4291
4292 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4293 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4294 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4295 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4296 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4297}
4298
Arthur Hung2141d542022-08-23 07:45:21 +00004299TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4300 const int32_t USAGE_A = 0x070004;
4301 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4302 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4303
4304 KeyboardInputMapper& mapper =
4305 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4306 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4307 // Key down by scan code.
4308 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4309 NotifyKeyArgs args;
4310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4311 ASSERT_EQ(DEVICE_ID, args.deviceId);
4312 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4313 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4314 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4315 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4316 ASSERT_EQ(KEY_HOME, args.scanCode);
4317 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4318
4319 // Disable device, it should synthesize cancellation events for down events.
4320 mFakePolicy->addDisabledDevice(DEVICE_ID);
4321 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
4322
4323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4324 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4325 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4326 ASSERT_EQ(KEY_HOME, args.scanCode);
4327 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4328}
4329
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004330// --- KeyboardInputMapperTest_ExternalDevice ---
4331
4332class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4333protected:
Chris Yea52ade12020-08-27 16:49:20 -07004334 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004335};
4336
4337TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004338 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4339 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004340
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004341 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4342 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4343 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4344 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004345
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004346 KeyboardInputMapper& mapper =
4347 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4348 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004349
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004350 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004351 NotifyKeyArgs args;
4352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4353 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4354
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004355 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4357 ASSERT_EQ(uint32_t(0), args.policyFlags);
4358
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004359 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4361 ASSERT_EQ(uint32_t(0), args.policyFlags);
4362
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004363 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4365 ASSERT_EQ(uint32_t(0), args.policyFlags);
4366
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004367 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4369 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4370
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004371 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4373 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4374}
4375
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004376TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004377 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004378
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004379 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4380 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4381 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004382
Powei Fengd041c5d2019-05-03 17:11:33 -07004383 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004384 KeyboardInputMapper& mapper =
4385 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4386 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004387
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004388 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004389 NotifyKeyArgs args;
4390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4391 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4392
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004393 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004394 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, READ_TIME, EV_KEY, KEY_DOWN, 1);
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 + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
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, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4407 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4408
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004409 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4411 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4412}
4413
Michael Wrightd02c5b62014-02-10 15:10:22 -08004414// --- CursorInputMapperTest ---
4415
4416class CursorInputMapperTest : public InputMapperTest {
4417protected:
4418 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4419
Michael Wright17db18e2020-06-26 20:51:44 +01004420 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004421
Chris Yea52ade12020-08-27 16:49:20 -07004422 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004423 InputMapperTest::SetUp();
4424
Michael Wright17db18e2020-06-26 20:51:44 +01004425 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004426 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004427 }
4428
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004429 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4430 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004431
4432 void prepareDisplay(int32_t orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004433 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4434 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4435 }
4436
4437 void prepareSecondaryDisplay() {
4438 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4439 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
4440 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004441 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004442
4443 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4444 float pressure) {
4445 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4446 0.0f, 0.0f, 0.0f, EPSILON));
4447 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004448};
4449
4450const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4451
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004452void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4453 int32_t originalY, int32_t rotatedX,
4454 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004455 NotifyMotionArgs args;
4456
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4458 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4459 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4461 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004462 ASSERT_NO_FATAL_FAILURE(
4463 assertCursorPointerCoords(args.pointerCoords[0],
4464 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4465 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466}
4467
4468TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004470 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004471
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004472 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004473}
4474
4475TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004476 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004477 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004478
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004479 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004480}
4481
4482TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004483 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004484 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004485
4486 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004487 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488
4489 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004490 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4491 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004492 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4493 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4494
4495 // When the bounds are set, then there should be a valid motion range.
4496 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4497
4498 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004499 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004500
4501 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4502 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4503 1, 800 - 1, 0.0f, 0.0f));
4504 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4505 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4506 2, 480 - 1, 0.0f, 0.0f));
4507 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4508 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4509 0.0f, 1.0f, 0.0f, 0.0f));
4510}
4511
4512TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004513 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004514 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004515
4516 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004517 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004518
4519 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4520 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4521 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4522 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4523 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4524 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4525 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4526 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4527 0.0f, 1.0f, 0.0f, 0.0f));
4528}
4529
4530TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004531 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004532 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004533
arthurhungdcef2dc2020-08-11 14:47:50 +08004534 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004535
4536 NotifyMotionArgs args;
4537
4538 // Button press.
4539 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004540 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4541 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4543 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4544 ASSERT_EQ(DEVICE_ID, args.deviceId);
4545 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4546 ASSERT_EQ(uint32_t(0), args.policyFlags);
4547 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4548 ASSERT_EQ(0, args.flags);
4549 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4550 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4551 ASSERT_EQ(0, args.edgeFlags);
4552 ASSERT_EQ(uint32_t(1), args.pointerCount);
4553 ASSERT_EQ(0, args.pointerProperties[0].id);
4554 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004555 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004556 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4557 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4558 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4559
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4561 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4562 ASSERT_EQ(DEVICE_ID, args.deviceId);
4563 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4564 ASSERT_EQ(uint32_t(0), args.policyFlags);
4565 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4566 ASSERT_EQ(0, args.flags);
4567 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4568 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4569 ASSERT_EQ(0, args.edgeFlags);
4570 ASSERT_EQ(uint32_t(1), args.pointerCount);
4571 ASSERT_EQ(0, args.pointerProperties[0].id);
4572 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004573 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004574 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4575 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4576 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4577
Michael Wrightd02c5b62014-02-10 15:10:22 -08004578 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004579 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4580 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4582 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4583 ASSERT_EQ(DEVICE_ID, args.deviceId);
4584 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4585 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004586 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4587 ASSERT_EQ(0, args.flags);
4588 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4589 ASSERT_EQ(0, args.buttonState);
4590 ASSERT_EQ(0, args.edgeFlags);
4591 ASSERT_EQ(uint32_t(1), args.pointerCount);
4592 ASSERT_EQ(0, args.pointerProperties[0].id);
4593 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004594 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004595 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4596 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4597 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4598
4599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4600 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4601 ASSERT_EQ(DEVICE_ID, args.deviceId);
4602 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4603 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004604 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4605 ASSERT_EQ(0, args.flags);
4606 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4607 ASSERT_EQ(0, args.buttonState);
4608 ASSERT_EQ(0, args.edgeFlags);
4609 ASSERT_EQ(uint32_t(1), args.pointerCount);
4610 ASSERT_EQ(0, args.pointerProperties[0].id);
4611 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004612 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004613 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4614 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4615 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4616}
4617
4618TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004619 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004620 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004621
4622 NotifyMotionArgs args;
4623
4624 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004625 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4626 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4628 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004629 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4630 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4631 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632
4633 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004634 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4635 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4637 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004638 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4639 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004640}
4641
4642TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004643 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004644 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004645
4646 NotifyMotionArgs args;
4647
4648 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004649 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4650 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4652 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004653 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004654
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4656 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004657 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004658
Michael Wrightd02c5b62014-02-10 15:10:22 -08004659 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004660 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4661 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004663 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004664 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004665
4666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004667 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004668 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004669}
4670
4671TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004672 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004673 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004674
4675 NotifyMotionArgs args;
4676
4677 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004678 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4679 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4680 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4681 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4683 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004684 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4685 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4686 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004687
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4689 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004690 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4691 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4692 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004693
Michael Wrightd02c5b62014-02-10 15:10:22 -08004694 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004695 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4696 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4697 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4699 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004700 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4701 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4702 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004703
4704 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004705 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4706 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004708 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004709 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004710
4711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004712 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004713 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004714}
4715
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004716TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004717 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004718 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004719 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4720 // need to be rotated.
4721 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004722 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004723
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004724 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004725 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4726 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4727 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4728 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4729 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4730 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4731 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4732 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4733}
4734
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004735TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004736 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004737 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004738 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4739 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004740 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004741
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004742 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004743 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004744 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4745 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4746 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4747 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4748 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4749 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4750 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4751 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4752
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004753 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004754 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004755 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4756 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4757 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4758 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4759 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4760 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4761 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4762 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004763
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004764 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004765 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004766 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4767 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4768 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4769 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4770 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4771 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4772 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4773 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4774
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004775 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004776 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004777 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4778 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4779 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4780 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4781 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4782 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4783 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4784 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004785}
4786
4787TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004788 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004789 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004790
4791 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4792 mFakePointerController->setPosition(100, 200);
4793 mFakePointerController->setButtonState(0);
4794
4795 NotifyMotionArgs motionArgs;
4796 NotifyKeyArgs keyArgs;
4797
4798 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4800 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4802 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4803 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4804 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004805 ASSERT_NO_FATAL_FAILURE(
4806 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004807
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4809 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4810 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4811 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004812 ASSERT_NO_FATAL_FAILURE(
4813 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004814
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004815 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4816 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004818 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004819 ASSERT_EQ(0, motionArgs.buttonState);
4820 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004821 ASSERT_NO_FATAL_FAILURE(
4822 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004823
4824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004825 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004826 ASSERT_EQ(0, motionArgs.buttonState);
4827 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004828 ASSERT_NO_FATAL_FAILURE(
4829 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004830
4831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004832 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004833 ASSERT_EQ(0, motionArgs.buttonState);
4834 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004835 ASSERT_NO_FATAL_FAILURE(
4836 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004837
4838 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004839 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4840 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4841 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4843 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4844 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4845 motionArgs.buttonState);
4846 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4847 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004848 ASSERT_NO_FATAL_FAILURE(
4849 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004850
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4852 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4853 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4854 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4855 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004856 ASSERT_NO_FATAL_FAILURE(
4857 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004858
4859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4860 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4861 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4862 motionArgs.buttonState);
4863 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4864 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004865 ASSERT_NO_FATAL_FAILURE(
4866 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004867
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004868 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4869 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004871 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004872 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4873 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004874 ASSERT_NO_FATAL_FAILURE(
4875 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004876
4877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004878 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004879 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4880 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004881 ASSERT_NO_FATAL_FAILURE(
4882 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004883
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004884 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4885 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004887 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4888 ASSERT_EQ(0, motionArgs.buttonState);
4889 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004890 ASSERT_NO_FATAL_FAILURE(
4891 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004892 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4893 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004894
4895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004896 ASSERT_EQ(0, motionArgs.buttonState);
4897 ASSERT_EQ(0, mFakePointerController->getButtonState());
4898 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004899 ASSERT_NO_FATAL_FAILURE(
4900 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004901
Michael Wrightd02c5b62014-02-10 15:10:22 -08004902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4903 ASSERT_EQ(0, motionArgs.buttonState);
4904 ASSERT_EQ(0, mFakePointerController->getButtonState());
4905 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004906 ASSERT_NO_FATAL_FAILURE(
4907 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004908
4909 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004910 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
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->assertNotifyKeyWasCalled(&keyArgs));
4913 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4914 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004915
Michael Wrightd02c5b62014-02-10 15:10:22 -08004916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004917 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004918 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4919 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004920 ASSERT_NO_FATAL_FAILURE(
4921 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004922
4923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4924 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4925 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4926 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004927 ASSERT_NO_FATAL_FAILURE(
4928 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004929
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004930 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4931 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004933 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004934 ASSERT_EQ(0, motionArgs.buttonState);
4935 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004936 ASSERT_NO_FATAL_FAILURE(
4937 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004938
4939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004940 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004941 ASSERT_EQ(0, motionArgs.buttonState);
4942 ASSERT_EQ(0, mFakePointerController->getButtonState());
4943
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004944 ASSERT_NO_FATAL_FAILURE(
4945 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4947 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4948 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4949
4950 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004951 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4952 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4954 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4955 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004956
Michael Wrightd02c5b62014-02-10 15:10:22 -08004957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004958 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004959 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4960 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004961 ASSERT_NO_FATAL_FAILURE(
4962 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004963
4964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4965 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4966 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4967 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004968 ASSERT_NO_FATAL_FAILURE(
4969 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004970
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004971 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4972 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004974 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004975 ASSERT_EQ(0, motionArgs.buttonState);
4976 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004977 ASSERT_NO_FATAL_FAILURE(
4978 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004979
4980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4981 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4982 ASSERT_EQ(0, motionArgs.buttonState);
4983 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004984 ASSERT_NO_FATAL_FAILURE(
4985 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004986
Michael Wrightd02c5b62014-02-10 15:10:22 -08004987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4988 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4989 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4990
4991 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004992 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4995 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4996 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004997
Michael Wrightd02c5b62014-02-10 15:10:22 -08004998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004999 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005000 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5001 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005002 ASSERT_NO_FATAL_FAILURE(
5003 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005004
5005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5006 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5007 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5008 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005009 ASSERT_NO_FATAL_FAILURE(
5010 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005011
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005012 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
5013 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005015 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005016 ASSERT_EQ(0, motionArgs.buttonState);
5017 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005018 ASSERT_NO_FATAL_FAILURE(
5019 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005020
5021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5022 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5023 ASSERT_EQ(0, motionArgs.buttonState);
5024 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005025 ASSERT_NO_FATAL_FAILURE(
5026 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005027
Michael Wrightd02c5b62014-02-10 15:10:22 -08005028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5029 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5030 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5031
5032 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005033 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
5034 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5036 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5037 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005038
Michael Wrightd02c5b62014-02-10 15:10:22 -08005039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005040 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005041 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5042 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005043 ASSERT_NO_FATAL_FAILURE(
5044 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005045
5046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5047 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5048 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5049 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005050 ASSERT_NO_FATAL_FAILURE(
5051 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005052
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005053 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
5054 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005056 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005057 ASSERT_EQ(0, motionArgs.buttonState);
5058 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005059 ASSERT_NO_FATAL_FAILURE(
5060 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005061
5062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5063 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5064 ASSERT_EQ(0, motionArgs.buttonState);
5065 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005066 ASSERT_NO_FATAL_FAILURE(
5067 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005068
Michael Wrightd02c5b62014-02-10 15:10:22 -08005069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5070 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5071 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5072}
5073
5074TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005075 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005076 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005077
5078 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5079 mFakePointerController->setPosition(100, 200);
5080 mFakePointerController->setButtonState(0);
5081
5082 NotifyMotionArgs args;
5083
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005084 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5085 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5086 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005088 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5089 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5090 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5091 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 +01005092 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005093}
5094
5095TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005096 addConfigurationProperty("cursor.mode", "pointer");
5097 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005098 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005099
5100 NotifyDeviceResetArgs resetArgs;
5101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5102 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5103 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5104
5105 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5106 mFakePointerController->setPosition(100, 200);
5107 mFakePointerController->setButtonState(0);
5108
5109 NotifyMotionArgs args;
5110
5111 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005112 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5113 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5114 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5116 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5117 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5118 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5119 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 +01005120 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005121
5122 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5124 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5126 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5127 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5128 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5129 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5131 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5132 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
5133 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5134 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5135
5136 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005137 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5138 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5140 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5141 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
5142 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5143 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5145 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5146 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
5147 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5148 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5149
5150 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005151 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
5152 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
5153 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5155 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5156 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5157 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5158 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 +01005159 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005160
5161 // Disable pointer capture and check that the device generation got bumped
5162 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08005163 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005164 mFakePolicy->setPointerCapture(false);
5165 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08005166 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005167
5168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005169 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5170
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005171 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5172 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5173 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5175 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005176 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5177 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5178 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 +01005179 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005180}
5181
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00005182/**
5183 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
5184 * pointer acceleration or speed processing should not be applied.
5185 */
5186TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
5187 addConfigurationProperty("cursor.mode", "pointer");
5188 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
5189 100.f /*high threshold*/, 10.f /*acceleration*/);
5190 mFakePolicy->setVelocityControlParams(testParams);
5191 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5192
5193 NotifyDeviceResetArgs resetArgs;
5194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5195 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5196 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5197
5198 NotifyMotionArgs args;
5199
5200 // Move and verify scale is applied.
5201 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5202 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5203 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5205 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5206 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5207 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5208 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5209 ASSERT_GT(relX, 10);
5210 ASSERT_GT(relY, 20);
5211
5212 // Enable Pointer Capture
5213 mFakePolicy->setPointerCapture(true);
5214 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5215 NotifyPointerCaptureChangedArgs captureArgs;
5216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5217 ASSERT_TRUE(captureArgs.request.enable);
5218
5219 // Move and verify scale is not applied.
5220 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5221 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5222 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5224 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5225 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5226 ASSERT_EQ(10, args.pointerCoords[0].getX());
5227 ASSERT_EQ(20, args.pointerCoords[0].getY());
5228}
5229
Prabir Pradhan208360b2022-06-24 18:37:04 +00005230TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5231 addConfigurationProperty("cursor.mode", "pointer");
5232 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5233
5234 NotifyDeviceResetArgs resetArgs;
5235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5236 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5237 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5238
5239 // Ensure the display is rotated.
5240 prepareDisplay(DISPLAY_ORIENTATION_90);
5241
5242 NotifyMotionArgs args;
5243
5244 // Verify that the coordinates are rotated.
5245 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5246 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5247 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5249 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5250 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5251 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5252 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
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 rotation 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 Pradhanc13ff082022-09-08 22:03:30 +00005272TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005273 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005274
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005275 // Set up the default display.
5276 prepareDisplay(DISPLAY_ORIENTATION_90);
5277
5278 // Set up the secondary display as the display on which the pointer should be shown.
5279 // The InputDevice is not associated with any display.
5280 prepareSecondaryDisplay();
5281 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08005282 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5283
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005284 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005285 mFakePointerController->setPosition(100, 200);
5286 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005287
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005288 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005289 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5290 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5291 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005293 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5294 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5295 WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01005296 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005297}
5298
5299TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
5300 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5301
5302 // Set up the default display.
5303 prepareDisplay(DISPLAY_ORIENTATION_90);
5304
5305 // Set up the secondary display as the display on which the pointer should be shown,
5306 // and associate the InputDevice with the secondary display.
5307 prepareSecondaryDisplay();
5308 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5309 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5310 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5311
5312 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5313 mFakePointerController->setPosition(100, 200);
5314 mFakePointerController->setButtonState(0);
5315
5316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5317 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5318 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005320 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5321 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5322 WithCoords(110.0f, 220.0f))));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005323 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
5324}
5325
5326TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
5327 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5328
5329 // Set up the default display as the display on which the pointer should be shown.
5330 prepareDisplay(DISPLAY_ORIENTATION_90);
5331 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5332
5333 // Associate the InputDevice with the secondary display.
5334 prepareSecondaryDisplay();
5335 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5336 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5337
5338 // The mapper should not generate any events because it is associated with a display that is
5339 // different from the pointer display.
5340 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5341 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5342 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005344}
5345
Michael Wrightd02c5b62014-02-10 15:10:22 -08005346// --- TouchInputMapperTest ---
5347
5348class TouchInputMapperTest : public InputMapperTest {
5349protected:
5350 static const int32_t RAW_X_MIN;
5351 static const int32_t RAW_X_MAX;
5352 static const int32_t RAW_Y_MIN;
5353 static const int32_t RAW_Y_MAX;
5354 static const int32_t RAW_TOUCH_MIN;
5355 static const int32_t RAW_TOUCH_MAX;
5356 static const int32_t RAW_TOOL_MIN;
5357 static const int32_t RAW_TOOL_MAX;
5358 static const int32_t RAW_PRESSURE_MIN;
5359 static const int32_t RAW_PRESSURE_MAX;
5360 static const int32_t RAW_ORIENTATION_MIN;
5361 static const int32_t RAW_ORIENTATION_MAX;
5362 static const int32_t RAW_DISTANCE_MIN;
5363 static const int32_t RAW_DISTANCE_MAX;
5364 static const int32_t RAW_TILT_MIN;
5365 static const int32_t RAW_TILT_MAX;
5366 static const int32_t RAW_ID_MIN;
5367 static const int32_t RAW_ID_MAX;
5368 static const int32_t RAW_SLOT_MIN;
5369 static const int32_t RAW_SLOT_MAX;
5370 static const float X_PRECISION;
5371 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005372 static const float X_PRECISION_VIRTUAL;
5373 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005374
5375 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005376 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005377
5378 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5379
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005380 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005381 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005382
Michael Wrightd02c5b62014-02-10 15:10:22 -08005383 enum Axes {
5384 POSITION = 1 << 0,
5385 TOUCH = 1 << 1,
5386 TOOL = 1 << 2,
5387 PRESSURE = 1 << 3,
5388 ORIENTATION = 1 << 4,
5389 MINOR = 1 << 5,
5390 ID = 1 << 6,
5391 DISTANCE = 1 << 7,
5392 TILT = 1 << 8,
5393 SLOT = 1 << 9,
5394 TOOL_TYPE = 1 << 10,
5395 };
5396
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005397 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5398 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005399 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005400 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005401 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005402 int32_t toRawX(float displayX);
5403 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005404 int32_t toRotatedRawX(float displayX);
5405 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005406 float toCookedX(float rawX, float rawY);
5407 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005408 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005409 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005410 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005411 float toDisplayY(int32_t rawY, int32_t displayHeight);
5412
Michael Wrightd02c5b62014-02-10 15:10:22 -08005413};
5414
5415const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5416const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5417const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5418const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5419const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5420const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5421const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5422const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005423const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5424const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005425const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5426const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5427const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5428const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5429const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5430const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5431const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5432const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5433const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5434const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5435const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5436const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005437const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5438 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5439const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5440 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005441const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5442 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005443
5444const float TouchInputMapperTest::GEOMETRIC_SCALE =
5445 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5446 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5447
5448const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5449 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5450 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5451};
5452
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005453void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005454 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5455 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005456}
5457
5458void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5459 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5460 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005461}
5462
Santos Cordonfa5cf462017-04-05 10:37:00 -07005463void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005464 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5465 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5466 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005467}
5468
Michael Wrightd02c5b62014-02-10 15:10:22 -08005469void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005470 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5471 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5472 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5473 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005474}
5475
Jason Gerecke489fda82012-09-07 17:19:40 -07005476void TouchInputMapperTest::prepareLocationCalibration() {
5477 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5478}
5479
Michael Wrightd02c5b62014-02-10 15:10:22 -08005480int32_t TouchInputMapperTest::toRawX(float displayX) {
5481 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5482}
5483
5484int32_t TouchInputMapperTest::toRawY(float displayY) {
5485 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5486}
5487
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005488int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5489 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5490}
5491
5492int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5493 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5494}
5495
Jason Gerecke489fda82012-09-07 17:19:40 -07005496float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5497 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5498 return rawX;
5499}
5500
5501float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5502 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5503 return rawY;
5504}
5505
Michael Wrightd02c5b62014-02-10 15:10:22 -08005506float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005507 return toDisplayX(rawX, DISPLAY_WIDTH);
5508}
5509
5510float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5511 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005512}
5513
5514float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005515 return toDisplayY(rawY, DISPLAY_HEIGHT);
5516}
5517
5518float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5519 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005520}
5521
5522
5523// --- SingleTouchInputMapperTest ---
5524
5525class SingleTouchInputMapperTest : public TouchInputMapperTest {
5526protected:
5527 void prepareButtons();
5528 void prepareAxes(int axes);
5529
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005530 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5531 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5532 void processUp(SingleTouchInputMapper& mappery);
5533 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5534 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5535 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5536 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5537 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5538 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005539};
5540
5541void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005542 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005543}
5544
5545void SingleTouchInputMapperTest::prepareAxes(int axes) {
5546 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005547 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5548 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005549 }
5550 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005551 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5552 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005553 }
5554 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005555 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5556 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005557 }
5558 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005559 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5560 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005561 }
5562 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005563 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5564 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005565 }
5566}
5567
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005568void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005569 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5570 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5571 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005572}
5573
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005574void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005575 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5576 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005577}
5578
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005579void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005580 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005581}
5582
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005583void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005584 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005585}
5586
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005587void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5588 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005589 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005590}
5591
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005592void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005593 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005594}
5595
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005596void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5597 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005598 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5599 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005600}
5601
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005602void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5603 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005604 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005605}
5606
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005607void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005608 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005609}
5610
Michael Wrightd02c5b62014-02-10 15:10:22 -08005611TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005612 prepareButtons();
5613 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005614 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005615
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005616 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005617}
5618
Michael Wrightd02c5b62014-02-10 15:10:22 -08005619TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005620 prepareButtons();
5621 prepareAxes(POSITION);
5622 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005623 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005624
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005625 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005626}
5627
5628TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005629 addConfigurationProperty("touch.deviceType", "touchScreen");
5630 prepareDisplay(DISPLAY_ORIENTATION_0);
5631 prepareButtons();
5632 prepareAxes(POSITION);
5633 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005634 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005635
5636 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005637 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005638
5639 // Virtual key is down.
5640 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5641 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5642 processDown(mapper, x, y);
5643 processSync(mapper);
5644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5645
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005646 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005647
5648 // Virtual key is up.
5649 processUp(mapper);
5650 processSync(mapper);
5651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5652
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005653 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005654}
5655
5656TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005657 addConfigurationProperty("touch.deviceType", "touchScreen");
5658 prepareDisplay(DISPLAY_ORIENTATION_0);
5659 prepareButtons();
5660 prepareAxes(POSITION);
5661 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005662 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005663
5664 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005665 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005666
5667 // Virtual key is down.
5668 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5669 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5670 processDown(mapper, x, y);
5671 processSync(mapper);
5672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5673
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005674 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005675
5676 // Virtual key is up.
5677 processUp(mapper);
5678 processSync(mapper);
5679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5680
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005681 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005682}
5683
5684TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005685 addConfigurationProperty("touch.deviceType", "touchScreen");
5686 prepareDisplay(DISPLAY_ORIENTATION_0);
5687 prepareButtons();
5688 prepareAxes(POSITION);
5689 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005690 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005691
Michael Wrightd02c5b62014-02-10 15:10:22 -08005692 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005693 ASSERT_TRUE(
5694 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005695 ASSERT_TRUE(flags[0]);
5696 ASSERT_FALSE(flags[1]);
5697}
5698
5699TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005700 addConfigurationProperty("touch.deviceType", "touchScreen");
5701 prepareDisplay(DISPLAY_ORIENTATION_0);
5702 prepareButtons();
5703 prepareAxes(POSITION);
5704 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005705 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005706
arthurhungdcef2dc2020-08-11 14:47:50 +08005707 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005708
5709 NotifyKeyArgs args;
5710
5711 // Press virtual key.
5712 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5713 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5714 processDown(mapper, x, y);
5715 processSync(mapper);
5716
5717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5718 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5719 ASSERT_EQ(DEVICE_ID, args.deviceId);
5720 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5721 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5722 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5723 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5724 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5725 ASSERT_EQ(KEY_HOME, args.scanCode);
5726 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5727 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5728
5729 // Release virtual key.
5730 processUp(mapper);
5731 processSync(mapper);
5732
5733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5734 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5735 ASSERT_EQ(DEVICE_ID, args.deviceId);
5736 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5737 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5738 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5739 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5740 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5741 ASSERT_EQ(KEY_HOME, args.scanCode);
5742 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5743 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5744
5745 // Should not have sent any motions.
5746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5747}
5748
5749TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005750 addConfigurationProperty("touch.deviceType", "touchScreen");
5751 prepareDisplay(DISPLAY_ORIENTATION_0);
5752 prepareButtons();
5753 prepareAxes(POSITION);
5754 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005755 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005756
arthurhungdcef2dc2020-08-11 14:47:50 +08005757 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005758
5759 NotifyKeyArgs keyArgs;
5760
5761 // Press virtual key.
5762 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5763 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5764 processDown(mapper, x, y);
5765 processSync(mapper);
5766
5767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5768 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5769 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5770 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5771 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5772 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5773 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5774 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5775 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5776 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5777 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5778
5779 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5780 // into the display area.
5781 y -= 100;
5782 processMove(mapper, x, y);
5783 processSync(mapper);
5784
5785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5786 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5787 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5788 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5789 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5790 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5791 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5792 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5793 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5794 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5795 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5796 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5797
5798 NotifyMotionArgs motionArgs;
5799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5800 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5801 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5802 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5803 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5804 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5805 ASSERT_EQ(0, motionArgs.flags);
5806 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5807 ASSERT_EQ(0, motionArgs.buttonState);
5808 ASSERT_EQ(0, motionArgs.edgeFlags);
5809 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5810 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5811 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5812 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5813 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5814 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5815 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5816 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5817
5818 // Keep moving out of bounds. Should generate a pointer move.
5819 y -= 50;
5820 processMove(mapper, x, y);
5821 processSync(mapper);
5822
5823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5824 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5825 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5826 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5827 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5828 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5829 ASSERT_EQ(0, motionArgs.flags);
5830 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5831 ASSERT_EQ(0, motionArgs.buttonState);
5832 ASSERT_EQ(0, motionArgs.edgeFlags);
5833 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5834 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5835 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5836 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5837 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5838 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5839 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5840 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5841
5842 // Release out of bounds. Should generate a pointer up.
5843 processUp(mapper);
5844 processSync(mapper);
5845
5846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5847 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5848 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5849 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5850 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5851 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5852 ASSERT_EQ(0, motionArgs.flags);
5853 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5854 ASSERT_EQ(0, motionArgs.buttonState);
5855 ASSERT_EQ(0, motionArgs.edgeFlags);
5856 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5857 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5858 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5859 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5860 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5861 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5862 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5863 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5864
5865 // Should not have sent any more keys or motions.
5866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5868}
5869
5870TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005871 addConfigurationProperty("touch.deviceType", "touchScreen");
5872 prepareDisplay(DISPLAY_ORIENTATION_0);
5873 prepareButtons();
5874 prepareAxes(POSITION);
5875 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005876 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005877
arthurhungdcef2dc2020-08-11 14:47:50 +08005878 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005879
5880 NotifyMotionArgs motionArgs;
5881
5882 // Initially go down out of bounds.
5883 int32_t x = -10;
5884 int32_t y = -10;
5885 processDown(mapper, x, y);
5886 processSync(mapper);
5887
5888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5889
5890 // Move into the display area. Should generate a pointer down.
5891 x = 50;
5892 y = 75;
5893 processMove(mapper, x, y);
5894 processSync(mapper);
5895
5896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5897 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5898 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5899 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5900 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5901 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5902 ASSERT_EQ(0, motionArgs.flags);
5903 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5904 ASSERT_EQ(0, motionArgs.buttonState);
5905 ASSERT_EQ(0, motionArgs.edgeFlags);
5906 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5907 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5908 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5909 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5910 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5911 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5912 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5913 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5914
5915 // Release. Should generate a pointer up.
5916 processUp(mapper);
5917 processSync(mapper);
5918
5919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5920 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5921 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5922 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5923 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5924 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5925 ASSERT_EQ(0, motionArgs.flags);
5926 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5927 ASSERT_EQ(0, motionArgs.buttonState);
5928 ASSERT_EQ(0, motionArgs.edgeFlags);
5929 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5930 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5931 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5933 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5934 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5935 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5936 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5937
5938 // Should not have sent any more keys or motions.
5939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5941}
5942
Santos Cordonfa5cf462017-04-05 10:37:00 -07005943TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005944 addConfigurationProperty("touch.deviceType", "touchScreen");
5945 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5946
5947 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5948 prepareButtons();
5949 prepareAxes(POSITION);
5950 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005951 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005952
arthurhungdcef2dc2020-08-11 14:47:50 +08005953 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005954
5955 NotifyMotionArgs motionArgs;
5956
5957 // Down.
5958 int32_t x = 100;
5959 int32_t y = 125;
5960 processDown(mapper, x, y);
5961 processSync(mapper);
5962
5963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5964 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5965 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5966 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5967 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5968 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5969 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5970 ASSERT_EQ(0, motionArgs.flags);
5971 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5972 ASSERT_EQ(0, motionArgs.buttonState);
5973 ASSERT_EQ(0, motionArgs.edgeFlags);
5974 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5975 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5976 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5977 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5978 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5979 1, 0, 0, 0, 0, 0, 0, 0));
5980 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5981 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5982 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5983
5984 // Move.
5985 x += 50;
5986 y += 75;
5987 processMove(mapper, x, y);
5988 processSync(mapper);
5989
5990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5991 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5992 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5993 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5994 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5995 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5996 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5997 ASSERT_EQ(0, motionArgs.flags);
5998 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5999 ASSERT_EQ(0, motionArgs.buttonState);
6000 ASSERT_EQ(0, motionArgs.edgeFlags);
6001 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6002 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6003 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6004 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6005 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6006 1, 0, 0, 0, 0, 0, 0, 0));
6007 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6008 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6009 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6010
6011 // Up.
6012 processUp(mapper);
6013 processSync(mapper);
6014
6015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6016 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6017 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6018 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6019 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6020 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6021 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6022 ASSERT_EQ(0, motionArgs.flags);
6023 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6024 ASSERT_EQ(0, motionArgs.buttonState);
6025 ASSERT_EQ(0, motionArgs.edgeFlags);
6026 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6027 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6028 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6029 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6030 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6031 1, 0, 0, 0, 0, 0, 0, 0));
6032 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6033 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6034 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6035
6036 // Should not have sent any more keys or motions.
6037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6039}
6040
Michael Wrightd02c5b62014-02-10 15:10:22 -08006041TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006042 addConfigurationProperty("touch.deviceType", "touchScreen");
6043 prepareDisplay(DISPLAY_ORIENTATION_0);
6044 prepareButtons();
6045 prepareAxes(POSITION);
6046 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006047 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006048
arthurhungdcef2dc2020-08-11 14:47:50 +08006049 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006050
6051 NotifyMotionArgs motionArgs;
6052
6053 // Down.
6054 int32_t x = 100;
6055 int32_t y = 125;
6056 processDown(mapper, x, y);
6057 processSync(mapper);
6058
6059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6060 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6061 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6062 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6063 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6064 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6065 ASSERT_EQ(0, motionArgs.flags);
6066 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6067 ASSERT_EQ(0, motionArgs.buttonState);
6068 ASSERT_EQ(0, motionArgs.edgeFlags);
6069 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6070 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6071 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6072 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6073 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6074 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6075 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6076 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6077
6078 // Move.
6079 x += 50;
6080 y += 75;
6081 processMove(mapper, x, y);
6082 processSync(mapper);
6083
6084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6085 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6086 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6087 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6088 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6089 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6090 ASSERT_EQ(0, motionArgs.flags);
6091 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6092 ASSERT_EQ(0, motionArgs.buttonState);
6093 ASSERT_EQ(0, motionArgs.edgeFlags);
6094 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6095 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6096 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6097 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6098 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6099 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6100 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6101 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6102
6103 // Up.
6104 processUp(mapper);
6105 processSync(mapper);
6106
6107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6108 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6109 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6110 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6111 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6112 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6113 ASSERT_EQ(0, motionArgs.flags);
6114 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6115 ASSERT_EQ(0, motionArgs.buttonState);
6116 ASSERT_EQ(0, motionArgs.edgeFlags);
6117 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6118 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6119 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6120 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6121 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6122 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6123 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6124 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6125
6126 // Should not have sent any more keys or motions.
6127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6129}
6130
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006131TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006132 addConfigurationProperty("touch.deviceType", "touchScreen");
6133 prepareButtons();
6134 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006135 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
6136 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006137 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006138
6139 NotifyMotionArgs args;
6140
6141 // Rotation 90.
6142 prepareDisplay(DISPLAY_ORIENTATION_90);
6143 processDown(mapper, toRawX(50), toRawY(75));
6144 processSync(mapper);
6145
6146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6147 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6148 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6149
6150 processUp(mapper);
6151 processSync(mapper);
6152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6153}
6154
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006155TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006156 addConfigurationProperty("touch.deviceType", "touchScreen");
6157 prepareButtons();
6158 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006159 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6160 // orientation-aware are affected by display rotation.
6161 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006162 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006163
6164 NotifyMotionArgs args;
6165
6166 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006167 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006168 prepareDisplay(DISPLAY_ORIENTATION_0);
6169 processDown(mapper, toRawX(50), toRawY(75));
6170 processSync(mapper);
6171
6172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6173 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6174 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6175
6176 processUp(mapper);
6177 processSync(mapper);
6178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6179
6180 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006181 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006182 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006183 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006184 processSync(mapper);
6185
6186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6187 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6188 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6189
6190 processUp(mapper);
6191 processSync(mapper);
6192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6193
6194 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006195 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006196 prepareDisplay(DISPLAY_ORIENTATION_180);
6197 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6198 processSync(mapper);
6199
6200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6201 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6202 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6203
6204 processUp(mapper);
6205 processSync(mapper);
6206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6207
6208 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006209 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006210 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006211 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006212 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
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006223TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6224 addConfigurationProperty("touch.deviceType", "touchScreen");
6225 prepareButtons();
6226 prepareAxes(POSITION);
6227 addConfigurationProperty("touch.orientationAware", "1");
6228 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6229 clearViewports();
6230 prepareDisplay(DISPLAY_ORIENTATION_0);
6231 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6232 NotifyMotionArgs args;
6233
6234 // Orientation 0.
6235 processDown(mapper, toRawX(50), toRawY(75));
6236 processSync(mapper);
6237
6238 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6239 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6240 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6241
6242 processUp(mapper);
6243 processSync(mapper);
6244 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6245}
6246
6247TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6248 addConfigurationProperty("touch.deviceType", "touchScreen");
6249 prepareButtons();
6250 prepareAxes(POSITION);
6251 addConfigurationProperty("touch.orientationAware", "1");
6252 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6253 clearViewports();
6254 prepareDisplay(DISPLAY_ORIENTATION_0);
6255 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6256 NotifyMotionArgs args;
6257
6258 // Orientation 90.
6259 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6260 processSync(mapper);
6261
6262 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6263 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6264 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6265
6266 processUp(mapper);
6267 processSync(mapper);
6268 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6269}
6270
6271TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6272 addConfigurationProperty("touch.deviceType", "touchScreen");
6273 prepareButtons();
6274 prepareAxes(POSITION);
6275 addConfigurationProperty("touch.orientationAware", "1");
6276 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6277 clearViewports();
6278 prepareDisplay(DISPLAY_ORIENTATION_0);
6279 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6280 NotifyMotionArgs args;
6281
6282 // Orientation 180.
6283 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6284 processSync(mapper);
6285
6286 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6287 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6288 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6289
6290 processUp(mapper);
6291 processSync(mapper);
6292 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6293}
6294
6295TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6296 addConfigurationProperty("touch.deviceType", "touchScreen");
6297 prepareButtons();
6298 prepareAxes(POSITION);
6299 addConfigurationProperty("touch.orientationAware", "1");
6300 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6301 clearViewports();
6302 prepareDisplay(DISPLAY_ORIENTATION_0);
6303 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6304 NotifyMotionArgs args;
6305
6306 // Orientation 270.
6307 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6308 processSync(mapper);
6309
6310 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6311 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6312 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6313
6314 processUp(mapper);
6315 processSync(mapper);
6316 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6317}
6318
6319TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6320 addConfigurationProperty("touch.deviceType", "touchScreen");
6321 prepareButtons();
6322 prepareAxes(POSITION);
6323 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6324 // orientation-aware are affected by display rotation.
6325 addConfigurationProperty("touch.orientationAware", "0");
6326 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6327 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6328
6329 NotifyMotionArgs args;
6330
6331 // Orientation 90, Rotation 0.
6332 clearViewports();
6333 prepareDisplay(DISPLAY_ORIENTATION_0);
6334 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6335 processSync(mapper);
6336
6337 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6338 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6339 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6340
6341 processUp(mapper);
6342 processSync(mapper);
6343 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6344
6345 // Orientation 90, Rotation 90.
6346 clearViewports();
6347 prepareDisplay(DISPLAY_ORIENTATION_90);
6348 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6349 processSync(mapper);
6350
6351 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6352 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6353 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6354
6355 processUp(mapper);
6356 processSync(mapper);
6357 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6358
6359 // Orientation 90, Rotation 180.
6360 clearViewports();
6361 prepareDisplay(DISPLAY_ORIENTATION_180);
6362 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6363 processSync(mapper);
6364
6365 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6366 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6367 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6368
6369 processUp(mapper);
6370 processSync(mapper);
6371 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6372
6373 // Orientation 90, Rotation 270.
6374 clearViewports();
6375 prepareDisplay(DISPLAY_ORIENTATION_270);
6376 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6377 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6378 processSync(mapper);
6379
6380 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6381 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6382 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6383
6384 processUp(mapper);
6385 processSync(mapper);
6386 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6387}
6388
Michael Wrightd02c5b62014-02-10 15:10:22 -08006389TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006390 addConfigurationProperty("touch.deviceType", "touchScreen");
6391 prepareDisplay(DISPLAY_ORIENTATION_0);
6392 prepareButtons();
6393 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006394 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006395
6396 // These calculations are based on the input device calibration documentation.
6397 int32_t rawX = 100;
6398 int32_t rawY = 200;
6399 int32_t rawPressure = 10;
6400 int32_t rawToolMajor = 12;
6401 int32_t rawDistance = 2;
6402 int32_t rawTiltX = 30;
6403 int32_t rawTiltY = 110;
6404
6405 float x = toDisplayX(rawX);
6406 float y = toDisplayY(rawY);
6407 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6408 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6409 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6410 float distance = float(rawDistance);
6411
6412 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6413 float tiltScale = M_PI / 180;
6414 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6415 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6416 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6417 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6418
6419 processDown(mapper, rawX, rawY);
6420 processPressure(mapper, rawPressure);
6421 processToolMajor(mapper, rawToolMajor);
6422 processDistance(mapper, rawDistance);
6423 processTilt(mapper, rawTiltX, rawTiltY);
6424 processSync(mapper);
6425
6426 NotifyMotionArgs args;
6427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6428 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6429 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6430 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6431}
6432
Jason Gerecke489fda82012-09-07 17:19:40 -07006433TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006434 addConfigurationProperty("touch.deviceType", "touchScreen");
6435 prepareDisplay(DISPLAY_ORIENTATION_0);
6436 prepareLocationCalibration();
6437 prepareButtons();
6438 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006439 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006440
6441 int32_t rawX = 100;
6442 int32_t rawY = 200;
6443
6444 float x = toDisplayX(toCookedX(rawX, rawY));
6445 float y = toDisplayY(toCookedY(rawX, rawY));
6446
6447 processDown(mapper, rawX, rawY);
6448 processSync(mapper);
6449
6450 NotifyMotionArgs args;
6451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6453 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6454}
6455
Michael Wrightd02c5b62014-02-10 15:10:22 -08006456TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006457 addConfigurationProperty("touch.deviceType", "touchScreen");
6458 prepareDisplay(DISPLAY_ORIENTATION_0);
6459 prepareButtons();
6460 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006461 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006462
6463 NotifyMotionArgs motionArgs;
6464 NotifyKeyArgs keyArgs;
6465
6466 processDown(mapper, 100, 200);
6467 processSync(mapper);
6468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6469 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6470 ASSERT_EQ(0, motionArgs.buttonState);
6471
6472 // press BTN_LEFT, release BTN_LEFT
6473 processKey(mapper, BTN_LEFT, 1);
6474 processSync(mapper);
6475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6476 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6477 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6478
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6480 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6481 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6482
Michael Wrightd02c5b62014-02-10 15:10:22 -08006483 processKey(mapper, BTN_LEFT, 0);
6484 processSync(mapper);
6485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006486 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006487 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006488
6489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006490 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006491 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006492
6493 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6494 processKey(mapper, BTN_RIGHT, 1);
6495 processKey(mapper, BTN_MIDDLE, 1);
6496 processSync(mapper);
6497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6498 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6499 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6500 motionArgs.buttonState);
6501
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6503 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6504 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6505
6506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6507 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6508 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6509 motionArgs.buttonState);
6510
Michael Wrightd02c5b62014-02-10 15:10:22 -08006511 processKey(mapper, BTN_RIGHT, 0);
6512 processSync(mapper);
6513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006514 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006515 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006516
6517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006518 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006519 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006520
6521 processKey(mapper, BTN_MIDDLE, 0);
6522 processSync(mapper);
6523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006524 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006525 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006526
6527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006528 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006529 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006530
6531 // press BTN_BACK, release BTN_BACK
6532 processKey(mapper, BTN_BACK, 1);
6533 processSync(mapper);
6534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6535 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6536 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006537
Michael Wrightd02c5b62014-02-10 15:10:22 -08006538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006539 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006540 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6541
6542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6543 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6544 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006545
6546 processKey(mapper, BTN_BACK, 0);
6547 processSync(mapper);
6548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006549 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006550 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006551
6552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006553 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006554 ASSERT_EQ(0, motionArgs.buttonState);
6555
Michael Wrightd02c5b62014-02-10 15:10:22 -08006556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6557 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6558 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6559
6560 // press BTN_SIDE, release BTN_SIDE
6561 processKey(mapper, BTN_SIDE, 1);
6562 processSync(mapper);
6563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6564 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6565 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006566
Michael Wrightd02c5b62014-02-10 15:10:22 -08006567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006568 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006569 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6570
6571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6572 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6573 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006574
6575 processKey(mapper, BTN_SIDE, 0);
6576 processSync(mapper);
6577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006578 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006579 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006580
6581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006582 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006583 ASSERT_EQ(0, motionArgs.buttonState);
6584
Michael Wrightd02c5b62014-02-10 15:10:22 -08006585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6586 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6587 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6588
6589 // press BTN_FORWARD, release BTN_FORWARD
6590 processKey(mapper, BTN_FORWARD, 1);
6591 processSync(mapper);
6592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6593 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6594 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006595
Michael Wrightd02c5b62014-02-10 15:10:22 -08006596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006597 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006598 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6599
6600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6601 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6602 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006603
6604 processKey(mapper, BTN_FORWARD, 0);
6605 processSync(mapper);
6606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006607 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006608 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006609
6610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006611 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006612 ASSERT_EQ(0, motionArgs.buttonState);
6613
Michael Wrightd02c5b62014-02-10 15:10:22 -08006614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6615 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6616 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6617
6618 // press BTN_EXTRA, release BTN_EXTRA
6619 processKey(mapper, BTN_EXTRA, 1);
6620 processSync(mapper);
6621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6622 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6623 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006624
Michael Wrightd02c5b62014-02-10 15:10:22 -08006625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006626 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006627 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6628
6629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6630 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6631 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006632
6633 processKey(mapper, BTN_EXTRA, 0);
6634 processSync(mapper);
6635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006636 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006637 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006638
6639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006640 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006641 ASSERT_EQ(0, motionArgs.buttonState);
6642
Michael Wrightd02c5b62014-02-10 15:10:22 -08006643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6644 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6645 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6646
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6648
Michael Wrightd02c5b62014-02-10 15:10:22 -08006649 // press BTN_STYLUS, release BTN_STYLUS
6650 processKey(mapper, BTN_STYLUS, 1);
6651 processSync(mapper);
6652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6653 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006654 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6655
6656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6657 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6658 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006659
6660 processKey(mapper, BTN_STYLUS, 0);
6661 processSync(mapper);
6662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006663 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006664 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006665
6666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006667 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006668 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006669
6670 // press BTN_STYLUS2, release BTN_STYLUS2
6671 processKey(mapper, BTN_STYLUS2, 1);
6672 processSync(mapper);
6673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6674 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006675 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6676
6677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6678 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6679 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006680
6681 processKey(mapper, BTN_STYLUS2, 0);
6682 processSync(mapper);
6683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006684 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006685 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006686
6687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006688 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006689 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006690
6691 // release touch
6692 processUp(mapper);
6693 processSync(mapper);
6694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6695 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6696 ASSERT_EQ(0, motionArgs.buttonState);
6697}
6698
6699TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006700 addConfigurationProperty("touch.deviceType", "touchScreen");
6701 prepareDisplay(DISPLAY_ORIENTATION_0);
6702 prepareButtons();
6703 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006704 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006705
6706 NotifyMotionArgs motionArgs;
6707
6708 // default tool type is finger
6709 processDown(mapper, 100, 200);
6710 processSync(mapper);
6711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6712 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6713 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6714
6715 // eraser
6716 processKey(mapper, BTN_TOOL_RUBBER, 1);
6717 processSync(mapper);
6718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6719 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6720 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6721
6722 // stylus
6723 processKey(mapper, BTN_TOOL_RUBBER, 0);
6724 processKey(mapper, BTN_TOOL_PEN, 1);
6725 processSync(mapper);
6726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6727 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6728 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6729
6730 // brush
6731 processKey(mapper, BTN_TOOL_PEN, 0);
6732 processKey(mapper, BTN_TOOL_BRUSH, 1);
6733 processSync(mapper);
6734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6735 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6736 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6737
6738 // pencil
6739 processKey(mapper, BTN_TOOL_BRUSH, 0);
6740 processKey(mapper, BTN_TOOL_PENCIL, 1);
6741 processSync(mapper);
6742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6743 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6744 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6745
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006746 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006747 processKey(mapper, BTN_TOOL_PENCIL, 0);
6748 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6749 processSync(mapper);
6750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6751 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6752 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6753
6754 // mouse
6755 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6756 processKey(mapper, BTN_TOOL_MOUSE, 1);
6757 processSync(mapper);
6758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6759 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6760 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6761
6762 // lens
6763 processKey(mapper, BTN_TOOL_MOUSE, 0);
6764 processKey(mapper, BTN_TOOL_LENS, 1);
6765 processSync(mapper);
6766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6767 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6768 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6769
6770 // double-tap
6771 processKey(mapper, BTN_TOOL_LENS, 0);
6772 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6773 processSync(mapper);
6774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6775 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6776 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6777
6778 // triple-tap
6779 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6780 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6781 processSync(mapper);
6782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6783 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6784 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6785
6786 // quad-tap
6787 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6788 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6789 processSync(mapper);
6790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6791 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6792 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6793
6794 // finger
6795 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6796 processKey(mapper, BTN_TOOL_FINGER, 1);
6797 processSync(mapper);
6798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6799 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6800 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6801
6802 // stylus trumps finger
6803 processKey(mapper, BTN_TOOL_PEN, 1);
6804 processSync(mapper);
6805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6806 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6807 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6808
6809 // eraser trumps stylus
6810 processKey(mapper, BTN_TOOL_RUBBER, 1);
6811 processSync(mapper);
6812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6813 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6814 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6815
6816 // mouse trumps eraser
6817 processKey(mapper, BTN_TOOL_MOUSE, 1);
6818 processSync(mapper);
6819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6820 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6821 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6822
6823 // back to default tool type
6824 processKey(mapper, BTN_TOOL_MOUSE, 0);
6825 processKey(mapper, BTN_TOOL_RUBBER, 0);
6826 processKey(mapper, BTN_TOOL_PEN, 0);
6827 processKey(mapper, BTN_TOOL_FINGER, 0);
6828 processSync(mapper);
6829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6830 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6831 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6832}
6833
6834TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006835 addConfigurationProperty("touch.deviceType", "touchScreen");
6836 prepareDisplay(DISPLAY_ORIENTATION_0);
6837 prepareButtons();
6838 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006839 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006840 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006841
6842 NotifyMotionArgs motionArgs;
6843
6844 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6845 processKey(mapper, BTN_TOOL_FINGER, 1);
6846 processMove(mapper, 100, 200);
6847 processSync(mapper);
6848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6849 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6851 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6852
6853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6854 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6855 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6856 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6857
6858 // move a little
6859 processMove(mapper, 150, 250);
6860 processSync(mapper);
6861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6862 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6863 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6864 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6865
6866 // down when BTN_TOUCH is pressed, pressure defaults to 1
6867 processKey(mapper, BTN_TOUCH, 1);
6868 processSync(mapper);
6869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6870 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6871 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6872 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6873
6874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6875 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6876 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6877 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6878
6879 // up when BTN_TOUCH is released, hover restored
6880 processKey(mapper, BTN_TOUCH, 0);
6881 processSync(mapper);
6882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6883 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6884 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6885 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6886
6887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6888 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6889 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6890 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6891
6892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6893 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6894 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6895 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6896
6897 // exit hover when pointer goes away
6898 processKey(mapper, BTN_TOOL_FINGER, 0);
6899 processSync(mapper);
6900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6901 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6902 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6903 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6904}
6905
6906TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006907 addConfigurationProperty("touch.deviceType", "touchScreen");
6908 prepareDisplay(DISPLAY_ORIENTATION_0);
6909 prepareButtons();
6910 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006911 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006912
6913 NotifyMotionArgs motionArgs;
6914
6915 // initially hovering because pressure is 0
6916 processDown(mapper, 100, 200);
6917 processPressure(mapper, 0);
6918 processSync(mapper);
6919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6920 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6921 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6922 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6923
6924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6925 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6926 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6927 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6928
6929 // move a little
6930 processMove(mapper, 150, 250);
6931 processSync(mapper);
6932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6933 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6934 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6935 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6936
6937 // down when pressure is non-zero
6938 processPressure(mapper, RAW_PRESSURE_MAX);
6939 processSync(mapper);
6940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6941 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6942 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6943 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6944
6945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6946 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6947 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6948 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6949
6950 // up when pressure becomes 0, hover restored
6951 processPressure(mapper, 0);
6952 processSync(mapper);
6953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6954 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6955 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6956 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6957
6958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6959 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6960 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6961 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6962
6963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6964 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6965 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6966 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6967
6968 // exit hover when pointer goes away
6969 processUp(mapper);
6970 processSync(mapper);
6971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6972 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6973 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6974 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6975}
6976
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006977TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6978 addConfigurationProperty("touch.deviceType", "touchScreen");
6979 prepareDisplay(DISPLAY_ORIENTATION_0);
6980 prepareButtons();
6981 prepareAxes(POSITION | PRESSURE);
6982 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6983
6984 // Touch down.
6985 processDown(mapper, 100, 200);
6986 processPressure(mapper, 1);
6987 processSync(mapper);
6988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6989 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6990
6991 // Reset the mapper. This should cancel the ongoing gesture.
6992 resetMapper(mapper, ARBITRARY_TIME);
6993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6994 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6995
6996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6997}
6998
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006999TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
7000 addConfigurationProperty("touch.deviceType", "touchScreen");
7001 prepareDisplay(DISPLAY_ORIENTATION_0);
7002 prepareButtons();
7003 prepareAxes(POSITION | PRESSURE);
7004 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7005
7006 // Set the initial state for the touch pointer.
7007 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
7008 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
7009 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
7010 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7011
7012 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007013 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
7014 // does not generate any events.
7015 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007016
7017 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
7018 // the recreated touch state to generate a down event.
7019 processSync(mapper);
7020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7021 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
7022
7023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7024}
7025
lilinnan687e58f2022-07-19 16:00:50 +08007026TEST_F(SingleTouchInputMapperTest,
7027 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
7028 addConfigurationProperty("touch.deviceType", "touchScreen");
7029 prepareDisplay(DISPLAY_ORIENTATION_0);
7030 prepareButtons();
7031 prepareAxes(POSITION);
7032 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7033 NotifyMotionArgs motionArgs;
7034
7035 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00007036 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08007037 processSync(mapper);
7038
7039 // We should receive a down event
7040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7041 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7042
7043 // Change display id
7044 clearViewports();
7045 prepareSecondaryDisplay(ViewportType::INTERNAL);
7046
7047 // We should receive a cancel event
7048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7049 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7050 // Then receive reset called
7051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7052}
7053
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007054TEST_F(SingleTouchInputMapperTest,
7055 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
7056 addConfigurationProperty("touch.deviceType", "touchScreen");
7057 prepareDisplay(DISPLAY_ORIENTATION_0);
7058 prepareButtons();
7059 prepareAxes(POSITION);
7060 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7062 NotifyMotionArgs motionArgs;
7063
7064 // Start a new gesture.
7065 processDown(mapper, 100, 200);
7066 processSync(mapper);
7067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7068 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7069
7070 // Make the viewport inactive. This will put the device in disabled mode.
7071 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7072 viewport->isActive = false;
7073 mFakePolicy->updateViewport(*viewport);
7074 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7075
7076 // We should receive a cancel event for the ongoing gesture.
7077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7078 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7079 // Then we should be notified that the device was reset.
7080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7081
7082 // No events are generated while the viewport is inactive.
7083 processMove(mapper, 101, 201);
7084 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007085 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007086 processSync(mapper);
7087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7088
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007089 // Start a new gesture while the viewport is still inactive.
7090 processDown(mapper, 300, 400);
7091 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
7092 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
7093 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7094 processSync(mapper);
7095
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007096 // Make the viewport active again. The device should resume processing events.
7097 viewport->isActive = true;
7098 mFakePolicy->updateViewport(*viewport);
7099 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7100
7101 // The device is reset because it changes back to direct mode, without generating any events.
7102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7104
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007105 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007106 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7108 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007109
7110 // No more events.
7111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
7113}
7114
Prabir Pradhan5632d622021-09-06 07:57:20 -07007115// --- TouchDisplayProjectionTest ---
7116
7117class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
7118public:
7119 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
7120 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
7121 // rotated equivalent of the given un-rotated physical display bounds.
7122 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
7123 uint32_t inverseRotationFlags;
7124 auto width = DISPLAY_WIDTH;
7125 auto height = DISPLAY_HEIGHT;
7126 switch (orientation) {
7127 case DISPLAY_ORIENTATION_90:
7128 inverseRotationFlags = ui::Transform::ROT_270;
7129 std::swap(width, height);
7130 break;
7131 case DISPLAY_ORIENTATION_180:
7132 inverseRotationFlags = ui::Transform::ROT_180;
7133 break;
7134 case DISPLAY_ORIENTATION_270:
7135 inverseRotationFlags = ui::Transform::ROT_90;
7136 std::swap(width, height);
7137 break;
7138 case DISPLAY_ORIENTATION_0:
7139 inverseRotationFlags = ui::Transform::ROT_0;
7140 break;
7141 default:
7142 FAIL() << "Invalid orientation: " << orientation;
7143 }
7144
7145 const ui::Transform rotation(inverseRotationFlags, width, height);
7146 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
7147
7148 std::optional<DisplayViewport> internalViewport =
7149 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7150 DisplayViewport& v = *internalViewport;
7151 v.displayId = DISPLAY_ID;
7152 v.orientation = orientation;
7153
7154 v.logicalLeft = 0;
7155 v.logicalTop = 0;
7156 v.logicalRight = 100;
7157 v.logicalBottom = 100;
7158
7159 v.physicalLeft = rotatedPhysicalDisplay.left;
7160 v.physicalTop = rotatedPhysicalDisplay.top;
7161 v.physicalRight = rotatedPhysicalDisplay.right;
7162 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7163
7164 v.deviceWidth = width;
7165 v.deviceHeight = height;
7166
7167 v.isActive = true;
7168 v.uniqueId = UNIQUE_ID;
7169 v.type = ViewportType::INTERNAL;
7170 mFakePolicy->updateViewport(v);
7171 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7172 }
7173
7174 void assertReceivedMove(const Point& point) {
7175 NotifyMotionArgs motionArgs;
7176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7177 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7178 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7180 1, 0, 0, 0, 0, 0, 0, 0));
7181 }
7182};
7183
7184TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7185 addConfigurationProperty("touch.deviceType", "touchScreen");
7186 prepareDisplay(DISPLAY_ORIENTATION_0);
7187
7188 prepareButtons();
7189 prepareAxes(POSITION);
7190 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7191
7192 NotifyMotionArgs motionArgs;
7193
7194 // Configure the DisplayViewport such that the logical display maps to a subsection of
7195 // the display panel called the physical display. Here, the physical display is bounded by the
7196 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7197 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7198 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7199 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7200
7201 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7202 DISPLAY_ORIENTATION_270}) {
7203 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7204
7205 // Touches outside the physical display should be ignored, and should not generate any
7206 // events. Ensure touches at the following points that lie outside of the physical display
7207 // area do not generate any events.
7208 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7209 processDown(mapper, toRawX(point.x), toRawY(point.y));
7210 processSync(mapper);
7211 processUp(mapper);
7212 processSync(mapper);
7213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7214 << "Unexpected event generated for touch outside physical display at point: "
7215 << point.x << ", " << point.y;
7216 }
7217 }
7218}
7219
7220TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7221 addConfigurationProperty("touch.deviceType", "touchScreen");
7222 prepareDisplay(DISPLAY_ORIENTATION_0);
7223
7224 prepareButtons();
7225 prepareAxes(POSITION);
7226 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7227
7228 NotifyMotionArgs motionArgs;
7229
7230 // Configure the DisplayViewport such that the logical display maps to a subsection of
7231 // the display panel called the physical display. Here, the physical display is bounded by the
7232 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7233 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7234
7235 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7236 DISPLAY_ORIENTATION_270}) {
7237 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7238
7239 // Touches that start outside the physical display should be ignored until it enters the
7240 // physical display bounds, at which point it should generate a down event. Start a touch at
7241 // the point (5, 100), which is outside the physical display bounds.
7242 static const Point kOutsidePoint{5, 100};
7243 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7244 processSync(mapper);
7245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7246
7247 // Move the touch into the physical display area. This should generate a pointer down.
7248 processMove(mapper, toRawX(11), toRawY(21));
7249 processSync(mapper);
7250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7251 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7252 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7253 ASSERT_NO_FATAL_FAILURE(
7254 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7255
7256 // Move the touch inside the physical display area. This should generate a pointer move.
7257 processMove(mapper, toRawX(69), toRawY(159));
7258 processSync(mapper);
7259 assertReceivedMove({69, 159});
7260
7261 // Move outside the physical display area. Since the pointer is already down, this should
7262 // now continue generating events.
7263 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7264 processSync(mapper);
7265 assertReceivedMove(kOutsidePoint);
7266
7267 // Release. This should generate a pointer up.
7268 processUp(mapper);
7269 processSync(mapper);
7270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7271 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7272 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7273 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7274
7275 // Ensure no more events were generated.
7276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7278 }
7279}
7280
Michael Wrightd02c5b62014-02-10 15:10:22 -08007281// --- MultiTouchInputMapperTest ---
7282
7283class MultiTouchInputMapperTest : public TouchInputMapperTest {
7284protected:
7285 void prepareAxes(int axes);
7286
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007287 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7288 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7289 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7290 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7291 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7292 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7293 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7294 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7295 void processId(MultiTouchInputMapper& mapper, int32_t id);
7296 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7297 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7298 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
7299 void processMTSync(MultiTouchInputMapper& mapper);
7300 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007301};
7302
7303void MultiTouchInputMapperTest::prepareAxes(int axes) {
7304 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007305 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7306 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007307 }
7308 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007309 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7310 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007311 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007312 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7313 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007314 }
7315 }
7316 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007317 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7318 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007319 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007320 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007321 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007322 }
7323 }
7324 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007325 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7326 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007327 }
7328 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007329 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7330 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007331 }
7332 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007333 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7334 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007335 }
7336 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007337 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7338 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007339 }
7340 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007341 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7342 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007343 }
7344 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007345 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007346 }
7347}
7348
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007349void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7350 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007351 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7352 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007353}
7354
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007355void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7356 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007357 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007358}
7359
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007360void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7361 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007362 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007363}
7364
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007365void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007366 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007367}
7368
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007369void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007370 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007371}
7372
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007373void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7374 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007375 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007376}
7377
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007378void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007379 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007380}
7381
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007382void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007383 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007384}
7385
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007386void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007387 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007388}
7389
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007390void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007391 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007392}
7393
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007394void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007395 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007396}
7397
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007398void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7399 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007400 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007401}
7402
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007403void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007404 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007405}
7406
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007407void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007409}
7410
Michael Wrightd02c5b62014-02-10 15:10:22 -08007411TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007412 addConfigurationProperty("touch.deviceType", "touchScreen");
7413 prepareDisplay(DISPLAY_ORIENTATION_0);
7414 prepareAxes(POSITION);
7415 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007416 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007417
arthurhungdcef2dc2020-08-11 14:47:50 +08007418 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007419
7420 NotifyMotionArgs motionArgs;
7421
7422 // Two fingers down at once.
7423 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7424 processPosition(mapper, x1, y1);
7425 processMTSync(mapper);
7426 processPosition(mapper, x2, y2);
7427 processMTSync(mapper);
7428 processSync(mapper);
7429
7430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7431 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7432 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7433 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7434 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7435 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7436 ASSERT_EQ(0, motionArgs.flags);
7437 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7438 ASSERT_EQ(0, motionArgs.buttonState);
7439 ASSERT_EQ(0, motionArgs.edgeFlags);
7440 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7441 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7442 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7444 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7445 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7446 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7447 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7448
7449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7450 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7451 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7452 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7453 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007454 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007455 ASSERT_EQ(0, motionArgs.flags);
7456 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7457 ASSERT_EQ(0, motionArgs.buttonState);
7458 ASSERT_EQ(0, motionArgs.edgeFlags);
7459 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7460 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7461 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7462 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7463 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7464 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7465 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7466 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7467 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7468 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7469 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7470 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7471
7472 // Move.
7473 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7474 processPosition(mapper, x1, y1);
7475 processMTSync(mapper);
7476 processPosition(mapper, x2, y2);
7477 processMTSync(mapper);
7478 processSync(mapper);
7479
7480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7481 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7482 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7483 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7484 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7485 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7486 ASSERT_EQ(0, motionArgs.flags);
7487 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7488 ASSERT_EQ(0, motionArgs.buttonState);
7489 ASSERT_EQ(0, motionArgs.edgeFlags);
7490 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7491 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7492 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7493 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7494 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7496 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7497 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7498 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7499 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7500 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7501 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7502
7503 // First finger up.
7504 x2 += 15; y2 -= 20;
7505 processPosition(mapper, x2, y2);
7506 processMTSync(mapper);
7507 processSync(mapper);
7508
7509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7510 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7511 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7512 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7513 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007514 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007515 ASSERT_EQ(0, motionArgs.flags);
7516 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7517 ASSERT_EQ(0, motionArgs.buttonState);
7518 ASSERT_EQ(0, motionArgs.edgeFlags);
7519 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7520 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7521 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7522 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7523 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7525 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7526 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7527 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7528 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7529 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7530 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7531
7532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7533 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7534 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7535 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7536 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7537 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7538 ASSERT_EQ(0, motionArgs.flags);
7539 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7540 ASSERT_EQ(0, motionArgs.buttonState);
7541 ASSERT_EQ(0, motionArgs.edgeFlags);
7542 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7543 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7544 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7546 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7547 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7548 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7549 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7550
7551 // Move.
7552 x2 += 20; y2 -= 25;
7553 processPosition(mapper, x2, y2);
7554 processMTSync(mapper);
7555 processSync(mapper);
7556
7557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7558 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7559 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7560 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7561 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7562 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7563 ASSERT_EQ(0, motionArgs.flags);
7564 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7565 ASSERT_EQ(0, motionArgs.buttonState);
7566 ASSERT_EQ(0, motionArgs.edgeFlags);
7567 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7568 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7569 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7571 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7572 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7573 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7574 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7575
7576 // New finger down.
7577 int32_t x3 = 700, y3 = 300;
7578 processPosition(mapper, x2, y2);
7579 processMTSync(mapper);
7580 processPosition(mapper, x3, y3);
7581 processMTSync(mapper);
7582 processSync(mapper);
7583
7584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7585 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7586 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7587 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7588 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007589 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007590 ASSERT_EQ(0, motionArgs.flags);
7591 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7592 ASSERT_EQ(0, motionArgs.buttonState);
7593 ASSERT_EQ(0, motionArgs.edgeFlags);
7594 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7595 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7596 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7597 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7598 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7600 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7601 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7602 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7603 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7604 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7605 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7606
7607 // Second finger up.
7608 x3 += 30; y3 -= 20;
7609 processPosition(mapper, x3, y3);
7610 processMTSync(mapper);
7611 processSync(mapper);
7612
7613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7614 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7615 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7616 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7617 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007618 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007619 ASSERT_EQ(0, motionArgs.flags);
7620 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7621 ASSERT_EQ(0, motionArgs.buttonState);
7622 ASSERT_EQ(0, motionArgs.edgeFlags);
7623 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7624 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7625 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7626 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7627 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7628 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7629 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7631 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7632 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7633 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7634 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7635
7636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7637 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7638 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7639 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7640 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7641 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7642 ASSERT_EQ(0, motionArgs.flags);
7643 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7644 ASSERT_EQ(0, motionArgs.buttonState);
7645 ASSERT_EQ(0, motionArgs.edgeFlags);
7646 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7647 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7648 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7649 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7650 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7651 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7652 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7653 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7654
7655 // Last finger up.
7656 processMTSync(mapper);
7657 processSync(mapper);
7658
7659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7660 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7661 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7662 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7663 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7664 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7665 ASSERT_EQ(0, motionArgs.flags);
7666 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7667 ASSERT_EQ(0, motionArgs.buttonState);
7668 ASSERT_EQ(0, motionArgs.edgeFlags);
7669 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7670 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7671 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7672 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7673 toDisplayX(x3), toDisplayY(y3), 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 // Should not have sent any more keys or motions.
7679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7681}
7682
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007683TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7684 addConfigurationProperty("touch.deviceType", "touchScreen");
7685 prepareDisplay(DISPLAY_ORIENTATION_0);
7686
7687 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7688 /*fuzz*/ 0, /*resolution*/ 10);
7689 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7690 /*fuzz*/ 0, /*resolution*/ 11);
7691 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7692 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7693 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7694 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7695 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7696 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7697 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7698 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7699
7700 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7701
7702 // X and Y axes
7703 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7704 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7705 // Touch major and minor
7706 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7707 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7708 // Tool major and minor
7709 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7710 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7711}
7712
7713TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7714 addConfigurationProperty("touch.deviceType", "touchScreen");
7715 prepareDisplay(DISPLAY_ORIENTATION_0);
7716
7717 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7718 /*fuzz*/ 0, /*resolution*/ 10);
7719 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7720 /*fuzz*/ 0, /*resolution*/ 11);
7721
7722 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7723
7724 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7725
7726 // Touch major and minor
7727 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7728 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7729 // Tool major and minor
7730 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7731 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7732}
7733
Michael Wrightd02c5b62014-02-10 15:10:22 -08007734TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007735 addConfigurationProperty("touch.deviceType", "touchScreen");
7736 prepareDisplay(DISPLAY_ORIENTATION_0);
7737 prepareAxes(POSITION | ID);
7738 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007739 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007740
arthurhungdcef2dc2020-08-11 14:47:50 +08007741 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007742
7743 NotifyMotionArgs motionArgs;
7744
7745 // Two fingers down at once.
7746 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7747 processPosition(mapper, x1, y1);
7748 processId(mapper, 1);
7749 processMTSync(mapper);
7750 processPosition(mapper, x2, y2);
7751 processId(mapper, 2);
7752 processMTSync(mapper);
7753 processSync(mapper);
7754
7755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7756 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7757 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7758 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7759 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7760 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7761 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7762
7763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007764 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007765 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7766 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7767 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7768 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7769 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7770 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7771 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7772 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7773 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7774
7775 // Move.
7776 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7777 processPosition(mapper, x1, y1);
7778 processId(mapper, 1);
7779 processMTSync(mapper);
7780 processPosition(mapper, x2, y2);
7781 processId(mapper, 2);
7782 processMTSync(mapper);
7783 processSync(mapper);
7784
7785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7786 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7787 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7788 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7789 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7790 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7791 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7792 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7793 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7794 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7795 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7796
7797 // First finger up.
7798 x2 += 15; y2 -= 20;
7799 processPosition(mapper, x2, y2);
7800 processId(mapper, 2);
7801 processMTSync(mapper);
7802 processSync(mapper);
7803
7804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007805 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007806 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7807 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7808 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7809 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7810 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7811 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7812 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7814 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7815
7816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7817 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7818 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7819 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7820 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7821 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7822 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7823
7824 // Move.
7825 x2 += 20; y2 -= 25;
7826 processPosition(mapper, x2, y2);
7827 processId(mapper, 2);
7828 processMTSync(mapper);
7829 processSync(mapper);
7830
7831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7832 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7833 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7834 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7835 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7836 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7837 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7838
7839 // New finger down.
7840 int32_t x3 = 700, y3 = 300;
7841 processPosition(mapper, x2, y2);
7842 processId(mapper, 2);
7843 processMTSync(mapper);
7844 processPosition(mapper, x3, y3);
7845 processId(mapper, 3);
7846 processMTSync(mapper);
7847 processSync(mapper);
7848
7849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007850 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007851 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7852 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7853 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7854 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7855 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7856 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7857 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7858 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7859 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7860
7861 // Second finger up.
7862 x3 += 30; y3 -= 20;
7863 processPosition(mapper, x3, y3);
7864 processId(mapper, 3);
7865 processMTSync(mapper);
7866 processSync(mapper);
7867
7868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007869 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007870 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7871 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7872 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7873 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7874 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7875 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7876 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7877 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7878 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7879
7880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7881 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7882 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7883 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7884 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7886 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7887
7888 // Last finger up.
7889 processMTSync(mapper);
7890 processSync(mapper);
7891
7892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7893 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7894 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7895 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7896 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7897 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7898 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7899
7900 // Should not have sent any more keys or motions.
7901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7903}
7904
7905TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007906 addConfigurationProperty("touch.deviceType", "touchScreen");
7907 prepareDisplay(DISPLAY_ORIENTATION_0);
7908 prepareAxes(POSITION | ID | SLOT);
7909 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007910 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007911
arthurhungdcef2dc2020-08-11 14:47:50 +08007912 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007913
7914 NotifyMotionArgs motionArgs;
7915
7916 // Two fingers down at once.
7917 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7918 processPosition(mapper, x1, y1);
7919 processId(mapper, 1);
7920 processSlot(mapper, 1);
7921 processPosition(mapper, x2, y2);
7922 processId(mapper, 2);
7923 processSync(mapper);
7924
7925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7926 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7927 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7928 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7929 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7930 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7931 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7932
7933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007934 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007935 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7936 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7937 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7938 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7939 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7940 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7941 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7942 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7943 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7944
7945 // Move.
7946 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7947 processSlot(mapper, 0);
7948 processPosition(mapper, x1, y1);
7949 processSlot(mapper, 1);
7950 processPosition(mapper, x2, y2);
7951 processSync(mapper);
7952
7953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7954 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7955 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7956 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7957 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7958 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7959 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7960 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7961 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7962 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7963 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7964
7965 // First finger up.
7966 x2 += 15; y2 -= 20;
7967 processSlot(mapper, 0);
7968 processId(mapper, -1);
7969 processSlot(mapper, 1);
7970 processPosition(mapper, x2, y2);
7971 processSync(mapper);
7972
7973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007974 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007975 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7976 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7977 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7978 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7979 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7980 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7981 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7982 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7983 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7984
7985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7986 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7987 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7988 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7989 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7990 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7991 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7992
7993 // Move.
7994 x2 += 20; y2 -= 25;
7995 processPosition(mapper, x2, y2);
7996 processSync(mapper);
7997
7998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7999 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8000 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8001 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8002 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8003 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8004 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8005
8006 // New finger down.
8007 int32_t x3 = 700, y3 = 300;
8008 processPosition(mapper, x2, y2);
8009 processSlot(mapper, 0);
8010 processId(mapper, 3);
8011 processPosition(mapper, x3, y3);
8012 processSync(mapper);
8013
8014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008015 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008016 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8017 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8018 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8019 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8020 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8021 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8022 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8023 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8024 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8025
8026 // Second finger up.
8027 x3 += 30; y3 -= 20;
8028 processSlot(mapper, 1);
8029 processId(mapper, -1);
8030 processSlot(mapper, 0);
8031 processPosition(mapper, x3, y3);
8032 processSync(mapper);
8033
8034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008035 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008036 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8037 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8038 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8039 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8040 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8041 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8042 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8043 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8044 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8045
8046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8047 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8048 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8049 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8050 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8051 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8052 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8053
8054 // Last finger up.
8055 processId(mapper, -1);
8056 processSync(mapper);
8057
8058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8059 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8060 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8061 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8062 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8063 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8064 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8065
8066 // Should not have sent any more keys or motions.
8067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8069}
8070
8071TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008072 addConfigurationProperty("touch.deviceType", "touchScreen");
8073 prepareDisplay(DISPLAY_ORIENTATION_0);
8074 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008075 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008076
8077 // These calculations are based on the input device calibration documentation.
8078 int32_t rawX = 100;
8079 int32_t rawY = 200;
8080 int32_t rawTouchMajor = 7;
8081 int32_t rawTouchMinor = 6;
8082 int32_t rawToolMajor = 9;
8083 int32_t rawToolMinor = 8;
8084 int32_t rawPressure = 11;
8085 int32_t rawDistance = 0;
8086 int32_t rawOrientation = 3;
8087 int32_t id = 5;
8088
8089 float x = toDisplayX(rawX);
8090 float y = toDisplayY(rawY);
8091 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8092 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8093 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8094 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8095 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8096 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8097 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8098 float distance = float(rawDistance);
8099
8100 processPosition(mapper, rawX, rawY);
8101 processTouchMajor(mapper, rawTouchMajor);
8102 processTouchMinor(mapper, rawTouchMinor);
8103 processToolMajor(mapper, rawToolMajor);
8104 processToolMinor(mapper, rawToolMinor);
8105 processPressure(mapper, rawPressure);
8106 processOrientation(mapper, rawOrientation);
8107 processDistance(mapper, rawDistance);
8108 processId(mapper, id);
8109 processMTSync(mapper);
8110 processSync(mapper);
8111
8112 NotifyMotionArgs args;
8113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8114 ASSERT_EQ(0, args.pointerProperties[0].id);
8115 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8116 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8117 orientation, distance));
8118}
8119
8120TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008121 addConfigurationProperty("touch.deviceType", "touchScreen");
8122 prepareDisplay(DISPLAY_ORIENTATION_0);
8123 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8124 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008125 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008126
8127 // These calculations are based on the input device calibration documentation.
8128 int32_t rawX = 100;
8129 int32_t rawY = 200;
8130 int32_t rawTouchMajor = 140;
8131 int32_t rawTouchMinor = 120;
8132 int32_t rawToolMajor = 180;
8133 int32_t rawToolMinor = 160;
8134
8135 float x = toDisplayX(rawX);
8136 float y = toDisplayY(rawY);
8137 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8138 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8139 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8140 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8141 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8142
8143 processPosition(mapper, rawX, rawY);
8144 processTouchMajor(mapper, rawTouchMajor);
8145 processTouchMinor(mapper, rawTouchMinor);
8146 processToolMajor(mapper, rawToolMajor);
8147 processToolMinor(mapper, rawToolMinor);
8148 processMTSync(mapper);
8149 processSync(mapper);
8150
8151 NotifyMotionArgs args;
8152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8153 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8154 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8155}
8156
8157TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008158 addConfigurationProperty("touch.deviceType", "touchScreen");
8159 prepareDisplay(DISPLAY_ORIENTATION_0);
8160 prepareAxes(POSITION | TOUCH | TOOL);
8161 addConfigurationProperty("touch.size.calibration", "diameter");
8162 addConfigurationProperty("touch.size.scale", "10");
8163 addConfigurationProperty("touch.size.bias", "160");
8164 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008165 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008166
8167 // These calculations are based on the input device calibration documentation.
8168 // Note: We only provide a single common touch/tool value because the device is assumed
8169 // not to emit separate values for each pointer (isSummed = 1).
8170 int32_t rawX = 100;
8171 int32_t rawY = 200;
8172 int32_t rawX2 = 150;
8173 int32_t rawY2 = 250;
8174 int32_t rawTouchMajor = 5;
8175 int32_t rawToolMajor = 8;
8176
8177 float x = toDisplayX(rawX);
8178 float y = toDisplayY(rawY);
8179 float x2 = toDisplayX(rawX2);
8180 float y2 = toDisplayY(rawY2);
8181 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8182 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8183 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8184
8185 processPosition(mapper, rawX, rawY);
8186 processTouchMajor(mapper, rawTouchMajor);
8187 processToolMajor(mapper, rawToolMajor);
8188 processMTSync(mapper);
8189 processPosition(mapper, rawX2, rawY2);
8190 processTouchMajor(mapper, rawTouchMajor);
8191 processToolMajor(mapper, rawToolMajor);
8192 processMTSync(mapper);
8193 processSync(mapper);
8194
8195 NotifyMotionArgs args;
8196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8197 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8198
8199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008200 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008201 ASSERT_EQ(size_t(2), args.pointerCount);
8202 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8203 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8204 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8205 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8206}
8207
8208TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008209 addConfigurationProperty("touch.deviceType", "touchScreen");
8210 prepareDisplay(DISPLAY_ORIENTATION_0);
8211 prepareAxes(POSITION | TOUCH | TOOL);
8212 addConfigurationProperty("touch.size.calibration", "area");
8213 addConfigurationProperty("touch.size.scale", "43");
8214 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008215 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008216
8217 // These calculations are based on the input device calibration documentation.
8218 int32_t rawX = 100;
8219 int32_t rawY = 200;
8220 int32_t rawTouchMajor = 5;
8221 int32_t rawToolMajor = 8;
8222
8223 float x = toDisplayX(rawX);
8224 float y = toDisplayY(rawY);
8225 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8226 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8227 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8228
8229 processPosition(mapper, rawX, rawY);
8230 processTouchMajor(mapper, rawTouchMajor);
8231 processToolMajor(mapper, rawToolMajor);
8232 processMTSync(mapper);
8233 processSync(mapper);
8234
8235 NotifyMotionArgs args;
8236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8237 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8238 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8239}
8240
8241TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008242 addConfigurationProperty("touch.deviceType", "touchScreen");
8243 prepareDisplay(DISPLAY_ORIENTATION_0);
8244 prepareAxes(POSITION | PRESSURE);
8245 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8246 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008247 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008248
Michael Wrightaa449c92017-12-13 21:21:43 +00008249 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008250 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008251 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8252 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8253 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8254
Michael Wrightd02c5b62014-02-10 15:10:22 -08008255 // These calculations are based on the input device calibration documentation.
8256 int32_t rawX = 100;
8257 int32_t rawY = 200;
8258 int32_t rawPressure = 60;
8259
8260 float x = toDisplayX(rawX);
8261 float y = toDisplayY(rawY);
8262 float pressure = float(rawPressure) * 0.01f;
8263
8264 processPosition(mapper, rawX, rawY);
8265 processPressure(mapper, rawPressure);
8266 processMTSync(mapper);
8267 processSync(mapper);
8268
8269 NotifyMotionArgs args;
8270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8271 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8272 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8273}
8274
8275TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008276 addConfigurationProperty("touch.deviceType", "touchScreen");
8277 prepareDisplay(DISPLAY_ORIENTATION_0);
8278 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008279 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008280
8281 NotifyMotionArgs motionArgs;
8282 NotifyKeyArgs keyArgs;
8283
8284 processId(mapper, 1);
8285 processPosition(mapper, 100, 200);
8286 processSync(mapper);
8287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8288 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8289 ASSERT_EQ(0, motionArgs.buttonState);
8290
8291 // press BTN_LEFT, release BTN_LEFT
8292 processKey(mapper, BTN_LEFT, 1);
8293 processSync(mapper);
8294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8295 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8296 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8297
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8299 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8300 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8301
Michael Wrightd02c5b62014-02-10 15:10:22 -08008302 processKey(mapper, BTN_LEFT, 0);
8303 processSync(mapper);
8304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008305 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008306 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008307
8308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008309 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008310 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008311
8312 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8313 processKey(mapper, BTN_RIGHT, 1);
8314 processKey(mapper, BTN_MIDDLE, 1);
8315 processSync(mapper);
8316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8317 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8318 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8319 motionArgs.buttonState);
8320
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8322 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8323 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8324
8325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8326 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8327 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8328 motionArgs.buttonState);
8329
Michael Wrightd02c5b62014-02-10 15:10:22 -08008330 processKey(mapper, BTN_RIGHT, 0);
8331 processSync(mapper);
8332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008333 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008334 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008335
8336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008337 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008338 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008339
8340 processKey(mapper, BTN_MIDDLE, 0);
8341 processSync(mapper);
8342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008343 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008344 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008345
8346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008347 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008348 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008349
8350 // press BTN_BACK, release BTN_BACK
8351 processKey(mapper, BTN_BACK, 1);
8352 processSync(mapper);
8353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8354 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8355 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008356
Michael Wrightd02c5b62014-02-10 15:10:22 -08008357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008358 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008359 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8360
8361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8362 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8363 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008364
8365 processKey(mapper, BTN_BACK, 0);
8366 processSync(mapper);
8367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008368 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008369 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008370
8371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008372 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008373 ASSERT_EQ(0, motionArgs.buttonState);
8374
Michael Wrightd02c5b62014-02-10 15:10:22 -08008375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8376 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8377 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8378
8379 // press BTN_SIDE, release BTN_SIDE
8380 processKey(mapper, BTN_SIDE, 1);
8381 processSync(mapper);
8382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8383 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8384 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008385
Michael Wrightd02c5b62014-02-10 15:10:22 -08008386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008387 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008388 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8389
8390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8391 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8392 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008393
8394 processKey(mapper, BTN_SIDE, 0);
8395 processSync(mapper);
8396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008397 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008398 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008399
8400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008401 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008402 ASSERT_EQ(0, motionArgs.buttonState);
8403
Michael Wrightd02c5b62014-02-10 15:10:22 -08008404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8405 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8406 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8407
8408 // press BTN_FORWARD, release BTN_FORWARD
8409 processKey(mapper, BTN_FORWARD, 1);
8410 processSync(mapper);
8411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8412 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8413 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008414
Michael Wrightd02c5b62014-02-10 15:10:22 -08008415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008416 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008417 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8418
8419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8420 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8421 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008422
8423 processKey(mapper, BTN_FORWARD, 0);
8424 processSync(mapper);
8425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008426 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008427 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008428
8429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008430 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008431 ASSERT_EQ(0, motionArgs.buttonState);
8432
Michael Wrightd02c5b62014-02-10 15:10:22 -08008433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8434 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8435 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8436
8437 // press BTN_EXTRA, release BTN_EXTRA
8438 processKey(mapper, BTN_EXTRA, 1);
8439 processSync(mapper);
8440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8441 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8442 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008443
Michael Wrightd02c5b62014-02-10 15:10:22 -08008444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008445 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008446 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8447
8448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8449 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8450 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008451
8452 processKey(mapper, BTN_EXTRA, 0);
8453 processSync(mapper);
8454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008455 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008456 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008457
8458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008459 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008460 ASSERT_EQ(0, motionArgs.buttonState);
8461
Michael Wrightd02c5b62014-02-10 15:10:22 -08008462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8463 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8464 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8465
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8467
Michael Wrightd02c5b62014-02-10 15:10:22 -08008468 // press BTN_STYLUS, release BTN_STYLUS
8469 processKey(mapper, BTN_STYLUS, 1);
8470 processSync(mapper);
8471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8472 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008473 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8474
8475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8476 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8477 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008478
8479 processKey(mapper, BTN_STYLUS, 0);
8480 processSync(mapper);
8481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008482 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008483 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008484
8485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008486 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008487 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008488
8489 // press BTN_STYLUS2, release BTN_STYLUS2
8490 processKey(mapper, BTN_STYLUS2, 1);
8491 processSync(mapper);
8492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8493 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008494 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8495
8496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8497 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8498 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008499
8500 processKey(mapper, BTN_STYLUS2, 0);
8501 processSync(mapper);
8502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008503 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008504 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008505
8506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008507 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008508 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008509
8510 // release touch
8511 processId(mapper, -1);
8512 processSync(mapper);
8513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8514 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8515 ASSERT_EQ(0, motionArgs.buttonState);
8516}
8517
8518TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008519 addConfigurationProperty("touch.deviceType", "touchScreen");
8520 prepareDisplay(DISPLAY_ORIENTATION_0);
8521 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008522 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008523
8524 NotifyMotionArgs motionArgs;
8525
8526 // default tool type is finger
8527 processId(mapper, 1);
8528 processPosition(mapper, 100, 200);
8529 processSync(mapper);
8530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8531 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8532 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8533
8534 // eraser
8535 processKey(mapper, BTN_TOOL_RUBBER, 1);
8536 processSync(mapper);
8537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8538 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8539 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8540
8541 // stylus
8542 processKey(mapper, BTN_TOOL_RUBBER, 0);
8543 processKey(mapper, BTN_TOOL_PEN, 1);
8544 processSync(mapper);
8545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8546 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8547 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8548
8549 // brush
8550 processKey(mapper, BTN_TOOL_PEN, 0);
8551 processKey(mapper, BTN_TOOL_BRUSH, 1);
8552 processSync(mapper);
8553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8554 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8555 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8556
8557 // pencil
8558 processKey(mapper, BTN_TOOL_BRUSH, 0);
8559 processKey(mapper, BTN_TOOL_PENCIL, 1);
8560 processSync(mapper);
8561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8562 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8563 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8564
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008565 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008566 processKey(mapper, BTN_TOOL_PENCIL, 0);
8567 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8568 processSync(mapper);
8569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8570 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8571 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8572
8573 // mouse
8574 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8575 processKey(mapper, BTN_TOOL_MOUSE, 1);
8576 processSync(mapper);
8577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8578 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8579 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8580
8581 // lens
8582 processKey(mapper, BTN_TOOL_MOUSE, 0);
8583 processKey(mapper, BTN_TOOL_LENS, 1);
8584 processSync(mapper);
8585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8586 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8587 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8588
8589 // double-tap
8590 processKey(mapper, BTN_TOOL_LENS, 0);
8591 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8592 processSync(mapper);
8593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8594 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8595 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8596
8597 // triple-tap
8598 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8599 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8600 processSync(mapper);
8601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8602 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8603 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8604
8605 // quad-tap
8606 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8607 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8608 processSync(mapper);
8609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8610 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8611 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8612
8613 // finger
8614 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8615 processKey(mapper, BTN_TOOL_FINGER, 1);
8616 processSync(mapper);
8617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8618 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8619 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8620
8621 // stylus trumps finger
8622 processKey(mapper, BTN_TOOL_PEN, 1);
8623 processSync(mapper);
8624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8625 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8626 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8627
8628 // eraser trumps stylus
8629 processKey(mapper, BTN_TOOL_RUBBER, 1);
8630 processSync(mapper);
8631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8632 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8633 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8634
8635 // mouse trumps eraser
8636 processKey(mapper, BTN_TOOL_MOUSE, 1);
8637 processSync(mapper);
8638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8639 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8640 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8641
8642 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8643 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8644 processSync(mapper);
8645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8646 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8647 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8648
8649 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8650 processToolType(mapper, MT_TOOL_PEN);
8651 processSync(mapper);
8652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8653 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8654 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8655
8656 // back to default tool type
8657 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8658 processKey(mapper, BTN_TOOL_MOUSE, 0);
8659 processKey(mapper, BTN_TOOL_RUBBER, 0);
8660 processKey(mapper, BTN_TOOL_PEN, 0);
8661 processKey(mapper, BTN_TOOL_FINGER, 0);
8662 processSync(mapper);
8663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8664 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8665 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8666}
8667
8668TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008669 addConfigurationProperty("touch.deviceType", "touchScreen");
8670 prepareDisplay(DISPLAY_ORIENTATION_0);
8671 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008672 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008673 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008674
8675 NotifyMotionArgs motionArgs;
8676
8677 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8678 processId(mapper, 1);
8679 processPosition(mapper, 100, 200);
8680 processSync(mapper);
8681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8682 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8683 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8684 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8685
8686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8687 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8688 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8689 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8690
8691 // move a little
8692 processPosition(mapper, 150, 250);
8693 processSync(mapper);
8694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8695 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8697 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8698
8699 // down when BTN_TOUCH is pressed, pressure defaults to 1
8700 processKey(mapper, BTN_TOUCH, 1);
8701 processSync(mapper);
8702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8703 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8704 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8705 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8706
8707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8708 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8709 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8710 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8711
8712 // up when BTN_TOUCH is released, hover restored
8713 processKey(mapper, BTN_TOUCH, 0);
8714 processSync(mapper);
8715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8716 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8718 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8719
8720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8721 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8722 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8723 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8724
8725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8726 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8727 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8728 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8729
8730 // exit hover when pointer goes away
8731 processId(mapper, -1);
8732 processSync(mapper);
8733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8734 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8735 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8736 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8737}
8738
8739TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008740 addConfigurationProperty("touch.deviceType", "touchScreen");
8741 prepareDisplay(DISPLAY_ORIENTATION_0);
8742 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008743 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008744
8745 NotifyMotionArgs motionArgs;
8746
8747 // initially hovering because pressure is 0
8748 processId(mapper, 1);
8749 processPosition(mapper, 100, 200);
8750 processPressure(mapper, 0);
8751 processSync(mapper);
8752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8753 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8754 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8755 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8756
8757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8758 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8760 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8761
8762 // move a little
8763 processPosition(mapper, 150, 250);
8764 processSync(mapper);
8765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8766 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8768 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8769
8770 // down when pressure becomes non-zero
8771 processPressure(mapper, RAW_PRESSURE_MAX);
8772 processSync(mapper);
8773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8774 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8776 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8777
8778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8779 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8780 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8781 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8782
8783 // up when pressure becomes 0, hover restored
8784 processPressure(mapper, 0);
8785 processSync(mapper);
8786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8787 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8789 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8790
8791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8792 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8793 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8794 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8795
8796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8797 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8798 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8799 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8800
8801 // exit hover when pointer goes away
8802 processId(mapper, -1);
8803 processSync(mapper);
8804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8805 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8806 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8807 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8808}
8809
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008810/**
8811 * Set the input device port <--> display port associations, and check that the
8812 * events are routed to the display that matches the display port.
8813 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8814 */
8815TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008816 const std::string usb2 = "USB2";
8817 const uint8_t hdmi1 = 0;
8818 const uint8_t hdmi2 = 1;
8819 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008820 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008821
8822 addConfigurationProperty("touch.deviceType", "touchScreen");
8823 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008824 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008825
8826 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8827 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8828
8829 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8830 // for this input device is specified, and the matching viewport is not present,
8831 // the input device should be disabled (at the mapper level).
8832
8833 // Add viewport for display 2 on hdmi2
8834 prepareSecondaryDisplay(type, hdmi2);
8835 // Send a touch event
8836 processPosition(mapper, 100, 100);
8837 processSync(mapper);
8838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8839
8840 // Add viewport for display 1 on hdmi1
8841 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8842 // Send a touch event again
8843 processPosition(mapper, 100, 100);
8844 processSync(mapper);
8845
8846 NotifyMotionArgs args;
8847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8848 ASSERT_EQ(DISPLAY_ID, args.displayId);
8849}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008850
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008851TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8852 addConfigurationProperty("touch.deviceType", "touchScreen");
8853 prepareAxes(POSITION);
8854 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8855
8856 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8857
8858 prepareDisplay(DISPLAY_ORIENTATION_0);
8859 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8860
8861 // Send a touch event
8862 processPosition(mapper, 100, 100);
8863 processSync(mapper);
8864
8865 NotifyMotionArgs args;
8866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8867 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8868}
8869
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008870TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008871 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008872 std::shared_ptr<FakePointerController> fakePointerController =
8873 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008874 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008875 fakePointerController->setPosition(100, 200);
8876 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008877 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008878
Garfield Tan888a6a42020-01-09 11:39:16 -08008879 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008880 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008881
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008882 prepareDisplay(DISPLAY_ORIENTATION_0);
8883 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008884 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008885
8886 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008887 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008888
8889 NotifyMotionArgs motionArgs;
8890 processPosition(mapper, 100, 100);
8891 processSync(mapper);
8892
8893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8894 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8895 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8896}
8897
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008898/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008899 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8900 */
8901TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8902 addConfigurationProperty("touch.deviceType", "touchScreen");
8903 prepareAxes(POSITION);
8904 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8905
8906 prepareDisplay(DISPLAY_ORIENTATION_0);
8907 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8908 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8909 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8910 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8911
8912 NotifyMotionArgs args;
8913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8914 ASSERT_EQ(26, args.readTime);
8915
8916 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8917 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8918 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8919
8920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8921 ASSERT_EQ(33, args.readTime);
8922}
8923
8924/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008925 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8926 * events should not be delivered to the listener.
8927 */
8928TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8929 addConfigurationProperty("touch.deviceType", "touchScreen");
8930 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8931 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8932 ViewportType::INTERNAL);
8933 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8934 prepareAxes(POSITION);
8935 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8936
8937 NotifyMotionArgs motionArgs;
8938 processPosition(mapper, 100, 100);
8939 processSync(mapper);
8940
8941 mFakeListener->assertNotifyMotionWasNotCalled();
8942}
8943
Garfield Tanc734e4f2021-01-15 20:01:39 -08008944TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8945 addConfigurationProperty("touch.deviceType", "touchScreen");
8946 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8947 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8948 ViewportType::INTERNAL);
8949 std::optional<DisplayViewport> optionalDisplayViewport =
8950 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8951 ASSERT_TRUE(optionalDisplayViewport.has_value());
8952 DisplayViewport displayViewport = *optionalDisplayViewport;
8953
8954 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8955 prepareAxes(POSITION);
8956 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8957
8958 // Finger down
8959 int32_t x = 100, y = 100;
8960 processPosition(mapper, x, y);
8961 processSync(mapper);
8962
8963 NotifyMotionArgs motionArgs;
8964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8965 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8966
8967 // Deactivate display viewport
8968 displayViewport.isActive = false;
8969 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8970 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8971
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008972 // The ongoing touch should be canceled immediately
8973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8974 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8975
8976 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008977 x += 10, y += 10;
8978 processPosition(mapper, x, y);
8979 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008981
8982 // Reactivate display viewport
8983 displayViewport.isActive = true;
8984 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8985 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8986
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008987 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008988 x += 10, y += 10;
8989 processPosition(mapper, x, y);
8990 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8992 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008993}
8994
Arthur Hung7c645402019-01-25 17:45:42 +08008995TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8996 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008997 prepareAxes(POSITION | ID | SLOT);
8998 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008999 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009000
9001 // Create the second touch screen device, and enable multi fingers.
9002 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009003 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009004 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009005 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009006 std::shared_ptr<InputDevice> device2 =
9007 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009008 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009009
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009010 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9011 0 /*flat*/, 0 /*fuzz*/);
9012 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9013 0 /*flat*/, 0 /*fuzz*/);
9014 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
9015 0 /*flat*/, 0 /*fuzz*/);
9016 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
9017 0 /*flat*/, 0 /*fuzz*/);
9018 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
9019 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9020 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009021
9022 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009023 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009024 std::list<NotifyArgs> unused =
9025 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9026 0 /*changes*/);
9027 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009028
9029 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009030 std::shared_ptr<FakePointerController> fakePointerController =
9031 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009032 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009033
9034 // Setup policy for associated displays and show touches.
9035 const uint8_t hdmi1 = 0;
9036 const uint8_t hdmi2 = 1;
9037 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9038 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9039 mFakePolicy->setShowTouches(true);
9040
9041 // Create displays.
9042 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009043 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009044
9045 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009046 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9047 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
9048 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009049
9050 // Two fingers down at default display.
9051 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9052 processPosition(mapper, x1, y1);
9053 processId(mapper, 1);
9054 processSlot(mapper, 1);
9055 processPosition(mapper, x2, y2);
9056 processId(mapper, 2);
9057 processSync(mapper);
9058
9059 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9060 fakePointerController->getSpots().find(DISPLAY_ID);
9061 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9062 ASSERT_EQ(size_t(2), iter->second.size());
9063
9064 // Two fingers down at second display.
9065 processPosition(mapper2, x1, y1);
9066 processId(mapper2, 1);
9067 processSlot(mapper2, 1);
9068 processPosition(mapper2, x2, y2);
9069 processId(mapper2, 2);
9070 processSync(mapper2);
9071
9072 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9073 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9074 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009075
9076 // Disable the show touches configuration and ensure the spots are cleared.
9077 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009078 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9079 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009080
9081 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009082}
9083
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009084TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009085 prepareAxes(POSITION);
9086 addConfigurationProperty("touch.deviceType", "touchScreen");
9087 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009088 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009089
9090 NotifyMotionArgs motionArgs;
9091 // Unrotated video frame
9092 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9093 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009094 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009095 processPosition(mapper, 100, 200);
9096 processSync(mapper);
9097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9098 ASSERT_EQ(frames, motionArgs.videoFrames);
9099
9100 // Subsequent touch events should not have any videoframes
9101 // This is implemented separately in FakeEventHub,
9102 // but that should match the behaviour of TouchVideoDevice.
9103 processPosition(mapper, 200, 200);
9104 processSync(mapper);
9105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9106 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9107}
9108
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009109TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009110 prepareAxes(POSITION);
9111 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009112 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009113 // Unrotated video frame
9114 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9115 NotifyMotionArgs motionArgs;
9116
9117 // Test all 4 orientations
9118 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009119 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
9120 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9121 clearViewports();
9122 prepareDisplay(orientation);
9123 std::vector<TouchVideoFrame> frames{frame};
9124 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9125 processPosition(mapper, 100, 200);
9126 processSync(mapper);
9127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9128 ASSERT_EQ(frames, motionArgs.videoFrames);
9129 }
9130}
9131
9132TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9133 prepareAxes(POSITION);
9134 addConfigurationProperty("touch.deviceType", "touchScreen");
9135 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9136 // orientation-aware are affected by display rotation.
9137 addConfigurationProperty("touch.orientationAware", "0");
9138 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9139 // Unrotated video frame
9140 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9141 NotifyMotionArgs motionArgs;
9142
9143 // Test all 4 orientations
9144 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009145 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
9146 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9147 clearViewports();
9148 prepareDisplay(orientation);
9149 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009150 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009151 processPosition(mapper, 100, 200);
9152 processSync(mapper);
9153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009154 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9155 // compared to the display. This is so that when the window transform (which contains the
9156 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9157 // window's coordinate space.
9158 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009159 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009160
9161 // Release finger.
9162 processSync(mapper);
9163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009164 }
9165}
9166
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009167TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009168 prepareAxes(POSITION);
9169 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009170 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009171 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9172 // so mix these.
9173 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9174 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9175 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9176 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9177 NotifyMotionArgs motionArgs;
9178
9179 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009180 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009181 processPosition(mapper, 100, 200);
9182 processSync(mapper);
9183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009184 ASSERT_EQ(frames, motionArgs.videoFrames);
9185}
9186
9187TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9188 prepareAxes(POSITION);
9189 addConfigurationProperty("touch.deviceType", "touchScreen");
9190 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9191 // orientation-aware are affected by display rotation.
9192 addConfigurationProperty("touch.orientationAware", "0");
9193 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9194 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9195 // so mix these.
9196 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9197 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9198 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9199 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9200 NotifyMotionArgs motionArgs;
9201
9202 prepareDisplay(DISPLAY_ORIENTATION_90);
9203 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9204 processPosition(mapper, 100, 200);
9205 processSync(mapper);
9206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9207 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9208 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9209 // compared to the display. This is so that when the window transform (which contains the
9210 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9211 // window's coordinate space.
9212 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
9213 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009214 ASSERT_EQ(frames, motionArgs.videoFrames);
9215}
9216
Arthur Hung9da14732019-09-02 16:16:58 +08009217/**
9218 * If we had defined port associations, but the viewport is not ready, the touch device would be
9219 * expected to be disabled, and it should be enabled after the viewport has found.
9220 */
9221TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009222 constexpr uint8_t hdmi2 = 1;
9223 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009224 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009225
9226 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9227
9228 addConfigurationProperty("touch.deviceType", "touchScreen");
9229 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009230 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009231
9232 ASSERT_EQ(mDevice->isEnabled(), false);
9233
9234 // Add display on hdmi2, the device should be enabled and can receive touch event.
9235 prepareSecondaryDisplay(type, hdmi2);
9236 ASSERT_EQ(mDevice->isEnabled(), true);
9237
9238 // Send a touch event.
9239 processPosition(mapper, 100, 100);
9240 processSync(mapper);
9241
9242 NotifyMotionArgs args;
9243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9244 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9245}
9246
Arthur Hung421eb1c2020-01-16 00:09:42 +08009247TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009248 addConfigurationProperty("touch.deviceType", "touchScreen");
9249 prepareDisplay(DISPLAY_ORIENTATION_0);
9250 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009251 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009252
9253 NotifyMotionArgs motionArgs;
9254
9255 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9256 // finger down
9257 processId(mapper, 1);
9258 processPosition(mapper, x1, y1);
9259 processSync(mapper);
9260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9261 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9262 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9263
9264 // finger move
9265 processId(mapper, 1);
9266 processPosition(mapper, x2, y2);
9267 processSync(mapper);
9268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9269 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9270 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9271
9272 // finger up.
9273 processId(mapper, -1);
9274 processSync(mapper);
9275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9276 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9277 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9278
9279 // new finger down
9280 processId(mapper, 1);
9281 processPosition(mapper, x3, y3);
9282 processSync(mapper);
9283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9284 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9285 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9286}
9287
9288/**
arthurhungcc7f9802020-04-30 17:55:40 +08009289 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9290 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009291 */
arthurhungcc7f9802020-04-30 17:55:40 +08009292TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009293 addConfigurationProperty("touch.deviceType", "touchScreen");
9294 prepareDisplay(DISPLAY_ORIENTATION_0);
9295 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009296 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009297
9298 NotifyMotionArgs motionArgs;
9299
9300 // default tool type is finger
9301 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009302 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009303 processPosition(mapper, x1, y1);
9304 processSync(mapper);
9305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9306 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9307 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9308
9309 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9310 processToolType(mapper, MT_TOOL_PALM);
9311 processSync(mapper);
9312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9313 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9314
9315 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009316 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009317 processPosition(mapper, x2, y2);
9318 processSync(mapper);
9319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9320
9321 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009322 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009323 processSync(mapper);
9324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9325
9326 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009327 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009328 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009329 processPosition(mapper, x3, y3);
9330 processSync(mapper);
9331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9332 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9333 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9334}
9335
arthurhungbf89a482020-04-17 17:37:55 +08009336/**
arthurhungcc7f9802020-04-30 17:55:40 +08009337 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9338 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009339 */
arthurhungcc7f9802020-04-30 17:55:40 +08009340TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009341 addConfigurationProperty("touch.deviceType", "touchScreen");
9342 prepareDisplay(DISPLAY_ORIENTATION_0);
9343 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9344 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9345
9346 NotifyMotionArgs motionArgs;
9347
9348 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009349 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9350 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009351 processPosition(mapper, x1, y1);
9352 processSync(mapper);
9353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9354 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9355 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9356
9357 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009358 processSlot(mapper, SECOND_SLOT);
9359 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009360 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009361 processSync(mapper);
9362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009363 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009364 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9365
9366 // If the tool type of the first finger changes to MT_TOOL_PALM,
9367 // we expect to receive ACTION_POINTER_UP with cancel flag.
9368 processSlot(mapper, FIRST_SLOT);
9369 processId(mapper, FIRST_TRACKING_ID);
9370 processToolType(mapper, MT_TOOL_PALM);
9371 processSync(mapper);
9372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009373 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009374 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9375
9376 // The following MOVE events of second finger should be processed.
9377 processSlot(mapper, SECOND_SLOT);
9378 processId(mapper, SECOND_TRACKING_ID);
9379 processPosition(mapper, x2 + 1, y2 + 1);
9380 processSync(mapper);
9381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9382 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9383 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9384
9385 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9386 // it. Second finger receive move.
9387 processSlot(mapper, FIRST_SLOT);
9388 processId(mapper, INVALID_TRACKING_ID);
9389 processSync(mapper);
9390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9391 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9392 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9393
9394 // Second finger keeps moving.
9395 processSlot(mapper, SECOND_SLOT);
9396 processId(mapper, SECOND_TRACKING_ID);
9397 processPosition(mapper, x2 + 2, y2 + 2);
9398 processSync(mapper);
9399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9400 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9401 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9402
9403 // Second finger up.
9404 processId(mapper, INVALID_TRACKING_ID);
9405 processSync(mapper);
9406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9407 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9408 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9409}
9410
9411/**
9412 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9413 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9414 */
9415TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9416 addConfigurationProperty("touch.deviceType", "touchScreen");
9417 prepareDisplay(DISPLAY_ORIENTATION_0);
9418 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9419 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9420
9421 NotifyMotionArgs motionArgs;
9422
9423 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9424 // First finger down.
9425 processId(mapper, FIRST_TRACKING_ID);
9426 processPosition(mapper, x1, y1);
9427 processSync(mapper);
9428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9429 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9430 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9431
9432 // Second finger down.
9433 processSlot(mapper, SECOND_SLOT);
9434 processId(mapper, SECOND_TRACKING_ID);
9435 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009436 processSync(mapper);
9437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009438 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009439 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9440
arthurhungcc7f9802020-04-30 17:55:40 +08009441 // If the tool type of the first finger changes to MT_TOOL_PALM,
9442 // we expect to receive ACTION_POINTER_UP with cancel flag.
9443 processSlot(mapper, FIRST_SLOT);
9444 processId(mapper, FIRST_TRACKING_ID);
9445 processToolType(mapper, MT_TOOL_PALM);
9446 processSync(mapper);
9447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009448 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009449 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9450
9451 // Second finger keeps moving.
9452 processSlot(mapper, SECOND_SLOT);
9453 processId(mapper, SECOND_TRACKING_ID);
9454 processPosition(mapper, x2 + 1, y2 + 1);
9455 processSync(mapper);
9456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9457 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9458
9459 // second finger becomes palm, receive cancel due to only 1 finger is active.
9460 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009461 processToolType(mapper, MT_TOOL_PALM);
9462 processSync(mapper);
9463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9464 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9465
arthurhungcc7f9802020-04-30 17:55:40 +08009466 // third finger down.
9467 processSlot(mapper, THIRD_SLOT);
9468 processId(mapper, THIRD_TRACKING_ID);
9469 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009470 processPosition(mapper, x3, y3);
9471 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9473 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9474 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009475 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9476
9477 // third finger move
9478 processId(mapper, THIRD_TRACKING_ID);
9479 processPosition(mapper, x3 + 1, y3 + 1);
9480 processSync(mapper);
9481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9482 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9483
9484 // first finger up, third finger receive move.
9485 processSlot(mapper, FIRST_SLOT);
9486 processId(mapper, INVALID_TRACKING_ID);
9487 processSync(mapper);
9488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9489 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9490 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9491
9492 // second finger up, third finger receive move.
9493 processSlot(mapper, SECOND_SLOT);
9494 processId(mapper, INVALID_TRACKING_ID);
9495 processSync(mapper);
9496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9497 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9498 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9499
9500 // third finger up.
9501 processSlot(mapper, THIRD_SLOT);
9502 processId(mapper, INVALID_TRACKING_ID);
9503 processSync(mapper);
9504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9505 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9506 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9507}
9508
9509/**
9510 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9511 * and the active finger could still be allowed to receive the events
9512 */
9513TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9514 addConfigurationProperty("touch.deviceType", "touchScreen");
9515 prepareDisplay(DISPLAY_ORIENTATION_0);
9516 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9517 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9518
9519 NotifyMotionArgs motionArgs;
9520
9521 // default tool type is finger
9522 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9523 processId(mapper, FIRST_TRACKING_ID);
9524 processPosition(mapper, x1, y1);
9525 processSync(mapper);
9526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9527 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9528 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9529
9530 // Second finger down.
9531 processSlot(mapper, SECOND_SLOT);
9532 processId(mapper, SECOND_TRACKING_ID);
9533 processPosition(mapper, x2, y2);
9534 processSync(mapper);
9535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009536 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009537 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9538
9539 // If the tool type of the second finger changes to MT_TOOL_PALM,
9540 // we expect to receive ACTION_POINTER_UP with cancel flag.
9541 processId(mapper, SECOND_TRACKING_ID);
9542 processToolType(mapper, MT_TOOL_PALM);
9543 processSync(mapper);
9544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009545 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009546 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9547
9548 // The following MOVE event should be processed.
9549 processSlot(mapper, FIRST_SLOT);
9550 processId(mapper, FIRST_TRACKING_ID);
9551 processPosition(mapper, x1 + 1, y1 + 1);
9552 processSync(mapper);
9553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9554 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9555 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9556
9557 // second finger up.
9558 processSlot(mapper, SECOND_SLOT);
9559 processId(mapper, INVALID_TRACKING_ID);
9560 processSync(mapper);
9561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9562 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9563
9564 // first finger keep moving
9565 processSlot(mapper, FIRST_SLOT);
9566 processId(mapper, FIRST_TRACKING_ID);
9567 processPosition(mapper, x1 + 2, y1 + 2);
9568 processSync(mapper);
9569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9570 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9571
9572 // first finger up.
9573 processId(mapper, INVALID_TRACKING_ID);
9574 processSync(mapper);
9575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9576 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9577 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009578}
9579
Arthur Hung9ad18942021-06-19 02:04:46 +00009580/**
9581 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9582 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9583 * cause slot be valid again.
9584 */
9585TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9586 addConfigurationProperty("touch.deviceType", "touchScreen");
9587 prepareDisplay(DISPLAY_ORIENTATION_0);
9588 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9589 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9590
9591 NotifyMotionArgs motionArgs;
9592
9593 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9594 // First finger down.
9595 processId(mapper, FIRST_TRACKING_ID);
9596 processPosition(mapper, x1, y1);
9597 processPressure(mapper, RAW_PRESSURE_MAX);
9598 processSync(mapper);
9599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9600 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9601 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9602
9603 // First finger move.
9604 processId(mapper, FIRST_TRACKING_ID);
9605 processPosition(mapper, x1 + 1, y1 + 1);
9606 processPressure(mapper, RAW_PRESSURE_MAX);
9607 processSync(mapper);
9608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9609 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9610 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9611
9612 // Second finger down.
9613 processSlot(mapper, SECOND_SLOT);
9614 processId(mapper, SECOND_TRACKING_ID);
9615 processPosition(mapper, x2, y2);
9616 processPressure(mapper, RAW_PRESSURE_MAX);
9617 processSync(mapper);
9618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009619 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009620 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9621
9622 // second finger up with some unexpected data.
9623 processSlot(mapper, SECOND_SLOT);
9624 processId(mapper, INVALID_TRACKING_ID);
9625 processPosition(mapper, x2, y2);
9626 processSync(mapper);
9627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009628 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009629 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9630
9631 // first finger up with some unexpected data.
9632 processSlot(mapper, FIRST_SLOT);
9633 processId(mapper, INVALID_TRACKING_ID);
9634 processPosition(mapper, x2, y2);
9635 processPressure(mapper, RAW_PRESSURE_MAX);
9636 processSync(mapper);
9637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9638 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9639 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9640}
9641
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009642TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9643 addConfigurationProperty("touch.deviceType", "touchScreen");
9644 prepareDisplay(DISPLAY_ORIENTATION_0);
9645 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9646 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9647
9648 // First finger down.
9649 processId(mapper, FIRST_TRACKING_ID);
9650 processPosition(mapper, 100, 200);
9651 processPressure(mapper, RAW_PRESSURE_MAX);
9652 processSync(mapper);
9653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9654 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9655
9656 // Second finger down.
9657 processSlot(mapper, SECOND_SLOT);
9658 processId(mapper, SECOND_TRACKING_ID);
9659 processPosition(mapper, 300, 400);
9660 processPressure(mapper, RAW_PRESSURE_MAX);
9661 processSync(mapper);
9662 ASSERT_NO_FATAL_FAILURE(
9663 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9664
9665 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009666 // preserved. Resetting should cancel the ongoing gesture.
9667 resetMapper(mapper, ARBITRARY_TIME);
9668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9669 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009670
9671 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9672 // the existing touch state to generate a down event.
9673 processPosition(mapper, 301, 302);
9674 processSync(mapper);
9675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9676 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
9677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9678 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
9679
9680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9681}
9682
9683TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9684 addConfigurationProperty("touch.deviceType", "touchScreen");
9685 prepareDisplay(DISPLAY_ORIENTATION_0);
9686 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9687 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9688
9689 // First finger touches down and releases.
9690 processId(mapper, FIRST_TRACKING_ID);
9691 processPosition(mapper, 100, 200);
9692 processPressure(mapper, RAW_PRESSURE_MAX);
9693 processSync(mapper);
9694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9695 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9696 processId(mapper, INVALID_TRACKING_ID);
9697 processSync(mapper);
9698 ASSERT_NO_FATAL_FAILURE(
9699 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9700
9701 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9702 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009703 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9705
9706 // Send an empty sync frame. Since there are no pointers, no events are generated.
9707 processSync(mapper);
9708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9709}
9710
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009711// --- MultiTouchInputMapperTest_ExternalDevice ---
9712
9713class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9714protected:
Chris Yea52ade12020-08-27 16:49:20 -07009715 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009716};
9717
9718/**
9719 * Expect fallback to internal viewport if device is external and external viewport is not present.
9720 */
9721TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9722 prepareAxes(POSITION);
9723 addConfigurationProperty("touch.deviceType", "touchScreen");
9724 prepareDisplay(DISPLAY_ORIENTATION_0);
9725 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9726
9727 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9728
9729 NotifyMotionArgs motionArgs;
9730
9731 // Expect the event to be sent to the internal viewport,
9732 // because an external viewport is not present.
9733 processPosition(mapper, 100, 100);
9734 processSync(mapper);
9735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9736 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9737
9738 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009739 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009740 processPosition(mapper, 100, 100);
9741 processSync(mapper);
9742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9743 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9744}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009745
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009746TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9747 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9748 std::shared_ptr<FakePointerController> fakePointerController =
9749 std::make_shared<FakePointerController>();
9750 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9751 fakePointerController->setPosition(0, 0);
9752 fakePointerController->setButtonState(0);
9753
9754 // prepare device and capture
9755 prepareDisplay(DISPLAY_ORIENTATION_0);
9756 prepareAxes(POSITION | ID | SLOT);
9757 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9758 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9759 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009760 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009761 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9762
9763 // captured touchpad should be a touchpad source
9764 NotifyDeviceResetArgs resetArgs;
9765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9766 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9767
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009768 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009769
9770 const InputDeviceInfo::MotionRange* relRangeX =
9771 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9772 ASSERT_NE(relRangeX, nullptr);
9773 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9774 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9775 const InputDeviceInfo::MotionRange* relRangeY =
9776 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9777 ASSERT_NE(relRangeY, nullptr);
9778 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9779 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9780
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009781 // run captured pointer tests - note that this is unscaled, so input listener events should be
9782 // identical to what the hardware sends (accounting for any
9783 // calibration).
9784 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009785 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009786 processId(mapper, 1);
9787 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9788 processKey(mapper, BTN_TOUCH, 1);
9789 processSync(mapper);
9790
9791 // expect coord[0] to contain initial location of touch 0
9792 NotifyMotionArgs args;
9793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9794 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9795 ASSERT_EQ(1U, args.pointerCount);
9796 ASSERT_EQ(0, args.pointerProperties[0].id);
9797 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9798 ASSERT_NO_FATAL_FAILURE(
9799 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9800
9801 // FINGER 1 DOWN
9802 processSlot(mapper, 1);
9803 processId(mapper, 2);
9804 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9805 processSync(mapper);
9806
9807 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009809 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009810 ASSERT_EQ(2U, args.pointerCount);
9811 ASSERT_EQ(0, args.pointerProperties[0].id);
9812 ASSERT_EQ(1, args.pointerProperties[1].id);
9813 ASSERT_NO_FATAL_FAILURE(
9814 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9815 ASSERT_NO_FATAL_FAILURE(
9816 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9817
9818 // FINGER 1 MOVE
9819 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9820 processSync(mapper);
9821
9822 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9823 // from move
9824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9825 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9826 ASSERT_NO_FATAL_FAILURE(
9827 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9828 ASSERT_NO_FATAL_FAILURE(
9829 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9830
9831 // FINGER 0 MOVE
9832 processSlot(mapper, 0);
9833 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9834 processSync(mapper);
9835
9836 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9838 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9839 ASSERT_NO_FATAL_FAILURE(
9840 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9841 ASSERT_NO_FATAL_FAILURE(
9842 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9843
9844 // BUTTON DOWN
9845 processKey(mapper, BTN_LEFT, 1);
9846 processSync(mapper);
9847
9848 // touchinputmapper design sends a move before button press
9849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9850 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9852 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9853
9854 // BUTTON UP
9855 processKey(mapper, BTN_LEFT, 0);
9856 processSync(mapper);
9857
9858 // touchinputmapper design sends a move after button release
9859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9860 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9862 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9863
9864 // FINGER 0 UP
9865 processId(mapper, -1);
9866 processSync(mapper);
9867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9868 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9869
9870 // FINGER 1 MOVE
9871 processSlot(mapper, 1);
9872 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9873 processSync(mapper);
9874
9875 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9877 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9878 ASSERT_EQ(1U, args.pointerCount);
9879 ASSERT_EQ(1, args.pointerProperties[0].id);
9880 ASSERT_NO_FATAL_FAILURE(
9881 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9882
9883 // FINGER 1 UP
9884 processId(mapper, -1);
9885 processKey(mapper, BTN_TOUCH, 0);
9886 processSync(mapper);
9887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9888 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9889
9890 // non captured touchpad should be a mouse source
9891 mFakePolicy->setPointerCapture(false);
9892 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9894 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9895}
9896
9897TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9898 std::shared_ptr<FakePointerController> fakePointerController =
9899 std::make_shared<FakePointerController>();
9900 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9901 fakePointerController->setPosition(0, 0);
9902 fakePointerController->setButtonState(0);
9903
9904 // prepare device and capture
9905 prepareDisplay(DISPLAY_ORIENTATION_0);
9906 prepareAxes(POSITION | ID | SLOT);
9907 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9908 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009909 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009910 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9911 // run uncaptured pointer tests - pushes out generic events
9912 // FINGER 0 DOWN
9913 processId(mapper, 3);
9914 processPosition(mapper, 100, 100);
9915 processKey(mapper, BTN_TOUCH, 1);
9916 processSync(mapper);
9917
9918 // start at (100,100), cursor should be at (0,0) * scale
9919 NotifyMotionArgs args;
9920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9921 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9922 ASSERT_NO_FATAL_FAILURE(
9923 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9924
9925 // FINGER 0 MOVE
9926 processPosition(mapper, 200, 200);
9927 processSync(mapper);
9928
9929 // compute scaling to help with touch position checking
9930 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9931 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9932 float scale =
9933 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9934
9935 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9937 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9938 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9939 0, 0, 0, 0, 0, 0, 0));
9940}
9941
9942TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9943 std::shared_ptr<FakePointerController> fakePointerController =
9944 std::make_shared<FakePointerController>();
9945
9946 prepareDisplay(DISPLAY_ORIENTATION_0);
9947 prepareAxes(POSITION | ID | SLOT);
9948 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009949 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009950 mFakePolicy->setPointerCapture(false);
9951 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9952
9953 // uncaptured touchpad should be a pointer device
9954 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9955
9956 // captured touchpad should be a touchpad device
9957 mFakePolicy->setPointerCapture(true);
9958 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9959 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9960}
9961
HQ Liue6983c72022-04-19 22:14:56 +00009962class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9963protected:
9964 float mPointerMovementScale;
9965 float mPointerXZoomScale;
9966 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9967 addConfigurationProperty("touch.deviceType", "pointer");
9968 std::shared_ptr<FakePointerController> fakePointerController =
9969 std::make_shared<FakePointerController>();
9970 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9971 fakePointerController->setPosition(0, 0);
9972 fakePointerController->setButtonState(0);
9973 prepareDisplay(DISPLAY_ORIENTATION_0);
9974
9975 prepareAxes(POSITION);
9976 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9977 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9978 // needs to be disabled, and the pointer gesture needs to be enabled.
9979 mFakePolicy->setPointerCapture(false);
9980 mFakePolicy->setPointerGestureEnabled(true);
9981 mFakePolicy->setPointerController(fakePointerController);
9982
9983 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9984 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9985 mPointerMovementScale =
9986 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9987 mPointerXZoomScale =
9988 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9989 }
9990
9991 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9992 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9993 /*flat*/ 0,
9994 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9995 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9996 /*flat*/ 0,
9997 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9998 }
9999};
10000
10001/**
10002 * Two fingers down on a pointer mode touch pad. The width
10003 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10004 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10005 * be greater than the both value to be freeform gesture, so that after two
10006 * fingers start to move downwards, the gesture should be swipe.
10007 */
10008TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10009 // The min freeform gesture width is 25units/mm x 30mm = 750
10010 // which is greater than fraction of the diagnal length of the touchpad (349).
10011 // Thus, MaxSwipWidth is 750.
10012 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10013 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10014 NotifyMotionArgs motionArgs;
10015
10016 // Two fingers down at once.
10017 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10018 // Pointer's initial position is used the [0,0] coordinate.
10019 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10020
10021 processId(mapper, FIRST_TRACKING_ID);
10022 processPosition(mapper, x1, y1);
10023 processMTSync(mapper);
10024 processId(mapper, SECOND_TRACKING_ID);
10025 processPosition(mapper, x2, y2);
10026 processMTSync(mapper);
10027 processSync(mapper);
10028
10029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10030 ASSERT_EQ(1U, motionArgs.pointerCount);
10031 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10032 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010033 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010034 ASSERT_NO_FATAL_FAILURE(
10035 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10036
10037 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10038 // that there should be 1 pointer.
10039 int32_t movingDistance = 200;
10040 y1 += movingDistance;
10041 y2 += movingDistance;
10042
10043 processId(mapper, FIRST_TRACKING_ID);
10044 processPosition(mapper, x1, y1);
10045 processMTSync(mapper);
10046 processId(mapper, SECOND_TRACKING_ID);
10047 processPosition(mapper, x2, y2);
10048 processMTSync(mapper);
10049 processSync(mapper);
10050
10051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10052 ASSERT_EQ(1U, motionArgs.pointerCount);
10053 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10054 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010055 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010056 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10057 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10058 0, 0, 0, 0));
10059}
10060
10061/**
10062 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10063 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10064 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10065 * value to be freeform gesture, so that after two fingers start to move downwards,
10066 * the gesture should be swipe.
10067 */
10068TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10069 // The min freeform gesture width is 5units/mm x 30mm = 150
10070 // which is greater than fraction of the diagnal length of the touchpad (349).
10071 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
10072 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
10073 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10074 NotifyMotionArgs motionArgs;
10075
10076 // Two fingers down at once.
10077 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10078 // Pointer's initial position is used the [0,0] coordinate.
10079 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10080
10081 processId(mapper, FIRST_TRACKING_ID);
10082 processPosition(mapper, x1, y1);
10083 processMTSync(mapper);
10084 processId(mapper, SECOND_TRACKING_ID);
10085 processPosition(mapper, x2, y2);
10086 processMTSync(mapper);
10087 processSync(mapper);
10088
10089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10090 ASSERT_EQ(1U, motionArgs.pointerCount);
10091 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10092 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010093 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010094 ASSERT_NO_FATAL_FAILURE(
10095 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10096
10097 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10098 // and there should be 1 pointer.
10099 int32_t movingDistance = 200;
10100 y1 += movingDistance;
10101 y2 += movingDistance;
10102
10103 processId(mapper, FIRST_TRACKING_ID);
10104 processPosition(mapper, x1, y1);
10105 processMTSync(mapper);
10106 processId(mapper, SECOND_TRACKING_ID);
10107 processPosition(mapper, x2, y2);
10108 processMTSync(mapper);
10109 processSync(mapper);
10110
10111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10112 ASSERT_EQ(1U, motionArgs.pointerCount);
10113 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10114 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010115 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010116 // New coordinate is the scaled relative coordinate from the initial coordinate.
10117 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10118 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10119 0, 0, 0, 0));
10120}
10121
10122/**
10123 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10124 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10125 * freeform gestures after two fingers start to move downwards.
10126 */
10127TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
10128 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10129 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10130
10131 NotifyMotionArgs motionArgs;
10132
10133 // Two fingers down at once. Wider than the max swipe width.
10134 // The gesture is expected to be PRESS, then transformed to FREEFORM
10135 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10136
10137 processId(mapper, FIRST_TRACKING_ID);
10138 processPosition(mapper, x1, y1);
10139 processMTSync(mapper);
10140 processId(mapper, SECOND_TRACKING_ID);
10141 processPosition(mapper, x2, y2);
10142 processMTSync(mapper);
10143 processSync(mapper);
10144
10145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10146 ASSERT_EQ(1U, motionArgs.pointerCount);
10147 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10148 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010149 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010150 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10151 ASSERT_NO_FATAL_FAILURE(
10152 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10153
10154 int32_t movingDistance = 200;
10155
10156 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10157 // then two down events for two pointers.
10158 y1 += movingDistance;
10159 y2 += movingDistance;
10160
10161 processId(mapper, FIRST_TRACKING_ID);
10162 processPosition(mapper, x1, y1);
10163 processMTSync(mapper);
10164 processId(mapper, SECOND_TRACKING_ID);
10165 processPosition(mapper, x2, y2);
10166 processMTSync(mapper);
10167 processSync(mapper);
10168
10169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10170 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10171 ASSERT_EQ(1U, motionArgs.pointerCount);
10172 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10174 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10175 ASSERT_EQ(1U, motionArgs.pointerCount);
10176 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10178 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010179 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010180 ASSERT_EQ(2U, motionArgs.pointerCount);
10181 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
10182 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010183 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010184 // Two pointers' scaled relative coordinates from their initial centroid.
10185 // Initial y coordinates are 0 as y1 and y2 have the same value.
10186 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10187 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10188 // When pointers move, the new coordinates equal to the initial coordinates plus
10189 // scaled moving distance.
10190 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10191 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10192 0, 0, 0, 0));
10193 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10194 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10195 0, 0, 0, 0));
10196
10197 // Move two fingers down again, expect one MOVE motion event.
10198 y1 += movingDistance;
10199 y2 += movingDistance;
10200
10201 processId(mapper, FIRST_TRACKING_ID);
10202 processPosition(mapper, x1, y1);
10203 processMTSync(mapper);
10204 processId(mapper, SECOND_TRACKING_ID);
10205 processPosition(mapper, x2, y2);
10206 processMTSync(mapper);
10207 processSync(mapper);
10208
10209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10210 ASSERT_EQ(2U, motionArgs.pointerCount);
10211 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10212 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010213 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010214 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10215 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10216 0, 0, 0, 0, 0));
10217 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10218 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10219 0, 0, 0, 0, 0));
10220}
10221
Harry Cutts39b7ca22022-10-05 15:55:48 +000010222TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
10223 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10224 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10225 NotifyMotionArgs motionArgs;
10226
10227 // Place two fingers down.
10228 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10229
10230 processId(mapper, FIRST_TRACKING_ID);
10231 processPosition(mapper, x1, y1);
10232 processMTSync(mapper);
10233 processId(mapper, SECOND_TRACKING_ID);
10234 processPosition(mapper, x2, y2);
10235 processMTSync(mapper);
10236 processSync(mapper);
10237
10238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10239 ASSERT_EQ(1U, motionArgs.pointerCount);
10240 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10241 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10242 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10243 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10244
10245 // Move the two fingers down and to the left.
10246 int32_t movingDistance = 200;
10247 x1 -= movingDistance;
10248 y1 += movingDistance;
10249 x2 -= movingDistance;
10250 y2 += movingDistance;
10251
10252 processId(mapper, FIRST_TRACKING_ID);
10253 processPosition(mapper, x1, y1);
10254 processMTSync(mapper);
10255 processId(mapper, SECOND_TRACKING_ID);
10256 processPosition(mapper, x2, y2);
10257 processMTSync(mapper);
10258 processSync(mapper);
10259
10260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10261 ASSERT_EQ(1U, motionArgs.pointerCount);
10262 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10263 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10264 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10265 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10266}
10267
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010268// --- JoystickInputMapperTest ---
10269
10270class JoystickInputMapperTest : public InputMapperTest {
10271protected:
10272 static const int32_t RAW_X_MIN;
10273 static const int32_t RAW_X_MAX;
10274 static const int32_t RAW_Y_MIN;
10275 static const int32_t RAW_Y_MAX;
10276
10277 void SetUp() override {
10278 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10279 }
10280 void prepareAxes() {
10281 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10282 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10283 }
10284
10285 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10286 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10287 }
10288
10289 void processSync(JoystickInputMapper& mapper) {
10290 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10291 }
10292
10293 void prepareVirtualDisplay(int32_t orientation) {
10294 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10295 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10296 NO_PORT, ViewportType::VIRTUAL);
10297 }
10298};
10299
10300const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10301const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10302const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10303const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10304
10305TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10306 prepareAxes();
10307 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10308
10309 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10310
10311 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
10312
10313 // Send an axis event
10314 processAxis(mapper, ABS_X, 100);
10315 processSync(mapper);
10316
10317 NotifyMotionArgs args;
10318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10319 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10320
10321 // Send another axis event
10322 processAxis(mapper, ABS_Y, 100);
10323 processSync(mapper);
10324
10325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10326 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10327}
10328
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010329// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010330
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010331class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010332protected:
10333 static const char* DEVICE_NAME;
10334 static const char* DEVICE_LOCATION;
10335 static const int32_t DEVICE_ID;
10336 static const int32_t DEVICE_GENERATION;
10337 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010338 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010339 static const int32_t EVENTHUB_ID;
10340
10341 std::shared_ptr<FakeEventHub> mFakeEventHub;
10342 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010343 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010344 std::unique_ptr<InstrumentedInputReader> mReader;
10345 std::shared_ptr<InputDevice> mDevice;
10346
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010347 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010348 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010349 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010350 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010351 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010352 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010353 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10354 }
10355
10356 void SetUp() override { SetUp(DEVICE_CLASSES); }
10357
10358 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010359 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010360 mFakePolicy.clear();
10361 }
10362
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010363 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010364 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10365 mReader->requestRefreshConfiguration(changes);
10366 mReader->loopOnce();
10367 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010368 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010369 }
10370
10371 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10372 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010373 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010374 InputDeviceIdentifier identifier;
10375 identifier.name = name;
10376 identifier.location = location;
10377 std::shared_ptr<InputDevice> device =
10378 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10379 identifier);
10380 mReader->pushNextDevice(device);
10381 mFakeEventHub->addDevice(eventHubId, name, classes);
10382 mReader->loopOnce();
10383 return device;
10384 }
10385
10386 template <class T, typename... Args>
10387 T& addControllerAndConfigure(Args... args) {
10388 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10389
10390 return controller;
10391 }
10392};
10393
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010394const char* PeripheralControllerTest::DEVICE_NAME = "device";
10395const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10396const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10397const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10398const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010399const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10400 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010401const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010402
10403// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010404class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010405protected:
10406 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010407 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010408 }
10409};
10410
10411TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010412 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010413
10414 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
10415 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
10416}
10417
10418TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010419 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010420
10421 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
10422 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
10423}
10424
10425// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010426class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010427protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010428 void SetUp() override {
10429 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10430 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010431};
10432
Chris Ye85758332021-05-16 23:05:17 -070010433TEST_F(LightControllerTest, MonoLight) {
10434 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010435 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010436 .maxBrightness = 255,
10437 .flags = InputLightClass::BRIGHTNESS,
10438 .path = ""};
10439 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010440
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010441 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010442 InputDeviceInfo info;
10443 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010444 std::vector<InputDeviceLightInfo> lights = info.getLights();
10445 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010446 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10447 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10448
10449 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10450 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10451}
10452
10453TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10454 RawLightInfo infoMono = {.id = 1,
10455 .name = "mono_keyboard_backlight",
10456 .maxBrightness = 255,
10457 .flags = InputLightClass::BRIGHTNESS |
10458 InputLightClass::KEYBOARD_BACKLIGHT,
10459 .path = ""};
10460 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10461
10462 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10463 InputDeviceInfo info;
10464 controller.populateDeviceInfo(&info);
10465 std::vector<InputDeviceLightInfo> lights = info.getLights();
10466 ASSERT_EQ(1U, lights.size());
10467 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10468 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010469
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010470 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10471 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010472}
10473
10474TEST_F(LightControllerTest, RGBLight) {
10475 RawLightInfo infoRed = {.id = 1,
10476 .name = "red",
10477 .maxBrightness = 255,
10478 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10479 .path = ""};
10480 RawLightInfo infoGreen = {.id = 2,
10481 .name = "green",
10482 .maxBrightness = 255,
10483 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10484 .path = ""};
10485 RawLightInfo infoBlue = {.id = 3,
10486 .name = "blue",
10487 .maxBrightness = 255,
10488 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10489 .path = ""};
10490 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10491 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10492 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10493
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010494 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010495 InputDeviceInfo info;
10496 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010497 std::vector<InputDeviceLightInfo> lights = info.getLights();
10498 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010499 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10500 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10501 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10502
10503 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10504 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10505}
10506
10507TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10508 RawLightInfo infoRed = {.id = 1,
10509 .name = "red_keyboard_backlight",
10510 .maxBrightness = 255,
10511 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10512 InputLightClass::KEYBOARD_BACKLIGHT,
10513 .path = ""};
10514 RawLightInfo infoGreen = {.id = 2,
10515 .name = "green_keyboard_backlight",
10516 .maxBrightness = 255,
10517 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10518 InputLightClass::KEYBOARD_BACKLIGHT,
10519 .path = ""};
10520 RawLightInfo infoBlue = {.id = 3,
10521 .name = "blue_keyboard_backlight",
10522 .maxBrightness = 255,
10523 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10524 InputLightClass::KEYBOARD_BACKLIGHT,
10525 .path = ""};
10526 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10527 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10528 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10529
10530 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10531 InputDeviceInfo info;
10532 controller.populateDeviceInfo(&info);
10533 std::vector<InputDeviceLightInfo> lights = info.getLights();
10534 ASSERT_EQ(1U, lights.size());
10535 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10536 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10537 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10538
10539 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10540 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10541}
10542
10543TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10544 RawLightInfo infoRed = {.id = 1,
10545 .name = "red",
10546 .maxBrightness = 255,
10547 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10548 .path = ""};
10549 RawLightInfo infoGreen = {.id = 2,
10550 .name = "green",
10551 .maxBrightness = 255,
10552 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10553 .path = ""};
10554 RawLightInfo infoBlue = {.id = 3,
10555 .name = "blue",
10556 .maxBrightness = 255,
10557 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10558 .path = ""};
10559 RawLightInfo infoGlobal = {.id = 3,
10560 .name = "global_keyboard_backlight",
10561 .maxBrightness = 255,
10562 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10563 InputLightClass::KEYBOARD_BACKLIGHT,
10564 .path = ""};
10565 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10566 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10567 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10568 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10569
10570 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10571 InputDeviceInfo info;
10572 controller.populateDeviceInfo(&info);
10573 std::vector<InputDeviceLightInfo> lights = info.getLights();
10574 ASSERT_EQ(1U, lights.size());
10575 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10576 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10577 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010578
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010579 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10580 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010581}
10582
10583TEST_F(LightControllerTest, MultiColorRGBLight) {
10584 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010585 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010586 .maxBrightness = 255,
10587 .flags = InputLightClass::BRIGHTNESS |
10588 InputLightClass::MULTI_INTENSITY |
10589 InputLightClass::MULTI_INDEX,
10590 .path = ""};
10591
10592 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10593
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010594 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010595 InputDeviceInfo info;
10596 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010597 std::vector<InputDeviceLightInfo> lights = info.getLights();
10598 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010599 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10600 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10601 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10602
10603 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10604 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10605}
10606
10607TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10608 RawLightInfo infoColor = {.id = 1,
10609 .name = "multi_color_keyboard_backlight",
10610 .maxBrightness = 255,
10611 .flags = InputLightClass::BRIGHTNESS |
10612 InputLightClass::MULTI_INTENSITY |
10613 InputLightClass::MULTI_INDEX |
10614 InputLightClass::KEYBOARD_BACKLIGHT,
10615 .path = ""};
10616
10617 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10618
10619 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10620 InputDeviceInfo info;
10621 controller.populateDeviceInfo(&info);
10622 std::vector<InputDeviceLightInfo> lights = info.getLights();
10623 ASSERT_EQ(1U, lights.size());
10624 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10625 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10626 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010627
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010628 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10629 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010630}
10631
10632TEST_F(LightControllerTest, PlayerIdLight) {
10633 RawLightInfo info1 = {.id = 1,
10634 .name = "player1",
10635 .maxBrightness = 255,
10636 .flags = InputLightClass::BRIGHTNESS,
10637 .path = ""};
10638 RawLightInfo info2 = {.id = 2,
10639 .name = "player2",
10640 .maxBrightness = 255,
10641 .flags = InputLightClass::BRIGHTNESS,
10642 .path = ""};
10643 RawLightInfo info3 = {.id = 3,
10644 .name = "player3",
10645 .maxBrightness = 255,
10646 .flags = InputLightClass::BRIGHTNESS,
10647 .path = ""};
10648 RawLightInfo info4 = {.id = 4,
10649 .name = "player4",
10650 .maxBrightness = 255,
10651 .flags = InputLightClass::BRIGHTNESS,
10652 .path = ""};
10653 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10654 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10655 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10656 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10657
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010658 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010659 InputDeviceInfo info;
10660 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010661 std::vector<InputDeviceLightInfo> lights = info.getLights();
10662 ASSERT_EQ(1U, lights.size());
10663 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010664 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10665 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010666
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010667 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10668 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10669 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010670}
10671
Michael Wrightd02c5b62014-02-10 15:10:22 -080010672} // namespace android