blob: e51379c38f54d378af84f577beed6f636bd6f80c [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;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002785
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002786 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002787 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002788 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002789 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002790 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002791
Chris Yea52ade12020-08-27 16:49:20 -07002792 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002793 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002794 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002795 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002796 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002797 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002798 InputDeviceIdentifier identifier;
2799 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002800 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002801 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002802 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002803 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002804 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002805 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002806 }
2807
Chris Yea52ade12020-08-27 16:49:20 -07002808 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002809 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002810 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002811 }
2812};
2813
2814const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002815const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002816const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002817const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2818const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002819const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002820 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002821const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002822
2823TEST_F(InputDeviceTest, ImmutableProperties) {
2824 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002825 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002826 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002827}
2828
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002829TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
2830 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
2831
2832 // Configuration
2833 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2834 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002835 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002836
2837 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
2838}
2839
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002840TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2841 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002842}
2843
Michael Wrightd02c5b62014-02-10 15:10:22 -08002844TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2845 // Configuration.
2846 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002847 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002848
2849 // Reset.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002850 unused += mDevice->reset(ARBITRARY_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002851
2852 NotifyDeviceResetArgs resetArgs;
2853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2854 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2855 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2856
2857 // Metadata.
2858 ASSERT_TRUE(mDevice->isIgnored());
2859 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2860
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002861 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002862 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002863 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002864 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2865 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2866
2867 // State queries.
2868 ASSERT_EQ(0, mDevice->getMetaState());
2869
2870 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2871 << "Ignored device should return unknown key code state.";
2872 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2873 << "Ignored device should return unknown scan code state.";
2874 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2875 << "Ignored device should return unknown switch state.";
2876
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002877 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002878 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002879 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002880 << "Ignored device should never mark any key codes.";
2881 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2882 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2883}
2884
2885TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2886 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002887 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08002888
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002889 FakeInputMapper& mapper1 =
2890 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002891 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2892 mapper1.setMetaState(AMETA_ALT_ON);
2893 mapper1.addSupportedKeyCode(AKEYCODE_A);
2894 mapper1.addSupportedKeyCode(AKEYCODE_B);
2895 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2896 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2897 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2898 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2899 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002900
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002901 FakeInputMapper& mapper2 =
2902 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002903 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002904
2905 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002906 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002907
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002908 std::string propertyValue;
2909 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002910 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002911 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002912
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002913 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2914 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002915
2916 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002917 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002918 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2919 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002920
2921 NotifyDeviceResetArgs resetArgs;
2922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2923 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2924 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2925
2926 // Metadata.
2927 ASSERT_FALSE(mDevice->isIgnored());
2928 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2929
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002930 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002931 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002932 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002933 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2934 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2935
2936 // State queries.
2937 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2938 << "Should query mappers and combine meta states.";
2939
2940 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2941 << "Should return unknown key code state when source not supported.";
2942 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2943 << "Should return unknown scan code state when source not supported.";
2944 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2945 << "Should return unknown switch state when source not supported.";
2946
2947 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2948 << "Should query mapper when source is supported.";
2949 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2950 << "Should query mapper when source is supported.";
2951 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2952 << "Should query mapper when source is supported.";
2953
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002954 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002955 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002956 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002957 << "Should do nothing when source is unsupported.";
2958 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2959 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2960 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2961 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2962
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002963 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002964 << "Should query mapper when source is supported.";
2965 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2966 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2967 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2968 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2969
2970 // Event handling.
2971 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002972 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002973 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002974
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002975 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2976 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002977}
2978
Arthur Hung2c9a3342019-07-23 14:18:59 +08002979// A single input device is associated with a specific display. Check that:
2980// 1. Device is disabled if the viewport corresponding to the associated display is not found
2981// 2. Device is disabled when setEnabled API is called
2982TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002983 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002984
2985 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002986 std::list<NotifyArgs> unused =
2987 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002988
2989 // Device should be enabled by default.
2990 ASSERT_TRUE(mDevice->isEnabled());
2991
2992 // Prepare associated info.
2993 constexpr uint8_t hdmi = 1;
2994 const std::string UNIQUE_ID = "local:1";
2995
2996 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002997 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2998 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002999 // Device should be disabled because it is associated with a specific display via
3000 // input port <-> display port association, but the corresponding display is not found
3001 ASSERT_FALSE(mDevice->isEnabled());
3002
3003 // Prepare displays.
3004 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003005 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
3006 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003007 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3008 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003009 ASSERT_TRUE(mDevice->isEnabled());
3010
3011 // Device should be disabled after set disable.
3012 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003013 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3014 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003015 ASSERT_FALSE(mDevice->isEnabled());
3016
3017 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003018 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3019 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003020 ASSERT_FALSE(mDevice->isEnabled());
3021}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003022
Christine Franks1ba71cc2021-04-07 14:37:42 -07003023TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
3024 // Device should be enabled by default.
3025 mFakePolicy->clearViewports();
3026 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003027 std::list<NotifyArgs> unused =
3028 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003029 ASSERT_TRUE(mDevice->isEnabled());
3030
3031 // Device should be disabled because it is associated with a specific display, but the
3032 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08003033 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003034 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3035 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003036 ASSERT_FALSE(mDevice->isEnabled());
3037
3038 // Device should be enabled when a display is found.
3039 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3040 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
3041 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003042 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3043 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003044 ASSERT_TRUE(mDevice->isEnabled());
3045
3046 // Device should be disabled after set disable.
3047 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003048 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3049 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003050 ASSERT_FALSE(mDevice->isEnabled());
3051
3052 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003053 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3054 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003055 ASSERT_FALSE(mDevice->isEnabled());
3056}
3057
Christine Franks2a2293c2022-01-18 11:51:16 -08003058TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
3059 mFakePolicy->clearViewports();
3060 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003061 std::list<NotifyArgs> unused =
3062 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks2a2293c2022-01-18 11:51:16 -08003063
Christine Franks2a2293c2022-01-18 11:51:16 -08003064 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
3065 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3066 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
3067 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003068 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3069 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08003070 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
3071}
3072
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07003073/**
3074 * This test reproduces a crash caused by a dangling reference that remains after device is added
3075 * and removed. The reference is accessed in InputDevice::dump(..);
3076 */
3077TEST_F(InputDeviceTest, DumpDoesNotCrash) {
3078 constexpr int32_t TEST_EVENTHUB_ID = 10;
3079 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
3080
3081 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
3082 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
3083 device.removeEventHubDevice(TEST_EVENTHUB_ID);
3084 std::string dumpStr, eventHubDevStr;
3085 device.dump(dumpStr, eventHubDevStr);
3086}
3087
Michael Wrightd02c5b62014-02-10 15:10:22 -08003088// --- InputMapperTest ---
3089
3090class InputMapperTest : public testing::Test {
3091protected:
3092 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003093 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003094 static const int32_t DEVICE_ID;
3095 static const int32_t DEVICE_GENERATION;
3096 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003097 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003098 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003099
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003100 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003101 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003102 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08003103 std::unique_ptr<InstrumentedInputReader> mReader;
3104 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003105
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003106 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003107 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003108 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003109 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08003110 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003111 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08003112 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003113 // Consume the device reset notification generated when adding a new device.
3114 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003115 }
3116
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003117 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003118 SetUp(DEVICE_CLASSES);
3119 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003120
Chris Yea52ade12020-08-27 16:49:20 -07003121 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003122 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003123 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003124 }
3125
3126 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003127 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003128 }
3129
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003130 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00003131 if (!changes ||
3132 (changes &
3133 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
3134 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003135 mReader->requestRefreshConfiguration(changes);
3136 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08003137 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003138 std::list<NotifyArgs> out =
3139 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003140 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003141 for (const NotifyArgs& args : out) {
3142 mFakeListener->notify(args);
3143 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003144 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003145 return out;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003146 }
3147
arthurhungdcef2dc2020-08-11 14:47:50 +08003148 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
3149 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003150 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003151 InputDeviceIdentifier identifier;
3152 identifier.name = name;
3153 identifier.location = location;
3154 std::shared_ptr<InputDevice> device =
3155 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
3156 identifier);
3157 mReader->pushNextDevice(device);
3158 mFakeEventHub->addDevice(eventHubId, name, classes);
3159 mReader->loopOnce();
3160 return device;
3161 }
3162
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003163 template <class T, typename... Args>
3164 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003165 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003166 configureDevice(0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003167 std::list<NotifyArgs> resetArgList = mDevice->reset(ARBITRARY_TIME);
3168 resetArgList += mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003169 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003170 for (const NotifyArgs& loopArgs : resetArgList) {
3171 mFakeListener->notify(loopArgs);
3172 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003173 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003174 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003175 }
3176
3177 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003178 int32_t orientation, const std::string& uniqueId,
3179 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003180 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3181 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003182 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3183 }
3184
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003185 void clearViewports() {
3186 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003187 }
3188
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003189 std::list<NotifyArgs> process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type,
3190 int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003191 RawEvent event;
3192 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003193 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003194 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003195 event.type = type;
3196 event.code = code;
3197 event.value = value;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003198 std::list<NotifyArgs> processArgList = mapper.process(&event);
3199 for (const NotifyArgs& args : processArgList) {
3200 mFakeListener->notify(args);
3201 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003202 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003203 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003204 return processArgList;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003205 }
3206
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00003207 void resetMapper(InputMapper& mapper, nsecs_t when) {
3208 const auto resetArgs = mapper.reset(when);
3209 for (const auto args : resetArgs) {
3210 mFakeListener->notify(args);
3211 }
3212 // Loop the reader to flush the input listener queue.
3213 mReader->loopOnce();
3214 }
3215
Michael Wrightd02c5b62014-02-10 15:10:22 -08003216 static void assertMotionRange(const InputDeviceInfo& info,
3217 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3218 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003219 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003220 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3221 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3222 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3223 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3224 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3225 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3226 }
3227
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003228 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3229 float size, float touchMajor, float touchMinor, float toolMajor,
3230 float toolMinor, float orientation, float distance,
3231 float scaledAxisEpsilon = 1.f) {
3232 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3233 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003234 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3235 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003236 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3237 scaledAxisEpsilon);
3238 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3239 scaledAxisEpsilon);
3240 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3241 scaledAxisEpsilon);
3242 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3243 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003244 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3245 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3246 }
3247
Michael Wright17db18e2020-06-26 20:51:44 +01003248 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003249 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003250 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003251 ASSERT_NEAR(x, actualX, 1);
3252 ASSERT_NEAR(y, actualY, 1);
3253 }
3254};
3255
3256const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003257const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003258const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003259const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3260const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003261const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3262 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003263const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003264
3265// --- SwitchInputMapperTest ---
3266
3267class SwitchInputMapperTest : public InputMapperTest {
3268protected:
3269};
3270
3271TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003272 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003274 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003275}
3276
3277TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003278 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003279
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003280 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003281 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003283 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003284 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285}
3286
3287TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003288 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003289 std::list<NotifyArgs> out;
3290 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3291 ASSERT_TRUE(out.empty());
3292 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3293 ASSERT_TRUE(out.empty());
3294 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3295 ASSERT_TRUE(out.empty());
3296 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003297
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003298 ASSERT_EQ(1u, out.size());
3299 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003300 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003301 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3302 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003303 args.switchMask);
3304 ASSERT_EQ(uint32_t(0), args.policyFlags);
3305}
3306
Chris Ye87143712020-11-10 05:05:58 +00003307// --- VibratorInputMapperTest ---
3308class VibratorInputMapperTest : public InputMapperTest {
3309protected:
3310 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3311};
3312
3313TEST_F(VibratorInputMapperTest, GetSources) {
3314 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3315
3316 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3317}
3318
3319TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3320 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3321
3322 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3323}
3324
3325TEST_F(VibratorInputMapperTest, Vibrate) {
3326 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003327 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003328 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3329
3330 VibrationElement pattern(2);
3331 VibrationSequence sequence(2);
3332 pattern.duration = std::chrono::milliseconds(200);
3333 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3334 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3335 sequence.addElement(pattern);
3336 pattern.duration = std::chrono::milliseconds(500);
3337 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3338 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3339 sequence.addElement(pattern);
3340
3341 std::vector<int64_t> timings = {0, 1};
3342 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3343
3344 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003345 // Start vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003346 std::list<NotifyArgs> out = mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003347 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003348 // Verify vibrator state listener was notified.
3349 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003350 ASSERT_EQ(1u, out.size());
3351 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3352 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
3353 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08003354 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003355 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08003356 ASSERT_FALSE(mapper.isVibrating());
3357 // 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& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3361 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
3362 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003363}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003364
Chris Yef59a2f42020-10-16 12:55:26 -07003365// --- SensorInputMapperTest ---
3366
3367class SensorInputMapperTest : public InputMapperTest {
3368protected:
3369 static const int32_t ACCEL_RAW_MIN;
3370 static const int32_t ACCEL_RAW_MAX;
3371 static const int32_t ACCEL_RAW_FUZZ;
3372 static const int32_t ACCEL_RAW_FLAT;
3373 static const int32_t ACCEL_RAW_RESOLUTION;
3374
3375 static const int32_t GYRO_RAW_MIN;
3376 static const int32_t GYRO_RAW_MAX;
3377 static const int32_t GYRO_RAW_FUZZ;
3378 static const int32_t GYRO_RAW_FLAT;
3379 static const int32_t GYRO_RAW_RESOLUTION;
3380
3381 static const float GRAVITY_MS2_UNIT;
3382 static const float DEGREE_RADIAN_UNIT;
3383
3384 void prepareAccelAxes();
3385 void prepareGyroAxes();
3386 void setAccelProperties();
3387 void setGyroProperties();
3388 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3389};
3390
3391const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3392const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3393const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3394const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3395const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3396
3397const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3398const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3399const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3400const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3401const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3402
3403const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3404const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3405
3406void SensorInputMapperTest::prepareAccelAxes() {
3407 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3408 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3409 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3410 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3411 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3412 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3413}
3414
3415void SensorInputMapperTest::prepareGyroAxes() {
3416 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3417 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3418 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3419 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3420 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3421 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3422}
3423
3424void SensorInputMapperTest::setAccelProperties() {
3425 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3426 /* sensorDataIndex */ 0);
3427 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3428 /* sensorDataIndex */ 1);
3429 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3430 /* sensorDataIndex */ 2);
3431 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3432 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3433 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3434 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3435 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3436}
3437
3438void SensorInputMapperTest::setGyroProperties() {
3439 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3440 /* sensorDataIndex */ 0);
3441 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3442 /* sensorDataIndex */ 1);
3443 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3444 /* sensorDataIndex */ 2);
3445 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3446 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3447 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3448 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3449 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3450}
3451
3452TEST_F(SensorInputMapperTest, GetSources) {
3453 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3454
3455 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3456}
3457
3458TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3459 setAccelProperties();
3460 prepareAccelAxes();
3461 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3462
3463 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3464 std::chrono::microseconds(10000),
3465 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003466 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003467 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3468 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3469 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3470 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3471 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003472
3473 NotifySensorArgs args;
3474 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3475 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3476 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3477
3478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3479 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3480 ASSERT_EQ(args.deviceId, DEVICE_ID);
3481 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3482 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3483 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3484 ASSERT_EQ(args.values, values);
3485 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3486}
3487
3488TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3489 setGyroProperties();
3490 prepareGyroAxes();
3491 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3492
3493 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3494 std::chrono::microseconds(10000),
3495 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003496 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003497 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3498 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3499 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3500 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3501 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003502
3503 NotifySensorArgs args;
3504 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3505 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3506 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3507
3508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3509 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3510 ASSERT_EQ(args.deviceId, DEVICE_ID);
3511 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3512 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3513 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3514 ASSERT_EQ(args.values, values);
3515 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3516}
3517
Michael Wrightd02c5b62014-02-10 15:10:22 -08003518// --- KeyboardInputMapperTest ---
3519
3520class KeyboardInputMapperTest : public InputMapperTest {
3521protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003522 const std::string UNIQUE_ID = "local:0";
3523
3524 void prepareDisplay(int32_t orientation);
3525
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003526 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003527 int32_t originalKeyCode, int32_t rotatedKeyCode,
3528 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003529};
3530
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003531/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3532 * orientation.
3533 */
3534void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003535 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3536 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003537}
3538
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003539void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003540 int32_t originalScanCode, int32_t originalKeyCode,
3541 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003542 NotifyKeyArgs args;
3543
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003544 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3546 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3547 ASSERT_EQ(originalScanCode, args.scanCode);
3548 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003549 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003550
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003551 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3553 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3554 ASSERT_EQ(originalScanCode, args.scanCode);
3555 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003556 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003557}
3558
Michael Wrightd02c5b62014-02-10 15:10:22 -08003559TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003560 KeyboardInputMapper& mapper =
3561 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3562 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003563
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003564 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003565}
3566
3567TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3568 const int32_t USAGE_A = 0x070004;
3569 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003570 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3571 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003572 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3573 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3574 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003575
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003576 KeyboardInputMapper& mapper =
3577 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3578 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003579 // Initial metastate is AMETA_NONE.
3580 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003581
3582 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003583 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003584 NotifyKeyArgs args;
3585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3586 ASSERT_EQ(DEVICE_ID, args.deviceId);
3587 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3588 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3589 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3590 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3591 ASSERT_EQ(KEY_HOME, args.scanCode);
3592 ASSERT_EQ(AMETA_NONE, args.metaState);
3593 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3594 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3595 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3596
3597 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003598 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3600 ASSERT_EQ(DEVICE_ID, args.deviceId);
3601 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3602 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3603 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3604 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3605 ASSERT_EQ(KEY_HOME, args.scanCode);
3606 ASSERT_EQ(AMETA_NONE, args.metaState);
3607 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3608 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3609 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3610
3611 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003612 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3613 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3615 ASSERT_EQ(DEVICE_ID, args.deviceId);
3616 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3617 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3618 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3619 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3620 ASSERT_EQ(0, args.scanCode);
3621 ASSERT_EQ(AMETA_NONE, args.metaState);
3622 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3623 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3624 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3625
3626 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003627 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3628 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3630 ASSERT_EQ(DEVICE_ID, args.deviceId);
3631 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3632 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3633 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3634 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3635 ASSERT_EQ(0, args.scanCode);
3636 ASSERT_EQ(AMETA_NONE, args.metaState);
3637 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3638 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3639 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3640
3641 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003642 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3643 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3645 ASSERT_EQ(DEVICE_ID, args.deviceId);
3646 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3647 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3648 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3649 ASSERT_EQ(0, args.keyCode);
3650 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3651 ASSERT_EQ(AMETA_NONE, args.metaState);
3652 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3653 ASSERT_EQ(0U, args.policyFlags);
3654 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3655
3656 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003657 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3658 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3660 ASSERT_EQ(DEVICE_ID, args.deviceId);
3661 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3662 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3663 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3664 ASSERT_EQ(0, args.keyCode);
3665 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3666 ASSERT_EQ(AMETA_NONE, args.metaState);
3667 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3668 ASSERT_EQ(0U, args.policyFlags);
3669 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3670}
3671
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003672/**
3673 * Ensure that the readTime is set to the time when the EV_KEY is received.
3674 */
3675TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3676 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3677
3678 KeyboardInputMapper& mapper =
3679 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3680 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3681 NotifyKeyArgs args;
3682
3683 // Key down
3684 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3686 ASSERT_EQ(12, args.readTime);
3687
3688 // Key up
3689 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3691 ASSERT_EQ(15, args.readTime);
3692}
3693
Michael Wrightd02c5b62014-02-10 15:10:22 -08003694TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003695 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3696 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003697 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3698 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3699 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003700
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003701 KeyboardInputMapper& mapper =
3702 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3703 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003704
Arthur Hung95f68612022-04-07 14:08:22 +08003705 // Initial metastate is AMETA_NONE.
3706 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003707
3708 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003709 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003710 NotifyKeyArgs args;
3711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3712 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003713 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003714 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003715
3716 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003717 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3719 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003720 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003721
3722 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003723 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3725 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003726 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003727
3728 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003729 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3731 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003732 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003733 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003734}
3735
3736TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003737 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3738 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3739 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3740 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003741
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003742 KeyboardInputMapper& mapper =
3743 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3744 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003745
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003746 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003747 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3748 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3749 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3750 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3751 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3752 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3753 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3754 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3755}
3756
3757TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003758 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3759 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3760 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3761 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003762
Michael Wrightd02c5b62014-02-10 15:10:22 -08003763 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003764 KeyboardInputMapper& mapper =
3765 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3766 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003767
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003768 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003769 ASSERT_NO_FATAL_FAILURE(
3770 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3771 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3772 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3773 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3774 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3775 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3776 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003777
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003778 clearViewports();
3779 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003780 ASSERT_NO_FATAL_FAILURE(
3781 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3782 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3783 AKEYCODE_DPAD_UP, DISPLAY_ID));
3784 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3785 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3786 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3787 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003788
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003789 clearViewports();
3790 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003791 ASSERT_NO_FATAL_FAILURE(
3792 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3793 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3794 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3795 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3796 AKEYCODE_DPAD_UP, DISPLAY_ID));
3797 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3798 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003799
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003800 clearViewports();
3801 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003802 ASSERT_NO_FATAL_FAILURE(
3803 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3804 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3805 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3806 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3807 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3808 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3809 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003810
3811 // Special case: if orientation changes while key is down, we still emit the same keycode
3812 // in the key up as we did in the key down.
3813 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003814 clearViewports();
3815 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003816 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3818 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3819 ASSERT_EQ(KEY_UP, args.scanCode);
3820 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3821
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003822 clearViewports();
3823 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003824 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3826 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3827 ASSERT_EQ(KEY_UP, args.scanCode);
3828 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3829}
3830
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003831TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3832 // If the keyboard is not orientation aware,
3833 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003834 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003835
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003836 KeyboardInputMapper& mapper =
3837 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3838 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003839 NotifyKeyArgs args;
3840
3841 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003842 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003844 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3846 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3847
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003848 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003849 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003851 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3853 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3854}
3855
3856TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3857 // If the keyboard is orientation aware,
3858 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003859 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003860
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003861 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003862 KeyboardInputMapper& mapper =
3863 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3864 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003865 NotifyKeyArgs args;
3866
3867 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3868 // ^--- already checked by the previous test
3869
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003870 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003871 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003872 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003874 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3876 ASSERT_EQ(DISPLAY_ID, args.displayId);
3877
3878 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003879 clearViewports();
3880 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003881 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003882 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003884 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3886 ASSERT_EQ(newDisplayId, args.displayId);
3887}
3888
Michael Wrightd02c5b62014-02-10 15:10:22 -08003889TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003890 KeyboardInputMapper& mapper =
3891 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3892 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003893
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003894 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003895 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003896
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003897 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003898 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003899}
3900
Philip Junker4af3b3d2021-12-14 10:36:55 +01003901TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3902 KeyboardInputMapper& mapper =
3903 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3904 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3905
3906 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3907 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3908 << "If a mapping is available, the result is equal to the mapping";
3909
3910 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3911 << "If no mapping is available, the result is the key location";
3912}
3913
Michael Wrightd02c5b62014-02-10 15:10:22 -08003914TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003915 KeyboardInputMapper& mapper =
3916 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3917 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003918
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003919 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003920 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003921
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003922 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003923 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003924}
3925
3926TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003927 KeyboardInputMapper& mapper =
3928 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3929 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003930
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003931 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003932
Michael Wrightd02c5b62014-02-10 15:10:22 -08003933 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003934 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003935 ASSERT_TRUE(flags[0]);
3936 ASSERT_FALSE(flags[1]);
3937}
3938
3939TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003940 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3941 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3942 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3943 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3944 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3945 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003946
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003947 KeyboardInputMapper& mapper =
3948 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3949 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003950 // Initial metastate is AMETA_NONE.
3951 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003952
3953 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003954 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3955 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3956 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003957
3958 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003959 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3960 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003961 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3962 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3963 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003964 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003965
3966 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003967 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3968 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003969 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3970 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3971 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003972 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003973
3974 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003975 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3976 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003977 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3978 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3979 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003980 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003981
3982 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003983 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3984 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003985 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3986 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3987 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003988 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003989
3990 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003991 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3992 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003993 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3994 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3995 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003996 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003997
3998 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003999 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4000 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004001 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4002 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4003 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004004 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004005}
4006
Chris Yea52ade12020-08-27 16:49:20 -07004007TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
4008 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
4009 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
4010 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
4011 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
4012
4013 KeyboardInputMapper& mapper =
4014 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4015 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4016
Chris Yea52ade12020-08-27 16:49:20 -07004017 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004018 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07004019 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4020 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
4021 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
4022 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4023
4024 NotifyKeyArgs args;
4025 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004026 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07004027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4028 ASSERT_EQ(AMETA_NONE, args.metaState);
4029 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4030 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4031 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
4032
4033 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004034 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07004035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4036 ASSERT_EQ(AMETA_NONE, args.metaState);
4037 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4038 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4039 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
4040}
4041
Arthur Hung2c9a3342019-07-23 14:18:59 +08004042TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
4043 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004044 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4045 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4046 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4047 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004048
4049 // keyboard 2.
4050 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08004051 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08004052 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004053 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08004054 std::shared_ptr<InputDevice> device2 =
4055 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004056 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08004057
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004058 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4059 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4060 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4061 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004062
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004063 KeyboardInputMapper& mapper =
4064 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4065 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004066
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004067 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004068 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004069 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004070 std::list<NotifyArgs> unused =
4071 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4072 0 /*changes*/);
4073 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004074
4075 // Prepared displays and associated info.
4076 constexpr uint8_t hdmi1 = 0;
4077 constexpr uint8_t hdmi2 = 1;
4078 const std::string SECONDARY_UNIQUE_ID = "local:1";
4079
4080 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
4081 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
4082
4083 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004084 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4085 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004086 ASSERT_FALSE(device2->isEnabled());
4087
4088 // Prepare second display.
4089 constexpr int32_t newDisplayId = 2;
4090 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004091 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004092 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004093 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004094 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004095 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4096 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004097
4098 // Device should be enabled after the associated display is found.
4099 ASSERT_TRUE(mDevice->isEnabled());
4100 ASSERT_TRUE(device2->isEnabled());
4101
4102 // Test pad key events
4103 ASSERT_NO_FATAL_FAILURE(
4104 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
4105 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4106 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4107 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4108 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4109 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4110 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4111
4112 ASSERT_NO_FATAL_FAILURE(
4113 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
4114 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4115 AKEYCODE_DPAD_RIGHT, newDisplayId));
4116 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4117 AKEYCODE_DPAD_DOWN, newDisplayId));
4118 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4119 AKEYCODE_DPAD_LEFT, newDisplayId));
4120}
Michael Wrightd02c5b62014-02-10 15:10:22 -08004121
arthurhungc903df12020-08-11 15:08:42 +08004122TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
4123 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4124 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4125 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4126 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4127 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4128 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4129
4130 KeyboardInputMapper& mapper =
4131 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4132 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004133 // Initial metastate is AMETA_NONE.
4134 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004135
4136 // Initialization should have turned all of the lights off.
4137 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4138 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4139 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4140
4141 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004142 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4143 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004144 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4145 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4146
4147 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004148 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4149 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004150 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4151 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
4152
4153 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004154 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4155 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004156 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4157 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
4158
4159 mFakeEventHub->removeDevice(EVENTHUB_ID);
4160 mReader->loopOnce();
4161
4162 // keyboard 2 should default toggle keys.
4163 const std::string USB2 = "USB2";
4164 const std::string DEVICE_NAME2 = "KEYBOARD2";
4165 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4166 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4167 std::shared_ptr<InputDevice> device2 =
4168 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004169 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08004170 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4171 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4172 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4173 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4174 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4175 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4176
arthurhung6fe95782020-10-05 22:41:16 +08004177 KeyboardInputMapper& mapper2 =
4178 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4179 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004180 std::list<NotifyArgs> unused =
4181 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4182 0 /*changes*/);
4183 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08004184
4185 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4186 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4187 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004188 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4189 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004190}
4191
Arthur Hungcb40a002021-08-03 14:31:01 +00004192TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4193 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4194 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4195 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4196
4197 // Suppose we have two mappers. (DPAD + KEYBOARD)
4198 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4199 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4200 KeyboardInputMapper& mapper =
4201 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4202 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004203 // Initial metastate is AMETA_NONE.
4204 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004205
4206 mReader->toggleCapsLockState(DEVICE_ID);
4207 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4208}
4209
Arthur Hungfb3cc112022-04-13 07:39:50 +00004210TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4211 // keyboard 1.
4212 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4213 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4214 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4215 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4216 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4217 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4218
4219 KeyboardInputMapper& mapper1 =
4220 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4221 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4222
4223 // keyboard 2.
4224 const std::string USB2 = "USB2";
4225 const std::string DEVICE_NAME2 = "KEYBOARD2";
4226 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4227 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4228 std::shared_ptr<InputDevice> device2 =
4229 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4230 ftl::Flags<InputDeviceClass>(0));
4231 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4232 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4233 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4234 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4235 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4236 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4237
4238 KeyboardInputMapper& mapper2 =
4239 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4240 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004241 std::list<NotifyArgs> unused =
4242 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4243 0 /*changes*/);
4244 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004245
Arthur Hung95f68612022-04-07 14:08:22 +08004246 // Initial metastate is AMETA_NONE.
4247 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4248 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4249
4250 // Toggle num lock on and off.
4251 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4252 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004253 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4254 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4255 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4256
4257 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4258 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4259 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4260 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4261 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4262
4263 // Toggle caps lock on and off.
4264 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4265 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4266 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4267 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4268 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4269
4270 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4271 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4272 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4273 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4274 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4275
4276 // Toggle scroll lock on and off.
4277 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4278 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4279 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4280 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4281 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4282
4283 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4284 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4285 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4286 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4287 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4288}
4289
Arthur Hung2141d542022-08-23 07:45:21 +00004290TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4291 const int32_t USAGE_A = 0x070004;
4292 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4293 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4294
4295 KeyboardInputMapper& mapper =
4296 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4297 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4298 // Key down by scan code.
4299 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4300 NotifyKeyArgs args;
4301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4302 ASSERT_EQ(DEVICE_ID, args.deviceId);
4303 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4304 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4305 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4306 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4307 ASSERT_EQ(KEY_HOME, args.scanCode);
4308 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4309
4310 // Disable device, it should synthesize cancellation events for down events.
4311 mFakePolicy->addDisabledDevice(DEVICE_ID);
4312 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
4313
4314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4315 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4316 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4317 ASSERT_EQ(KEY_HOME, args.scanCode);
4318 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4319}
4320
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004321// --- KeyboardInputMapperTest_ExternalDevice ---
4322
4323class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4324protected:
Chris Yea52ade12020-08-27 16:49:20 -07004325 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004326};
4327
4328TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004329 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4330 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004331
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004332 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4333 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4334 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4335 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004336
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004337 KeyboardInputMapper& mapper =
4338 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4339 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004340
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004341 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004342 NotifyKeyArgs args;
4343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4344 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4345
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004346 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4348 ASSERT_EQ(uint32_t(0), args.policyFlags);
4349
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004350 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4352 ASSERT_EQ(uint32_t(0), args.policyFlags);
4353
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004354 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4356 ASSERT_EQ(uint32_t(0), args.policyFlags);
4357
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004358 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4360 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4361
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004362 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4364 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4365}
4366
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004367TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004368 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004369
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004370 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4371 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4372 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004373
Powei Fengd041c5d2019-05-03 17:11:33 -07004374 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004375 KeyboardInputMapper& mapper =
4376 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4377 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004378
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004379 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004380 NotifyKeyArgs args;
4381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4382 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4383
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004384 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4386 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4387
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004388 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4390 ASSERT_EQ(uint32_t(0), args.policyFlags);
4391
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004392 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4394 ASSERT_EQ(uint32_t(0), args.policyFlags);
4395
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004396 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4398 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4399
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004400 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4402 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4403}
4404
Michael Wrightd02c5b62014-02-10 15:10:22 -08004405// --- CursorInputMapperTest ---
4406
4407class CursorInputMapperTest : public InputMapperTest {
4408protected:
4409 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4410
Michael Wright17db18e2020-06-26 20:51:44 +01004411 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004412
Chris Yea52ade12020-08-27 16:49:20 -07004413 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004414 InputMapperTest::SetUp();
4415
Michael Wright17db18e2020-06-26 20:51:44 +01004416 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004417 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004418 }
4419
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004420 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4421 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004422
4423 void prepareDisplay(int32_t orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004424 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4425 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4426 }
4427
4428 void prepareSecondaryDisplay() {
4429 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4430 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
4431 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004432 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004433
4434 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4435 float pressure) {
4436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4437 0.0f, 0.0f, 0.0f, EPSILON));
4438 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004439};
4440
4441const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4442
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004443void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4444 int32_t originalY, int32_t rotatedX,
4445 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004446 NotifyMotionArgs args;
4447
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004448 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4452 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004453 ASSERT_NO_FATAL_FAILURE(
4454 assertCursorPointerCoords(args.pointerCoords[0],
4455 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4456 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004457}
4458
4459TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004460 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004461 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004462
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004463 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464}
4465
4466TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004467 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004468 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004470 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004471}
4472
4473TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004474 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004475 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004476
4477 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004478 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479
4480 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004481 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4482 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004483 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4484 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4485
4486 // When the bounds are set, then there should be a valid motion range.
4487 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4488
4489 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004490 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004491
4492 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4493 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4494 1, 800 - 1, 0.0f, 0.0f));
4495 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4496 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4497 2, 480 - 1, 0.0f, 0.0f));
4498 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4499 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4500 0.0f, 1.0f, 0.0f, 0.0f));
4501}
4502
4503TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004504 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004505 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004506
4507 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004508 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004509
4510 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4511 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4512 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4513 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4514 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4515 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4516 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4517 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4518 0.0f, 1.0f, 0.0f, 0.0f));
4519}
4520
4521TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004522 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004523 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004524
arthurhungdcef2dc2020-08-11 14:47:50 +08004525 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004526
4527 NotifyMotionArgs args;
4528
4529 // Button press.
4530 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004531 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4532 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4534 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4535 ASSERT_EQ(DEVICE_ID, args.deviceId);
4536 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4537 ASSERT_EQ(uint32_t(0), args.policyFlags);
4538 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4539 ASSERT_EQ(0, args.flags);
4540 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4541 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4542 ASSERT_EQ(0, args.edgeFlags);
4543 ASSERT_EQ(uint32_t(1), args.pointerCount);
4544 ASSERT_EQ(0, args.pointerProperties[0].id);
4545 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004546 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004547 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4548 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4549 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4550
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4552 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4553 ASSERT_EQ(DEVICE_ID, args.deviceId);
4554 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4555 ASSERT_EQ(uint32_t(0), args.policyFlags);
4556 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4557 ASSERT_EQ(0, args.flags);
4558 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4559 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4560 ASSERT_EQ(0, args.edgeFlags);
4561 ASSERT_EQ(uint32_t(1), args.pointerCount);
4562 ASSERT_EQ(0, args.pointerProperties[0].id);
4563 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004564 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004565 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4566 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4567 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4568
Michael Wrightd02c5b62014-02-10 15:10:22 -08004569 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004570 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4571 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4573 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4574 ASSERT_EQ(DEVICE_ID, args.deviceId);
4575 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4576 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004577 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4578 ASSERT_EQ(0, args.flags);
4579 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4580 ASSERT_EQ(0, args.buttonState);
4581 ASSERT_EQ(0, args.edgeFlags);
4582 ASSERT_EQ(uint32_t(1), args.pointerCount);
4583 ASSERT_EQ(0, args.pointerProperties[0].id);
4584 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004585 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004586 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4587 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4588 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4589
4590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4591 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4592 ASSERT_EQ(DEVICE_ID, args.deviceId);
4593 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4594 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004595 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4596 ASSERT_EQ(0, args.flags);
4597 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4598 ASSERT_EQ(0, args.buttonState);
4599 ASSERT_EQ(0, args.edgeFlags);
4600 ASSERT_EQ(uint32_t(1), args.pointerCount);
4601 ASSERT_EQ(0, args.pointerProperties[0].id);
4602 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004603 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004604 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4605 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4606 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4607}
4608
4609TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004610 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004611 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004612
4613 NotifyMotionArgs args;
4614
4615 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004616 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4617 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4619 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004620 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4621 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4622 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004623
4624 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004625 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
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], 0.0f,
4630 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004631}
4632
4633TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004634 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004635 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004636
4637 NotifyMotionArgs args;
4638
4639 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004640 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4641 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4643 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004644 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004645
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4647 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004648 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004649
Michael Wrightd02c5b62014-02-10 15:10:22 -08004650 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004651 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4652 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004654 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004655 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004656
4657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004658 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004659 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004660}
4661
4662TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004663 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004664 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004665
4666 NotifyMotionArgs args;
4667
4668 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004669 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4670 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4671 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4672 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4674 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004675 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4676 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4677 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004678
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4680 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004681 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4682 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4683 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004684
Michael Wrightd02c5b62014-02-10 15:10:22 -08004685 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4687 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4688 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4690 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004691 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4692 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4693 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004694
4695 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004696 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004699 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004700 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004701
4702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004703 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004704 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004705}
4706
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004707TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004708 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004709 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004710 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4711 // need to be rotated.
4712 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004713 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004714
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004715 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004716 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4717 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4718 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4719 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4720 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4721 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4722 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4723 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4724}
4725
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004726TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004727 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004728 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004729 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4730 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004731 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004732
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004733 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004734 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004735 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4736 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4737 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4738 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4739 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4740 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4741 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4742 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4743
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004744 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004745 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004746 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4747 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4748 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4749 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4750 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4751 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4752 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4753 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004754
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004755 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004756 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004757 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4758 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4759 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4760 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4761 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4762 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4763 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4764 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4765
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004766 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004767 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004768 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4769 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4770 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4771 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4772 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4773 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4774 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4775 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004776}
4777
4778TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004779 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004780 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004781
4782 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4783 mFakePointerController->setPosition(100, 200);
4784 mFakePointerController->setButtonState(0);
4785
4786 NotifyMotionArgs motionArgs;
4787 NotifyKeyArgs keyArgs;
4788
4789 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004790 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4791 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4793 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4794 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4795 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004796 ASSERT_NO_FATAL_FAILURE(
4797 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004798
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4800 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4801 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4802 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004803 ASSERT_NO_FATAL_FAILURE(
4804 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004805
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004806 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004809 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004810 ASSERT_EQ(0, motionArgs.buttonState);
4811 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004812 ASSERT_NO_FATAL_FAILURE(
4813 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004814
4815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004816 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004817 ASSERT_EQ(0, motionArgs.buttonState);
4818 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004819 ASSERT_NO_FATAL_FAILURE(
4820 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004821
4822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004823 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004824 ASSERT_EQ(0, motionArgs.buttonState);
4825 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004826 ASSERT_NO_FATAL_FAILURE(
4827 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004828
4829 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4834 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4835 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4836 motionArgs.buttonState);
4837 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4838 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004839 ASSERT_NO_FATAL_FAILURE(
4840 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004841
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4843 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4844 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4845 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4846 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004847 ASSERT_NO_FATAL_FAILURE(
4848 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004849
4850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4851 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4852 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4853 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
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004859 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004862 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004863 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4864 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, 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
4868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004869 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004870 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4871 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004872 ASSERT_NO_FATAL_FAILURE(
4873 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004874
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004875 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4876 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004878 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4879 ASSERT_EQ(0, motionArgs.buttonState);
4880 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004881 ASSERT_NO_FATAL_FAILURE(
4882 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004883 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4884 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004885
4886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004887 ASSERT_EQ(0, motionArgs.buttonState);
4888 ASSERT_EQ(0, mFakePointerController->getButtonState());
4889 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004890 ASSERT_NO_FATAL_FAILURE(
4891 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004892
Michael Wrightd02c5b62014-02-10 15:10:22 -08004893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4894 ASSERT_EQ(0, motionArgs.buttonState);
4895 ASSERT_EQ(0, mFakePointerController->getButtonState());
4896 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004897 ASSERT_NO_FATAL_FAILURE(
4898 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004899
4900 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004901 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4902 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4904 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4905 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004906
Michael Wrightd02c5b62014-02-10 15:10:22 -08004907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004908 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004909 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4910 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004911 ASSERT_NO_FATAL_FAILURE(
4912 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004913
4914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4915 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4916 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4917 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004918 ASSERT_NO_FATAL_FAILURE(
4919 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004920
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004921 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4922 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004924 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004925 ASSERT_EQ(0, motionArgs.buttonState);
4926 ASSERT_EQ(0, 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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004929
4930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004931 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004932 ASSERT_EQ(0, motionArgs.buttonState);
4933 ASSERT_EQ(0, mFakePointerController->getButtonState());
4934
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004935 ASSERT_NO_FATAL_FAILURE(
4936 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4938 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4939 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4940
4941 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004942 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4943 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4945 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4946 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004947
Michael Wrightd02c5b62014-02-10 15:10:22 -08004948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004949 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004950 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4951 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004952 ASSERT_NO_FATAL_FAILURE(
4953 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004954
4955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4956 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4957 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4958 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004959 ASSERT_NO_FATAL_FAILURE(
4960 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004961
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004962 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4963 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004965 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966 ASSERT_EQ(0, motionArgs.buttonState);
4967 ASSERT_EQ(0, 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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004970
4971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4972 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4973 ASSERT_EQ(0, motionArgs.buttonState);
4974 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004975 ASSERT_NO_FATAL_FAILURE(
4976 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004977
Michael Wrightd02c5b62014-02-10 15:10:22 -08004978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4979 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4980 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4981
4982 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004983 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4984 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4986 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4987 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004988
Michael Wrightd02c5b62014-02-10 15:10:22 -08004989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004990 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004991 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4992 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004993 ASSERT_NO_FATAL_FAILURE(
4994 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004995
4996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4997 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4998 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4999 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005000 ASSERT_NO_FATAL_FAILURE(
5001 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005002
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005003 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
5004 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005006 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005007 ASSERT_EQ(0, motionArgs.buttonState);
5008 ASSERT_EQ(0, 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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005011
5012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5013 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5014 ASSERT_EQ(0, motionArgs.buttonState);
5015 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005016 ASSERT_NO_FATAL_FAILURE(
5017 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005018
Michael Wrightd02c5b62014-02-10 15:10:22 -08005019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5020 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5021 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5022
5023 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005024 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
5025 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5027 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5028 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005029
Michael Wrightd02c5b62014-02-10 15:10:22 -08005030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005031 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005032 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5033 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005034 ASSERT_NO_FATAL_FAILURE(
5035 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005036
5037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5038 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5039 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5040 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005041 ASSERT_NO_FATAL_FAILURE(
5042 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005043
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005044 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
5045 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005047 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005048 ASSERT_EQ(0, motionArgs.buttonState);
5049 ASSERT_EQ(0, 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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005052
5053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5054 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5055 ASSERT_EQ(0, motionArgs.buttonState);
5056 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005057 ASSERT_NO_FATAL_FAILURE(
5058 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005059
Michael Wrightd02c5b62014-02-10 15:10:22 -08005060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5061 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5062 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5063}
5064
5065TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005066 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005067 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005068
5069 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5070 mFakePointerController->setPosition(100, 200);
5071 mFakePointerController->setButtonState(0);
5072
5073 NotifyMotionArgs args;
5074
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005075 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5076 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5077 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005079 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5080 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5081 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5082 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 +01005083 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005084}
5085
5086TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005087 addConfigurationProperty("cursor.mode", "pointer");
5088 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005089 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005090
5091 NotifyDeviceResetArgs resetArgs;
5092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5093 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5094 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5095
5096 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5097 mFakePointerController->setPosition(100, 200);
5098 mFakePointerController->setButtonState(0);
5099
5100 NotifyMotionArgs args;
5101
5102 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005103 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5104 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5105 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5107 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5108 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5109 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5110 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 +01005111 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005112
5113 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005114 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5115 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5117 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5118 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5119 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5120 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5122 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5123 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
5124 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5125 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5126
5127 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005128 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5129 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5131 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5132 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
5133 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5134 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5136 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5137 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
5138 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5139 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5140
5141 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005142 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
5143 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
5144 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5146 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5147 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5148 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5149 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 +01005150 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005151
5152 // Disable pointer capture and check that the device generation got bumped
5153 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08005154 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005155 mFakePolicy->setPointerCapture(false);
5156 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08005157 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005158
5159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005160 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5161
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005162 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5166 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005167 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5168 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5169 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 +01005170 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005171}
5172
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00005173/**
5174 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
5175 * pointer acceleration or speed processing should not be applied.
5176 */
5177TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
5178 addConfigurationProperty("cursor.mode", "pointer");
5179 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
5180 100.f /*high threshold*/, 10.f /*acceleration*/);
5181 mFakePolicy->setVelocityControlParams(testParams);
5182 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5183
5184 NotifyDeviceResetArgs resetArgs;
5185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5186 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5187 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5188
5189 NotifyMotionArgs args;
5190
5191 // Move and verify scale is applied.
5192 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5193 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5194 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5196 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5197 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5198 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5199 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5200 ASSERT_GT(relX, 10);
5201 ASSERT_GT(relY, 20);
5202
5203 // Enable Pointer Capture
5204 mFakePolicy->setPointerCapture(true);
5205 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5206 NotifyPointerCaptureChangedArgs captureArgs;
5207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5208 ASSERT_TRUE(captureArgs.request.enable);
5209
5210 // Move and verify scale is not applied.
5211 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5212 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5213 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5215 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5216 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5217 ASSERT_EQ(10, args.pointerCoords[0].getX());
5218 ASSERT_EQ(20, args.pointerCoords[0].getY());
5219}
5220
Prabir Pradhan208360b2022-06-24 18:37:04 +00005221TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5222 addConfigurationProperty("cursor.mode", "pointer");
5223 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5224
5225 NotifyDeviceResetArgs resetArgs;
5226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5227 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5228 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5229
5230 // Ensure the display is rotated.
5231 prepareDisplay(DISPLAY_ORIENTATION_90);
5232
5233 NotifyMotionArgs args;
5234
5235 // Verify that the coordinates are rotated.
5236 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5237 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5238 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5240 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5241 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5242 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5243 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5244
5245 // Enable Pointer Capture.
5246 mFakePolicy->setPointerCapture(true);
5247 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5248 NotifyPointerCaptureChangedArgs captureArgs;
5249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5250 ASSERT_TRUE(captureArgs.request.enable);
5251
5252 // Move and verify rotation is not applied.
5253 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5254 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5255 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5257 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5258 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5259 ASSERT_EQ(10, args.pointerCoords[0].getX());
5260 ASSERT_EQ(20, args.pointerCoords[0].getY());
5261}
5262
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005263TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005264 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005265
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005266 // Set up the default display.
5267 prepareDisplay(DISPLAY_ORIENTATION_90);
5268
5269 // Set up the secondary display as the display on which the pointer should be shown.
5270 // The InputDevice is not associated with any display.
5271 prepareSecondaryDisplay();
5272 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08005273 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5274
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005275 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005276 mFakePointerController->setPosition(100, 200);
5277 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005278
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005279 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005280 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5281 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5282 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005284 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5285 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5286 WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01005287 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005288}
5289
5290TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
5291 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5292
5293 // Set up the default display.
5294 prepareDisplay(DISPLAY_ORIENTATION_90);
5295
5296 // Set up the secondary display as the display on which the pointer should be shown,
5297 // and associate the InputDevice with the secondary display.
5298 prepareSecondaryDisplay();
5299 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5300 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5301 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5302
5303 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5304 mFakePointerController->setPosition(100, 200);
5305 mFakePointerController->setButtonState(0);
5306
5307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5308 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5309 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005311 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5312 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5313 WithCoords(110.0f, 220.0f))));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005314 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
5315}
5316
5317TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
5318 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5319
5320 // Set up the default display as the display on which the pointer should be shown.
5321 prepareDisplay(DISPLAY_ORIENTATION_90);
5322 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5323
5324 // Associate the InputDevice with the secondary display.
5325 prepareSecondaryDisplay();
5326 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5327 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5328
5329 // The mapper should not generate any events because it is associated with a display that is
5330 // different from the pointer display.
5331 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5332 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5333 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005335}
5336
Michael Wrightd02c5b62014-02-10 15:10:22 -08005337// --- TouchInputMapperTest ---
5338
5339class TouchInputMapperTest : public InputMapperTest {
5340protected:
5341 static const int32_t RAW_X_MIN;
5342 static const int32_t RAW_X_MAX;
5343 static const int32_t RAW_Y_MIN;
5344 static const int32_t RAW_Y_MAX;
5345 static const int32_t RAW_TOUCH_MIN;
5346 static const int32_t RAW_TOUCH_MAX;
5347 static const int32_t RAW_TOOL_MIN;
5348 static const int32_t RAW_TOOL_MAX;
5349 static const int32_t RAW_PRESSURE_MIN;
5350 static const int32_t RAW_PRESSURE_MAX;
5351 static const int32_t RAW_ORIENTATION_MIN;
5352 static const int32_t RAW_ORIENTATION_MAX;
5353 static const int32_t RAW_DISTANCE_MIN;
5354 static const int32_t RAW_DISTANCE_MAX;
5355 static const int32_t RAW_TILT_MIN;
5356 static const int32_t RAW_TILT_MAX;
5357 static const int32_t RAW_ID_MIN;
5358 static const int32_t RAW_ID_MAX;
5359 static const int32_t RAW_SLOT_MIN;
5360 static const int32_t RAW_SLOT_MAX;
5361 static const float X_PRECISION;
5362 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005363 static const float X_PRECISION_VIRTUAL;
5364 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005365
5366 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005367 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005368
5369 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5370
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005371 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005372 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005373
Michael Wrightd02c5b62014-02-10 15:10:22 -08005374 enum Axes {
5375 POSITION = 1 << 0,
5376 TOUCH = 1 << 1,
5377 TOOL = 1 << 2,
5378 PRESSURE = 1 << 3,
5379 ORIENTATION = 1 << 4,
5380 MINOR = 1 << 5,
5381 ID = 1 << 6,
5382 DISTANCE = 1 << 7,
5383 TILT = 1 << 8,
5384 SLOT = 1 << 9,
5385 TOOL_TYPE = 1 << 10,
5386 };
5387
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005388 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5389 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005390 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005391 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005392 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005393 int32_t toRawX(float displayX);
5394 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005395 int32_t toRotatedRawX(float displayX);
5396 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005397 float toCookedX(float rawX, float rawY);
5398 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005399 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005400 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005401 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005402 float toDisplayY(int32_t rawY, int32_t displayHeight);
5403
Michael Wrightd02c5b62014-02-10 15:10:22 -08005404};
5405
5406const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5407const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5408const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5409const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5410const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5411const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5412const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5413const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005414const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5415const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005416const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5417const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5418const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5419const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5420const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5421const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5422const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5423const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5424const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5425const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5426const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5427const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005428const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5429 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5430const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5431 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005432const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5433 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005434
5435const float TouchInputMapperTest::GEOMETRIC_SCALE =
5436 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5437 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5438
5439const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5440 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5441 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5442};
5443
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005444void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005445 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5446 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005447}
5448
5449void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5450 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5451 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005452}
5453
Santos Cordonfa5cf462017-04-05 10:37:00 -07005454void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005455 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5456 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5457 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005458}
5459
Michael Wrightd02c5b62014-02-10 15:10:22 -08005460void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005461 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5462 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5463 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5464 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005465}
5466
Jason Gerecke489fda82012-09-07 17:19:40 -07005467void TouchInputMapperTest::prepareLocationCalibration() {
5468 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5469}
5470
Michael Wrightd02c5b62014-02-10 15:10:22 -08005471int32_t TouchInputMapperTest::toRawX(float displayX) {
5472 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5473}
5474
5475int32_t TouchInputMapperTest::toRawY(float displayY) {
5476 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5477}
5478
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005479int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5480 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5481}
5482
5483int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5484 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5485}
5486
Jason Gerecke489fda82012-09-07 17:19:40 -07005487float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5488 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5489 return rawX;
5490}
5491
5492float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5493 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5494 return rawY;
5495}
5496
Michael Wrightd02c5b62014-02-10 15:10:22 -08005497float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005498 return toDisplayX(rawX, DISPLAY_WIDTH);
5499}
5500
5501float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5502 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005503}
5504
5505float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005506 return toDisplayY(rawY, DISPLAY_HEIGHT);
5507}
5508
5509float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5510 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005511}
5512
5513
5514// --- SingleTouchInputMapperTest ---
5515
5516class SingleTouchInputMapperTest : public TouchInputMapperTest {
5517protected:
5518 void prepareButtons();
5519 void prepareAxes(int axes);
5520
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005521 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5522 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5523 void processUp(SingleTouchInputMapper& mappery);
5524 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5525 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5526 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5527 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5528 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5529 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005530};
5531
5532void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005533 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005534}
5535
5536void SingleTouchInputMapperTest::prepareAxes(int axes) {
5537 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005538 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5539 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005540 }
5541 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005542 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5543 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005544 }
5545 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005546 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5547 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005548 }
5549 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005550 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5551 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005552 }
5553 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005554 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5555 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005556 }
5557}
5558
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005559void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005560 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5561 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5562 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005563}
5564
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005565void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005566 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5567 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005568}
5569
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005570void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005571 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005572}
5573
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005574void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005575 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005576}
5577
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005578void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5579 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005580 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005581}
5582
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005583void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005584 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005585}
5586
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005587void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5588 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005589 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5590 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005591}
5592
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005593void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5594 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005595 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005596}
5597
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005598void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005599 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005600}
5601
Michael Wrightd02c5b62014-02-10 15:10:22 -08005602TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005603 prepareButtons();
5604 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005605 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005606
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005607 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005608}
5609
Michael Wrightd02c5b62014-02-10 15:10:22 -08005610TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005611 prepareButtons();
5612 prepareAxes(POSITION);
5613 addConfigurationProperty("touch.deviceType", "touchScreen");
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_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005617}
5618
5619TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005620 addConfigurationProperty("touch.deviceType", "touchScreen");
5621 prepareDisplay(DISPLAY_ORIENTATION_0);
5622 prepareButtons();
5623 prepareAxes(POSITION);
5624 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005625 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005626
5627 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005628 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005629
5630 // Virtual key is down.
5631 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5632 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5633 processDown(mapper, x, y);
5634 processSync(mapper);
5635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5636
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005637 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005638
5639 // Virtual key is up.
5640 processUp(mapper);
5641 processSync(mapper);
5642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5643
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005644 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005645}
5646
5647TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005648 addConfigurationProperty("touch.deviceType", "touchScreen");
5649 prepareDisplay(DISPLAY_ORIENTATION_0);
5650 prepareButtons();
5651 prepareAxes(POSITION);
5652 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005653 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005654
5655 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005656 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005657
5658 // Virtual key is down.
5659 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5660 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5661 processDown(mapper, x, y);
5662 processSync(mapper);
5663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5664
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005665 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005666
5667 // Virtual key is up.
5668 processUp(mapper);
5669 processSync(mapper);
5670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5671
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005672 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005673}
5674
5675TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005676 addConfigurationProperty("touch.deviceType", "touchScreen");
5677 prepareDisplay(DISPLAY_ORIENTATION_0);
5678 prepareButtons();
5679 prepareAxes(POSITION);
5680 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005681 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005682
Michael Wrightd02c5b62014-02-10 15:10:22 -08005683 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005684 ASSERT_TRUE(
5685 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005686 ASSERT_TRUE(flags[0]);
5687 ASSERT_FALSE(flags[1]);
5688}
5689
5690TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005691 addConfigurationProperty("touch.deviceType", "touchScreen");
5692 prepareDisplay(DISPLAY_ORIENTATION_0);
5693 prepareButtons();
5694 prepareAxes(POSITION);
5695 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005696 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005697
arthurhungdcef2dc2020-08-11 14:47:50 +08005698 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005699
5700 NotifyKeyArgs args;
5701
5702 // Press virtual key.
5703 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5704 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5705 processDown(mapper, x, y);
5706 processSync(mapper);
5707
5708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5709 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5710 ASSERT_EQ(DEVICE_ID, args.deviceId);
5711 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5712 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5713 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5714 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5715 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5716 ASSERT_EQ(KEY_HOME, args.scanCode);
5717 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5718 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5719
5720 // Release virtual key.
5721 processUp(mapper);
5722 processSync(mapper);
5723
5724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5725 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5726 ASSERT_EQ(DEVICE_ID, args.deviceId);
5727 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5728 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5729 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5730 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5731 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5732 ASSERT_EQ(KEY_HOME, args.scanCode);
5733 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5734 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5735
5736 // Should not have sent any motions.
5737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5738}
5739
5740TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005741 addConfigurationProperty("touch.deviceType", "touchScreen");
5742 prepareDisplay(DISPLAY_ORIENTATION_0);
5743 prepareButtons();
5744 prepareAxes(POSITION);
5745 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005746 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005747
arthurhungdcef2dc2020-08-11 14:47:50 +08005748 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005749
5750 NotifyKeyArgs keyArgs;
5751
5752 // Press virtual key.
5753 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5754 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5755 processDown(mapper, x, y);
5756 processSync(mapper);
5757
5758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5759 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5760 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5761 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5762 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5763 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5764 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5765 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5766 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5767 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5768 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5769
5770 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5771 // into the display area.
5772 y -= 100;
5773 processMove(mapper, x, y);
5774 processSync(mapper);
5775
5776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5777 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5778 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5779 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5780 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5781 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5782 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5783 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5784 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5785 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5786 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5787 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5788
5789 NotifyMotionArgs motionArgs;
5790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5791 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5792 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5793 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5794 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5795 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5796 ASSERT_EQ(0, motionArgs.flags);
5797 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5798 ASSERT_EQ(0, motionArgs.buttonState);
5799 ASSERT_EQ(0, motionArgs.edgeFlags);
5800 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5801 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5802 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5803 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5804 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5805 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5806 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5807 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5808
5809 // Keep moving out of bounds. Should generate a pointer move.
5810 y -= 50;
5811 processMove(mapper, x, y);
5812 processSync(mapper);
5813
5814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5815 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5816 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5817 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5818 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5819 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5820 ASSERT_EQ(0, motionArgs.flags);
5821 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5822 ASSERT_EQ(0, motionArgs.buttonState);
5823 ASSERT_EQ(0, motionArgs.edgeFlags);
5824 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5825 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5826 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5827 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5828 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5829 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5830 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5831 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5832
5833 // Release out of bounds. Should generate a pointer up.
5834 processUp(mapper);
5835 processSync(mapper);
5836
5837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5838 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5839 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5840 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5841 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5842 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5843 ASSERT_EQ(0, motionArgs.flags);
5844 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5845 ASSERT_EQ(0, motionArgs.buttonState);
5846 ASSERT_EQ(0, motionArgs.edgeFlags);
5847 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5848 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5849 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5851 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5852 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5853 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5854 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5855
5856 // Should not have sent any more keys or motions.
5857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5859}
5860
5861TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005862 addConfigurationProperty("touch.deviceType", "touchScreen");
5863 prepareDisplay(DISPLAY_ORIENTATION_0);
5864 prepareButtons();
5865 prepareAxes(POSITION);
5866 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005867 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005868
arthurhungdcef2dc2020-08-11 14:47:50 +08005869 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005870
5871 NotifyMotionArgs motionArgs;
5872
5873 // Initially go down out of bounds.
5874 int32_t x = -10;
5875 int32_t y = -10;
5876 processDown(mapper, x, y);
5877 processSync(mapper);
5878
5879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5880
5881 // Move into the display area. Should generate a pointer down.
5882 x = 50;
5883 y = 75;
5884 processMove(mapper, x, y);
5885 processSync(mapper);
5886
5887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5888 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5889 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5890 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5891 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5892 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5893 ASSERT_EQ(0, motionArgs.flags);
5894 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5895 ASSERT_EQ(0, motionArgs.buttonState);
5896 ASSERT_EQ(0, motionArgs.edgeFlags);
5897 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5898 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5899 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5900 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5901 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5902 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5903 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5904 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5905
5906 // Release. Should generate a pointer up.
5907 processUp(mapper);
5908 processSync(mapper);
5909
5910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5911 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5912 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5913 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5914 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5915 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5916 ASSERT_EQ(0, motionArgs.flags);
5917 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5918 ASSERT_EQ(0, motionArgs.buttonState);
5919 ASSERT_EQ(0, motionArgs.edgeFlags);
5920 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5921 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5922 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5923 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5924 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5925 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5926 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5927 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5928
5929 // Should not have sent any more keys or motions.
5930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5932}
5933
Santos Cordonfa5cf462017-04-05 10:37:00 -07005934TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005935 addConfigurationProperty("touch.deviceType", "touchScreen");
5936 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5937
5938 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5939 prepareButtons();
5940 prepareAxes(POSITION);
5941 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005942 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005943
arthurhungdcef2dc2020-08-11 14:47:50 +08005944 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005945
5946 NotifyMotionArgs motionArgs;
5947
5948 // Down.
5949 int32_t x = 100;
5950 int32_t y = 125;
5951 processDown(mapper, x, y);
5952 processSync(mapper);
5953
5954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5955 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5956 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5957 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5958 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5959 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5960 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5961 ASSERT_EQ(0, motionArgs.flags);
5962 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5963 ASSERT_EQ(0, motionArgs.buttonState);
5964 ASSERT_EQ(0, motionArgs.edgeFlags);
5965 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5966 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5967 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5968 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5969 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5970 1, 0, 0, 0, 0, 0, 0, 0));
5971 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5972 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5973 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5974
5975 // Move.
5976 x += 50;
5977 y += 75;
5978 processMove(mapper, x, y);
5979 processSync(mapper);
5980
5981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5982 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5983 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5984 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5985 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5986 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5987 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5988 ASSERT_EQ(0, motionArgs.flags);
5989 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5990 ASSERT_EQ(0, motionArgs.buttonState);
5991 ASSERT_EQ(0, motionArgs.edgeFlags);
5992 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5993 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5994 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5995 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5996 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5997 1, 0, 0, 0, 0, 0, 0, 0));
5998 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5999 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6000 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6001
6002 // Up.
6003 processUp(mapper);
6004 processSync(mapper);
6005
6006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6007 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6008 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6009 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6010 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6011 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6012 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6013 ASSERT_EQ(0, motionArgs.flags);
6014 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6015 ASSERT_EQ(0, motionArgs.buttonState);
6016 ASSERT_EQ(0, motionArgs.edgeFlags);
6017 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6018 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6019 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6020 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6021 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6022 1, 0, 0, 0, 0, 0, 0, 0));
6023 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6024 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6025 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6026
6027 // Should not have sent any more keys or motions.
6028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6030}
6031
Michael Wrightd02c5b62014-02-10 15:10:22 -08006032TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006033 addConfigurationProperty("touch.deviceType", "touchScreen");
6034 prepareDisplay(DISPLAY_ORIENTATION_0);
6035 prepareButtons();
6036 prepareAxes(POSITION);
6037 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006038 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006039
arthurhungdcef2dc2020-08-11 14:47:50 +08006040 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006041
6042 NotifyMotionArgs motionArgs;
6043
6044 // Down.
6045 int32_t x = 100;
6046 int32_t y = 125;
6047 processDown(mapper, x, y);
6048 processSync(mapper);
6049
6050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6051 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6052 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6053 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6054 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6055 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6056 ASSERT_EQ(0, motionArgs.flags);
6057 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6058 ASSERT_EQ(0, motionArgs.buttonState);
6059 ASSERT_EQ(0, motionArgs.edgeFlags);
6060 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6061 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6062 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6063 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6064 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6065 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6066 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6067 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6068
6069 // Move.
6070 x += 50;
6071 y += 75;
6072 processMove(mapper, x, y);
6073 processSync(mapper);
6074
6075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6076 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6077 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6078 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6079 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6080 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6081 ASSERT_EQ(0, motionArgs.flags);
6082 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6083 ASSERT_EQ(0, motionArgs.buttonState);
6084 ASSERT_EQ(0, motionArgs.edgeFlags);
6085 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6086 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6087 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6088 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6089 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6090 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6091 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6092 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6093
6094 // Up.
6095 processUp(mapper);
6096 processSync(mapper);
6097
6098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6099 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6100 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6101 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6102 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6103 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6104 ASSERT_EQ(0, motionArgs.flags);
6105 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6106 ASSERT_EQ(0, motionArgs.buttonState);
6107 ASSERT_EQ(0, motionArgs.edgeFlags);
6108 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6109 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6110 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6111 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6112 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6113 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6114 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6115 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6116
6117 // Should not have sent any more keys or motions.
6118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6120}
6121
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006122TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006123 addConfigurationProperty("touch.deviceType", "touchScreen");
6124 prepareButtons();
6125 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006126 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
6127 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006128 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006129
6130 NotifyMotionArgs args;
6131
6132 // Rotation 90.
6133 prepareDisplay(DISPLAY_ORIENTATION_90);
6134 processDown(mapper, toRawX(50), toRawY(75));
6135 processSync(mapper);
6136
6137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6138 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6139 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6140
6141 processUp(mapper);
6142 processSync(mapper);
6143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6144}
6145
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006146TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006147 addConfigurationProperty("touch.deviceType", "touchScreen");
6148 prepareButtons();
6149 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006150 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6151 // orientation-aware are affected by display rotation.
6152 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006153 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006154
6155 NotifyMotionArgs args;
6156
6157 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006158 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006159 prepareDisplay(DISPLAY_ORIENTATION_0);
6160 processDown(mapper, toRawX(50), toRawY(75));
6161 processSync(mapper);
6162
6163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6164 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6165 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6166
6167 processUp(mapper);
6168 processSync(mapper);
6169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6170
6171 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006172 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006173 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006174 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006175 processSync(mapper);
6176
6177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6178 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6179 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6180
6181 processUp(mapper);
6182 processSync(mapper);
6183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6184
6185 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006186 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006187 prepareDisplay(DISPLAY_ORIENTATION_180);
6188 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6189 processSync(mapper);
6190
6191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6192 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6193 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6194
6195 processUp(mapper);
6196 processSync(mapper);
6197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6198
6199 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006200 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006201 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006202 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006203 processSync(mapper);
6204
6205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6206 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6207 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6208
6209 processUp(mapper);
6210 processSync(mapper);
6211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6212}
6213
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006214TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6215 addConfigurationProperty("touch.deviceType", "touchScreen");
6216 prepareButtons();
6217 prepareAxes(POSITION);
6218 addConfigurationProperty("touch.orientationAware", "1");
6219 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6220 clearViewports();
6221 prepareDisplay(DISPLAY_ORIENTATION_0);
6222 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6223 NotifyMotionArgs args;
6224
6225 // Orientation 0.
6226 processDown(mapper, toRawX(50), toRawY(75));
6227 processSync(mapper);
6228
6229 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6230 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6231 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6232
6233 processUp(mapper);
6234 processSync(mapper);
6235 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6236}
6237
6238TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6239 addConfigurationProperty("touch.deviceType", "touchScreen");
6240 prepareButtons();
6241 prepareAxes(POSITION);
6242 addConfigurationProperty("touch.orientationAware", "1");
6243 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6244 clearViewports();
6245 prepareDisplay(DISPLAY_ORIENTATION_0);
6246 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6247 NotifyMotionArgs args;
6248
6249 // Orientation 90.
6250 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6251 processSync(mapper);
6252
6253 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6254 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6255 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6256
6257 processUp(mapper);
6258 processSync(mapper);
6259 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6260}
6261
6262TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6263 addConfigurationProperty("touch.deviceType", "touchScreen");
6264 prepareButtons();
6265 prepareAxes(POSITION);
6266 addConfigurationProperty("touch.orientationAware", "1");
6267 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6268 clearViewports();
6269 prepareDisplay(DISPLAY_ORIENTATION_0);
6270 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6271 NotifyMotionArgs args;
6272
6273 // Orientation 180.
6274 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6275 processSync(mapper);
6276
6277 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6278 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6279 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6280
6281 processUp(mapper);
6282 processSync(mapper);
6283 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6284}
6285
6286TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6287 addConfigurationProperty("touch.deviceType", "touchScreen");
6288 prepareButtons();
6289 prepareAxes(POSITION);
6290 addConfigurationProperty("touch.orientationAware", "1");
6291 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6292 clearViewports();
6293 prepareDisplay(DISPLAY_ORIENTATION_0);
6294 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6295 NotifyMotionArgs args;
6296
6297 // Orientation 270.
6298 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6299 processSync(mapper);
6300
6301 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6302 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6303 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6304
6305 processUp(mapper);
6306 processSync(mapper);
6307 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6308}
6309
6310TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6311 addConfigurationProperty("touch.deviceType", "touchScreen");
6312 prepareButtons();
6313 prepareAxes(POSITION);
6314 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6315 // orientation-aware are affected by display rotation.
6316 addConfigurationProperty("touch.orientationAware", "0");
6317 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6318 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6319
6320 NotifyMotionArgs args;
6321
6322 // Orientation 90, Rotation 0.
6323 clearViewports();
6324 prepareDisplay(DISPLAY_ORIENTATION_0);
6325 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6326 processSync(mapper);
6327
6328 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6329 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6330 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6331
6332 processUp(mapper);
6333 processSync(mapper);
6334 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6335
6336 // Orientation 90, Rotation 90.
6337 clearViewports();
6338 prepareDisplay(DISPLAY_ORIENTATION_90);
6339 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6340 processSync(mapper);
6341
6342 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6343 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6344 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6345
6346 processUp(mapper);
6347 processSync(mapper);
6348 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6349
6350 // Orientation 90, Rotation 180.
6351 clearViewports();
6352 prepareDisplay(DISPLAY_ORIENTATION_180);
6353 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6354 processSync(mapper);
6355
6356 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6357 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6358 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6359
6360 processUp(mapper);
6361 processSync(mapper);
6362 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6363
6364 // Orientation 90, Rotation 270.
6365 clearViewports();
6366 prepareDisplay(DISPLAY_ORIENTATION_270);
6367 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6368 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6369 processSync(mapper);
6370
6371 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6372 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6373 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6374
6375 processUp(mapper);
6376 processSync(mapper);
6377 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6378}
6379
Michael Wrightd02c5b62014-02-10 15:10:22 -08006380TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006381 addConfigurationProperty("touch.deviceType", "touchScreen");
6382 prepareDisplay(DISPLAY_ORIENTATION_0);
6383 prepareButtons();
6384 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006385 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006386
6387 // These calculations are based on the input device calibration documentation.
6388 int32_t rawX = 100;
6389 int32_t rawY = 200;
6390 int32_t rawPressure = 10;
6391 int32_t rawToolMajor = 12;
6392 int32_t rawDistance = 2;
6393 int32_t rawTiltX = 30;
6394 int32_t rawTiltY = 110;
6395
6396 float x = toDisplayX(rawX);
6397 float y = toDisplayY(rawY);
6398 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6399 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6400 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6401 float distance = float(rawDistance);
6402
6403 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6404 float tiltScale = M_PI / 180;
6405 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6406 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6407 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6408 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6409
6410 processDown(mapper, rawX, rawY);
6411 processPressure(mapper, rawPressure);
6412 processToolMajor(mapper, rawToolMajor);
6413 processDistance(mapper, rawDistance);
6414 processTilt(mapper, rawTiltX, rawTiltY);
6415 processSync(mapper);
6416
6417 NotifyMotionArgs args;
6418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6419 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6420 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6421 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6422}
6423
Jason Gerecke489fda82012-09-07 17:19:40 -07006424TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006425 addConfigurationProperty("touch.deviceType", "touchScreen");
6426 prepareDisplay(DISPLAY_ORIENTATION_0);
6427 prepareLocationCalibration();
6428 prepareButtons();
6429 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006430 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006431
6432 int32_t rawX = 100;
6433 int32_t rawY = 200;
6434
6435 float x = toDisplayX(toCookedX(rawX, rawY));
6436 float y = toDisplayY(toCookedY(rawX, rawY));
6437
6438 processDown(mapper, rawX, rawY);
6439 processSync(mapper);
6440
6441 NotifyMotionArgs args;
6442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6444 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6445}
6446
Michael Wrightd02c5b62014-02-10 15:10:22 -08006447TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006448 addConfigurationProperty("touch.deviceType", "touchScreen");
6449 prepareDisplay(DISPLAY_ORIENTATION_0);
6450 prepareButtons();
6451 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006452 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006453
6454 NotifyMotionArgs motionArgs;
6455 NotifyKeyArgs keyArgs;
6456
6457 processDown(mapper, 100, 200);
6458 processSync(mapper);
6459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6460 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6461 ASSERT_EQ(0, motionArgs.buttonState);
6462
6463 // press BTN_LEFT, release BTN_LEFT
6464 processKey(mapper, BTN_LEFT, 1);
6465 processSync(mapper);
6466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6467 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6468 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6469
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6471 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6472 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6473
Michael Wrightd02c5b62014-02-10 15:10:22 -08006474 processKey(mapper, BTN_LEFT, 0);
6475 processSync(mapper);
6476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006477 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006478 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006479
6480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006482 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006483
6484 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6485 processKey(mapper, BTN_RIGHT, 1);
6486 processKey(mapper, BTN_MIDDLE, 1);
6487 processSync(mapper);
6488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6489 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6490 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6491 motionArgs.buttonState);
6492
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6494 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6495 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6496
6497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6498 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6499 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6500 motionArgs.buttonState);
6501
Michael Wrightd02c5b62014-02-10 15:10:22 -08006502 processKey(mapper, BTN_RIGHT, 0);
6503 processSync(mapper);
6504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006505 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006506 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006507
6508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006509 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006510 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006511
6512 processKey(mapper, BTN_MIDDLE, 0);
6513 processSync(mapper);
6514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006515 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006516 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006517
6518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006519 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006520 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006521
6522 // press BTN_BACK, release BTN_BACK
6523 processKey(mapper, BTN_BACK, 1);
6524 processSync(mapper);
6525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6526 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6527 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006528
Michael Wrightd02c5b62014-02-10 15:10:22 -08006529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006530 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006531 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6532
6533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6534 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6535 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006536
6537 processKey(mapper, BTN_BACK, 0);
6538 processSync(mapper);
6539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006540 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006541 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006542
6543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006544 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006545 ASSERT_EQ(0, motionArgs.buttonState);
6546
Michael Wrightd02c5b62014-02-10 15:10:22 -08006547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6548 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6549 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6550
6551 // press BTN_SIDE, release BTN_SIDE
6552 processKey(mapper, BTN_SIDE, 1);
6553 processSync(mapper);
6554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6555 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6556 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006557
Michael Wrightd02c5b62014-02-10 15:10:22 -08006558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006559 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006560 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6561
6562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6563 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6564 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006565
6566 processKey(mapper, BTN_SIDE, 0);
6567 processSync(mapper);
6568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006569 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006570 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006571
6572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006573 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006574 ASSERT_EQ(0, motionArgs.buttonState);
6575
Michael Wrightd02c5b62014-02-10 15:10:22 -08006576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6577 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6578 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6579
6580 // press BTN_FORWARD, release BTN_FORWARD
6581 processKey(mapper, BTN_FORWARD, 1);
6582 processSync(mapper);
6583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6584 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6585 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006586
Michael Wrightd02c5b62014-02-10 15:10:22 -08006587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006588 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006589 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6590
6591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6592 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6593 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006594
6595 processKey(mapper, BTN_FORWARD, 0);
6596 processSync(mapper);
6597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006598 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006599 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006600
6601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006602 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006603 ASSERT_EQ(0, motionArgs.buttonState);
6604
Michael Wrightd02c5b62014-02-10 15:10:22 -08006605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6606 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6607 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6608
6609 // press BTN_EXTRA, release BTN_EXTRA
6610 processKey(mapper, BTN_EXTRA, 1);
6611 processSync(mapper);
6612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6613 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6614 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006615
Michael Wrightd02c5b62014-02-10 15:10:22 -08006616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006617 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006618 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6619
6620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6621 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6622 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006623
6624 processKey(mapper, BTN_EXTRA, 0);
6625 processSync(mapper);
6626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006627 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006628 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006629
6630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006631 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006632 ASSERT_EQ(0, motionArgs.buttonState);
6633
Michael Wrightd02c5b62014-02-10 15:10:22 -08006634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6635 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6636 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6637
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6639
Michael Wrightd02c5b62014-02-10 15:10:22 -08006640 // press BTN_STYLUS, release BTN_STYLUS
6641 processKey(mapper, BTN_STYLUS, 1);
6642 processSync(mapper);
6643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006645 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6646
6647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6648 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6649 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006650
6651 processKey(mapper, BTN_STYLUS, 0);
6652 processSync(mapper);
6653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006654 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006655 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006656
6657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006658 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006659 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006660
6661 // press BTN_STYLUS2, release BTN_STYLUS2
6662 processKey(mapper, BTN_STYLUS2, 1);
6663 processSync(mapper);
6664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6665 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006666 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6667
6668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6669 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6670 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006671
6672 processKey(mapper, BTN_STYLUS2, 0);
6673 processSync(mapper);
6674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006675 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006676 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006677
6678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006679 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006680 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006681
6682 // release touch
6683 processUp(mapper);
6684 processSync(mapper);
6685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6686 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6687 ASSERT_EQ(0, motionArgs.buttonState);
6688}
6689
6690TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006691 addConfigurationProperty("touch.deviceType", "touchScreen");
6692 prepareDisplay(DISPLAY_ORIENTATION_0);
6693 prepareButtons();
6694 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006695 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006696
6697 NotifyMotionArgs motionArgs;
6698
6699 // default tool type is finger
6700 processDown(mapper, 100, 200);
6701 processSync(mapper);
6702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6703 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6704 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6705
6706 // eraser
6707 processKey(mapper, BTN_TOOL_RUBBER, 1);
6708 processSync(mapper);
6709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6710 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6711 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6712
6713 // stylus
6714 processKey(mapper, BTN_TOOL_RUBBER, 0);
6715 processKey(mapper, BTN_TOOL_PEN, 1);
6716 processSync(mapper);
6717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6718 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6719 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6720
6721 // brush
6722 processKey(mapper, BTN_TOOL_PEN, 0);
6723 processKey(mapper, BTN_TOOL_BRUSH, 1);
6724 processSync(mapper);
6725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6726 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6727 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6728
6729 // pencil
6730 processKey(mapper, BTN_TOOL_BRUSH, 0);
6731 processKey(mapper, BTN_TOOL_PENCIL, 1);
6732 processSync(mapper);
6733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6734 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6735 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6736
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006737 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006738 processKey(mapper, BTN_TOOL_PENCIL, 0);
6739 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6740 processSync(mapper);
6741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6742 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6743 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6744
6745 // mouse
6746 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6747 processKey(mapper, BTN_TOOL_MOUSE, 1);
6748 processSync(mapper);
6749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6750 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6751 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6752
6753 // lens
6754 processKey(mapper, BTN_TOOL_MOUSE, 0);
6755 processKey(mapper, BTN_TOOL_LENS, 1);
6756 processSync(mapper);
6757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6758 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6759 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6760
6761 // double-tap
6762 processKey(mapper, BTN_TOOL_LENS, 0);
6763 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6764 processSync(mapper);
6765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6766 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6767 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6768
6769 // triple-tap
6770 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6771 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6772 processSync(mapper);
6773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6774 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6775 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6776
6777 // quad-tap
6778 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6779 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6780 processSync(mapper);
6781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6782 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6783 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6784
6785 // finger
6786 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6787 processKey(mapper, BTN_TOOL_FINGER, 1);
6788 processSync(mapper);
6789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6790 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6791 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6792
6793 // stylus trumps finger
6794 processKey(mapper, BTN_TOOL_PEN, 1);
6795 processSync(mapper);
6796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6797 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6798 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6799
6800 // eraser trumps stylus
6801 processKey(mapper, BTN_TOOL_RUBBER, 1);
6802 processSync(mapper);
6803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6804 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6805 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6806
6807 // mouse trumps eraser
6808 processKey(mapper, BTN_TOOL_MOUSE, 1);
6809 processSync(mapper);
6810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6811 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6812 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6813
6814 // back to default tool type
6815 processKey(mapper, BTN_TOOL_MOUSE, 0);
6816 processKey(mapper, BTN_TOOL_RUBBER, 0);
6817 processKey(mapper, BTN_TOOL_PEN, 0);
6818 processKey(mapper, BTN_TOOL_FINGER, 0);
6819 processSync(mapper);
6820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6821 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6822 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6823}
6824
6825TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006826 addConfigurationProperty("touch.deviceType", "touchScreen");
6827 prepareDisplay(DISPLAY_ORIENTATION_0);
6828 prepareButtons();
6829 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006830 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006831 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006832
6833 NotifyMotionArgs motionArgs;
6834
6835 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6836 processKey(mapper, BTN_TOOL_FINGER, 1);
6837 processMove(mapper, 100, 200);
6838 processSync(mapper);
6839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6840 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6842 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6843
6844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6845 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6846 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6847 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6848
6849 // move a little
6850 processMove(mapper, 150, 250);
6851 processSync(mapper);
6852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6853 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6854 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6855 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6856
6857 // down when BTN_TOUCH is pressed, pressure defaults to 1
6858 processKey(mapper, BTN_TOUCH, 1);
6859 processSync(mapper);
6860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6861 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6862 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6863 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6864
6865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6866 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6867 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6868 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6869
6870 // up when BTN_TOUCH is released, hover restored
6871 processKey(mapper, BTN_TOUCH, 0);
6872 processSync(mapper);
6873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6874 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6875 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6876 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6877
6878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6879 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6880 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6881 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6882
6883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6884 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6886 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6887
6888 // exit hover when pointer goes away
6889 processKey(mapper, BTN_TOOL_FINGER, 0);
6890 processSync(mapper);
6891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6892 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6894 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6895}
6896
6897TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006898 addConfigurationProperty("touch.deviceType", "touchScreen");
6899 prepareDisplay(DISPLAY_ORIENTATION_0);
6900 prepareButtons();
6901 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006902 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006903
6904 NotifyMotionArgs motionArgs;
6905
6906 // initially hovering because pressure is 0
6907 processDown(mapper, 100, 200);
6908 processPressure(mapper, 0);
6909 processSync(mapper);
6910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6911 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6912 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6913 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6914
6915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6916 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6917 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6918 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6919
6920 // move a little
6921 processMove(mapper, 150, 250);
6922 processSync(mapper);
6923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6924 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6925 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6926 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6927
6928 // down when pressure is non-zero
6929 processPressure(mapper, RAW_PRESSURE_MAX);
6930 processSync(mapper);
6931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6932 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6933 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6934 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6935
6936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6937 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6938 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6939 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6940
6941 // up when pressure becomes 0, hover restored
6942 processPressure(mapper, 0);
6943 processSync(mapper);
6944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6945 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6946 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6947 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6948
6949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6950 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6951 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6952 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6953
6954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6955 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6956 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6957 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6958
6959 // exit hover when pointer goes away
6960 processUp(mapper);
6961 processSync(mapper);
6962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6963 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6964 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6965 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6966}
6967
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006968TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6969 addConfigurationProperty("touch.deviceType", "touchScreen");
6970 prepareDisplay(DISPLAY_ORIENTATION_0);
6971 prepareButtons();
6972 prepareAxes(POSITION | PRESSURE);
6973 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6974
6975 // Touch down.
6976 processDown(mapper, 100, 200);
6977 processPressure(mapper, 1);
6978 processSync(mapper);
6979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6980 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6981
6982 // Reset the mapper. This should cancel the ongoing gesture.
6983 resetMapper(mapper, ARBITRARY_TIME);
6984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6985 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6986
6987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6988}
6989
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006990TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6991 addConfigurationProperty("touch.deviceType", "touchScreen");
6992 prepareDisplay(DISPLAY_ORIENTATION_0);
6993 prepareButtons();
6994 prepareAxes(POSITION | PRESSURE);
6995 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6996
6997 // Set the initial state for the touch pointer.
6998 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6999 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
7000 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
7001 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7002
7003 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007004 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
7005 // does not generate any events.
7006 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007007
7008 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
7009 // the recreated touch state to generate a down event.
7010 processSync(mapper);
7011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7012 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
7013
7014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7015}
7016
lilinnan687e58f2022-07-19 16:00:50 +08007017TEST_F(SingleTouchInputMapperTest,
7018 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
7019 addConfigurationProperty("touch.deviceType", "touchScreen");
7020 prepareDisplay(DISPLAY_ORIENTATION_0);
7021 prepareButtons();
7022 prepareAxes(POSITION);
7023 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7024 NotifyMotionArgs motionArgs;
7025
7026 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00007027 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08007028 processSync(mapper);
7029
7030 // We should receive a down event
7031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7032 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7033
7034 // Change display id
7035 clearViewports();
7036 prepareSecondaryDisplay(ViewportType::INTERNAL);
7037
7038 // We should receive a cancel event
7039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7040 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7041 // Then receive reset called
7042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7043}
7044
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007045TEST_F(SingleTouchInputMapperTest,
7046 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
7047 addConfigurationProperty("touch.deviceType", "touchScreen");
7048 prepareDisplay(DISPLAY_ORIENTATION_0);
7049 prepareButtons();
7050 prepareAxes(POSITION);
7051 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7053 NotifyMotionArgs motionArgs;
7054
7055 // Start a new gesture.
7056 processDown(mapper, 100, 200);
7057 processSync(mapper);
7058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7059 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7060
7061 // Make the viewport inactive. This will put the device in disabled mode.
7062 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7063 viewport->isActive = false;
7064 mFakePolicy->updateViewport(*viewport);
7065 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7066
7067 // We should receive a cancel event for the ongoing gesture.
7068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7069 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7070 // Then we should be notified that the device was reset.
7071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7072
7073 // No events are generated while the viewport is inactive.
7074 processMove(mapper, 101, 201);
7075 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007076 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007077 processSync(mapper);
7078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7079
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007080 // Start a new gesture while the viewport is still inactive.
7081 processDown(mapper, 300, 400);
7082 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
7083 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
7084 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7085 processSync(mapper);
7086
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007087 // Make the viewport active again. The device should resume processing events.
7088 viewport->isActive = true;
7089 mFakePolicy->updateViewport(*viewport);
7090 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7091
7092 // The device is reset because it changes back to direct mode, without generating any events.
7093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7095
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007096 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007097 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7099 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007100
7101 // No more events.
7102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
7104}
7105
Prabir Pradhan5632d622021-09-06 07:57:20 -07007106// --- TouchDisplayProjectionTest ---
7107
7108class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
7109public:
7110 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
7111 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
7112 // rotated equivalent of the given un-rotated physical display bounds.
7113 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
7114 uint32_t inverseRotationFlags;
7115 auto width = DISPLAY_WIDTH;
7116 auto height = DISPLAY_HEIGHT;
7117 switch (orientation) {
7118 case DISPLAY_ORIENTATION_90:
7119 inverseRotationFlags = ui::Transform::ROT_270;
7120 std::swap(width, height);
7121 break;
7122 case DISPLAY_ORIENTATION_180:
7123 inverseRotationFlags = ui::Transform::ROT_180;
7124 break;
7125 case DISPLAY_ORIENTATION_270:
7126 inverseRotationFlags = ui::Transform::ROT_90;
7127 std::swap(width, height);
7128 break;
7129 case DISPLAY_ORIENTATION_0:
7130 inverseRotationFlags = ui::Transform::ROT_0;
7131 break;
7132 default:
7133 FAIL() << "Invalid orientation: " << orientation;
7134 }
7135
7136 const ui::Transform rotation(inverseRotationFlags, width, height);
7137 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
7138
7139 std::optional<DisplayViewport> internalViewport =
7140 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7141 DisplayViewport& v = *internalViewport;
7142 v.displayId = DISPLAY_ID;
7143 v.orientation = orientation;
7144
7145 v.logicalLeft = 0;
7146 v.logicalTop = 0;
7147 v.logicalRight = 100;
7148 v.logicalBottom = 100;
7149
7150 v.physicalLeft = rotatedPhysicalDisplay.left;
7151 v.physicalTop = rotatedPhysicalDisplay.top;
7152 v.physicalRight = rotatedPhysicalDisplay.right;
7153 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7154
7155 v.deviceWidth = width;
7156 v.deviceHeight = height;
7157
7158 v.isActive = true;
7159 v.uniqueId = UNIQUE_ID;
7160 v.type = ViewportType::INTERNAL;
7161 mFakePolicy->updateViewport(v);
7162 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7163 }
7164
7165 void assertReceivedMove(const Point& point) {
7166 NotifyMotionArgs motionArgs;
7167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7168 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7169 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7170 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7171 1, 0, 0, 0, 0, 0, 0, 0));
7172 }
7173};
7174
7175TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7176 addConfigurationProperty("touch.deviceType", "touchScreen");
7177 prepareDisplay(DISPLAY_ORIENTATION_0);
7178
7179 prepareButtons();
7180 prepareAxes(POSITION);
7181 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7182
7183 NotifyMotionArgs motionArgs;
7184
7185 // Configure the DisplayViewport such that the logical display maps to a subsection of
7186 // the display panel called the physical display. Here, the physical display is bounded by the
7187 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7188 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7189 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7190 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7191
7192 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7193 DISPLAY_ORIENTATION_270}) {
7194 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7195
7196 // Touches outside the physical display should be ignored, and should not generate any
7197 // events. Ensure touches at the following points that lie outside of the physical display
7198 // area do not generate any events.
7199 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7200 processDown(mapper, toRawX(point.x), toRawY(point.y));
7201 processSync(mapper);
7202 processUp(mapper);
7203 processSync(mapper);
7204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7205 << "Unexpected event generated for touch outside physical display at point: "
7206 << point.x << ", " << point.y;
7207 }
7208 }
7209}
7210
7211TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7212 addConfigurationProperty("touch.deviceType", "touchScreen");
7213 prepareDisplay(DISPLAY_ORIENTATION_0);
7214
7215 prepareButtons();
7216 prepareAxes(POSITION);
7217 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7218
7219 NotifyMotionArgs motionArgs;
7220
7221 // Configure the DisplayViewport such that the logical display maps to a subsection of
7222 // the display panel called the physical display. Here, the physical display is bounded by the
7223 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7224 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7225
7226 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7227 DISPLAY_ORIENTATION_270}) {
7228 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7229
7230 // Touches that start outside the physical display should be ignored until it enters the
7231 // physical display bounds, at which point it should generate a down event. Start a touch at
7232 // the point (5, 100), which is outside the physical display bounds.
7233 static const Point kOutsidePoint{5, 100};
7234 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7235 processSync(mapper);
7236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7237
7238 // Move the touch into the physical display area. This should generate a pointer down.
7239 processMove(mapper, toRawX(11), toRawY(21));
7240 processSync(mapper);
7241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7242 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7243 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7244 ASSERT_NO_FATAL_FAILURE(
7245 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7246
7247 // Move the touch inside the physical display area. This should generate a pointer move.
7248 processMove(mapper, toRawX(69), toRawY(159));
7249 processSync(mapper);
7250 assertReceivedMove({69, 159});
7251
7252 // Move outside the physical display area. Since the pointer is already down, this should
7253 // now continue generating events.
7254 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7255 processSync(mapper);
7256 assertReceivedMove(kOutsidePoint);
7257
7258 // Release. This should generate a pointer up.
7259 processUp(mapper);
7260 processSync(mapper);
7261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7262 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7264 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7265
7266 // Ensure no more events were generated.
7267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7269 }
7270}
7271
Michael Wrightd02c5b62014-02-10 15:10:22 -08007272// --- MultiTouchInputMapperTest ---
7273
7274class MultiTouchInputMapperTest : public TouchInputMapperTest {
7275protected:
7276 void prepareAxes(int axes);
7277
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007278 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7279 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7280 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7281 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7282 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7283 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7284 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7285 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7286 void processId(MultiTouchInputMapper& mapper, int32_t id);
7287 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7288 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7289 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
7290 void processMTSync(MultiTouchInputMapper& mapper);
7291 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007292};
7293
7294void MultiTouchInputMapperTest::prepareAxes(int axes) {
7295 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007296 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7297 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007298 }
7299 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007300 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7301 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007302 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007303 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7304 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007305 }
7306 }
7307 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007308 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7309 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007310 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007311 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007312 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007313 }
7314 }
7315 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007316 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7317 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007318 }
7319 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007320 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7321 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007322 }
7323 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007324 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7325 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007326 }
7327 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007328 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7329 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007330 }
7331 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007332 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7333 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007334 }
7335 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007336 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007337 }
7338}
7339
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007340void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7341 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007342 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7343 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007344}
7345
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007346void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7347 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007348 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007349}
7350
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007351void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7352 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007353 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007354}
7355
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007356void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007357 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007358}
7359
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007360void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007361 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007362}
7363
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007364void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7365 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007366 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007367}
7368
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007369void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007370 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007371}
7372
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007373void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007374 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007375}
7376
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007377void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007378 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007379}
7380
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007381void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007382 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007383}
7384
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007385void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007386 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007387}
7388
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007389void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7390 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007391 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007392}
7393
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007394void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007395 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007396}
7397
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007398void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007399 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007400}
7401
Michael Wrightd02c5b62014-02-10 15:10:22 -08007402TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007403 addConfigurationProperty("touch.deviceType", "touchScreen");
7404 prepareDisplay(DISPLAY_ORIENTATION_0);
7405 prepareAxes(POSITION);
7406 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007407 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007408
arthurhungdcef2dc2020-08-11 14:47:50 +08007409 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007410
7411 NotifyMotionArgs motionArgs;
7412
7413 // Two fingers down at once.
7414 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7415 processPosition(mapper, x1, y1);
7416 processMTSync(mapper);
7417 processPosition(mapper, x2, y2);
7418 processMTSync(mapper);
7419 processSync(mapper);
7420
7421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7422 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7423 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7424 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7425 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7426 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7427 ASSERT_EQ(0, motionArgs.flags);
7428 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7429 ASSERT_EQ(0, motionArgs.buttonState);
7430 ASSERT_EQ(0, motionArgs.edgeFlags);
7431 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7432 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7433 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7434 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7435 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7436 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7437 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7438 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7439
7440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7441 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7442 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7443 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7444 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007445 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007446 ASSERT_EQ(0, motionArgs.flags);
7447 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7448 ASSERT_EQ(0, motionArgs.buttonState);
7449 ASSERT_EQ(0, motionArgs.edgeFlags);
7450 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7451 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7452 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7453 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7454 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7455 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7456 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7457 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7458 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7459 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7460 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7461 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7462
7463 // Move.
7464 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7465 processPosition(mapper, x1, y1);
7466 processMTSync(mapper);
7467 processPosition(mapper, x2, y2);
7468 processMTSync(mapper);
7469 processSync(mapper);
7470
7471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7472 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7473 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7474 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7475 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7476 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7477 ASSERT_EQ(0, motionArgs.flags);
7478 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7479 ASSERT_EQ(0, motionArgs.buttonState);
7480 ASSERT_EQ(0, motionArgs.edgeFlags);
7481 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7482 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7483 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7484 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7485 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7486 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7487 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7488 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7489 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7490 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7491 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7492 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7493
7494 // First finger up.
7495 x2 += 15; y2 -= 20;
7496 processPosition(mapper, x2, y2);
7497 processMTSync(mapper);
7498 processSync(mapper);
7499
7500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7501 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7502 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7503 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7504 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007505 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007506 ASSERT_EQ(0, motionArgs.flags);
7507 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7508 ASSERT_EQ(0, motionArgs.buttonState);
7509 ASSERT_EQ(0, motionArgs.edgeFlags);
7510 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7511 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7512 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7513 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7514 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7516 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7517 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7518 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7519 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7520 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7521 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7522
7523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7524 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7525 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7526 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7527 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7528 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7529 ASSERT_EQ(0, motionArgs.flags);
7530 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7531 ASSERT_EQ(0, motionArgs.buttonState);
7532 ASSERT_EQ(0, motionArgs.edgeFlags);
7533 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7534 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7535 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7536 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7537 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7538 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7539 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7540 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7541
7542 // Move.
7543 x2 += 20; y2 -= 25;
7544 processPosition(mapper, x2, y2);
7545 processMTSync(mapper);
7546 processSync(mapper);
7547
7548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7549 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7550 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7551 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7552 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7553 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7554 ASSERT_EQ(0, motionArgs.flags);
7555 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7556 ASSERT_EQ(0, motionArgs.buttonState);
7557 ASSERT_EQ(0, motionArgs.edgeFlags);
7558 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7559 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7560 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7561 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7562 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7563 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7564 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7565 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7566
7567 // New finger down.
7568 int32_t x3 = 700, y3 = 300;
7569 processPosition(mapper, x2, y2);
7570 processMTSync(mapper);
7571 processPosition(mapper, x3, y3);
7572 processMTSync(mapper);
7573 processSync(mapper);
7574
7575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7576 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7577 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7578 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7579 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007580 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007581 ASSERT_EQ(0, motionArgs.flags);
7582 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7583 ASSERT_EQ(0, motionArgs.buttonState);
7584 ASSERT_EQ(0, motionArgs.edgeFlags);
7585 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7586 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7587 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7588 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7589 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7590 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7591 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7593 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7594 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7595 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7596 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7597
7598 // Second finger up.
7599 x3 += 30; y3 -= 20;
7600 processPosition(mapper, x3, y3);
7601 processMTSync(mapper);
7602 processSync(mapper);
7603
7604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7605 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7606 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7607 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7608 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007609 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007610 ASSERT_EQ(0, motionArgs.flags);
7611 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7612 ASSERT_EQ(0, motionArgs.buttonState);
7613 ASSERT_EQ(0, motionArgs.edgeFlags);
7614 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7615 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7616 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7617 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7618 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7619 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7620 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7622 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7623 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7624 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7625 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7626
7627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7628 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7629 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7630 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7631 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7632 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7633 ASSERT_EQ(0, motionArgs.flags);
7634 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7635 ASSERT_EQ(0, motionArgs.buttonState);
7636 ASSERT_EQ(0, motionArgs.edgeFlags);
7637 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7638 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7639 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7640 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7641 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7642 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7643 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7644 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7645
7646 // Last finger up.
7647 processMTSync(mapper);
7648 processSync(mapper);
7649
7650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7651 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7652 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7653 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7654 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7655 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7656 ASSERT_EQ(0, motionArgs.flags);
7657 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7658 ASSERT_EQ(0, motionArgs.buttonState);
7659 ASSERT_EQ(0, motionArgs.edgeFlags);
7660 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7661 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7662 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7663 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7664 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7665 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7666 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7667 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7668
7669 // Should not have sent any more keys or motions.
7670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7672}
7673
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007674TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7675 addConfigurationProperty("touch.deviceType", "touchScreen");
7676 prepareDisplay(DISPLAY_ORIENTATION_0);
7677
7678 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7679 /*fuzz*/ 0, /*resolution*/ 10);
7680 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7681 /*fuzz*/ 0, /*resolution*/ 11);
7682 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7683 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7684 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7685 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7686 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7687 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7688 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7689 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7690
7691 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7692
7693 // X and Y axes
7694 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7695 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7696 // Touch major and minor
7697 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7698 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7699 // Tool major and minor
7700 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7701 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7702}
7703
7704TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7705 addConfigurationProperty("touch.deviceType", "touchScreen");
7706 prepareDisplay(DISPLAY_ORIENTATION_0);
7707
7708 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7709 /*fuzz*/ 0, /*resolution*/ 10);
7710 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7711 /*fuzz*/ 0, /*resolution*/ 11);
7712
7713 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7714
7715 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7716
7717 // Touch major and minor
7718 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7719 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7720 // Tool major and minor
7721 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7722 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7723}
7724
Michael Wrightd02c5b62014-02-10 15:10:22 -08007725TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007726 addConfigurationProperty("touch.deviceType", "touchScreen");
7727 prepareDisplay(DISPLAY_ORIENTATION_0);
7728 prepareAxes(POSITION | ID);
7729 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007730 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007731
arthurhungdcef2dc2020-08-11 14:47:50 +08007732 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007733
7734 NotifyMotionArgs motionArgs;
7735
7736 // Two fingers down at once.
7737 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7738 processPosition(mapper, x1, y1);
7739 processId(mapper, 1);
7740 processMTSync(mapper);
7741 processPosition(mapper, x2, y2);
7742 processId(mapper, 2);
7743 processMTSync(mapper);
7744 processSync(mapper);
7745
7746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7747 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7748 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7749 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7750 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7752 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7753
7754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007755 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007756 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7757 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7758 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7759 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7760 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7761 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7762 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7763 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7764 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7765
7766 // Move.
7767 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7768 processPosition(mapper, x1, y1);
7769 processId(mapper, 1);
7770 processMTSync(mapper);
7771 processPosition(mapper, x2, y2);
7772 processId(mapper, 2);
7773 processMTSync(mapper);
7774 processSync(mapper);
7775
7776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7777 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7778 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7779 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7780 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7781 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7782 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7784 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7785 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7786 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7787
7788 // First finger up.
7789 x2 += 15; y2 -= 20;
7790 processPosition(mapper, x2, y2);
7791 processId(mapper, 2);
7792 processMTSync(mapper);
7793 processSync(mapper);
7794
7795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007796 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007797 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7798 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7799 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7800 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7801 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7803 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7804 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7805 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7806
7807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7808 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7809 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7810 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7811 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7812 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7813 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7814
7815 // Move.
7816 x2 += 20; y2 -= 25;
7817 processPosition(mapper, x2, y2);
7818 processId(mapper, 2);
7819 processMTSync(mapper);
7820 processSync(mapper);
7821
7822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7823 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7824 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7825 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7826 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7827 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7828 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7829
7830 // New finger down.
7831 int32_t x3 = 700, y3 = 300;
7832 processPosition(mapper, x2, y2);
7833 processId(mapper, 2);
7834 processMTSync(mapper);
7835 processPosition(mapper, x3, y3);
7836 processId(mapper, 3);
7837 processMTSync(mapper);
7838 processSync(mapper);
7839
7840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007841 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007842 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7843 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7844 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7845 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7846 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7847 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7848 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7849 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7850 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7851
7852 // Second finger up.
7853 x3 += 30; y3 -= 20;
7854 processPosition(mapper, x3, y3);
7855 processId(mapper, 3);
7856 processMTSync(mapper);
7857 processSync(mapper);
7858
7859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007860 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007861 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7862 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7863 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7864 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7865 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7866 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7867 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7868 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7869 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7870
7871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7872 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7873 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7874 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7875 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7876 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7877 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7878
7879 // Last finger up.
7880 processMTSync(mapper);
7881 processSync(mapper);
7882
7883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7884 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7885 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7886 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7887 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7888 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7889 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7890
7891 // Should not have sent any more keys or motions.
7892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7894}
7895
7896TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007897 addConfigurationProperty("touch.deviceType", "touchScreen");
7898 prepareDisplay(DISPLAY_ORIENTATION_0);
7899 prepareAxes(POSITION | ID | SLOT);
7900 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007901 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007902
arthurhungdcef2dc2020-08-11 14:47:50 +08007903 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007904
7905 NotifyMotionArgs motionArgs;
7906
7907 // Two fingers down at once.
7908 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7909 processPosition(mapper, x1, y1);
7910 processId(mapper, 1);
7911 processSlot(mapper, 1);
7912 processPosition(mapper, x2, y2);
7913 processId(mapper, 2);
7914 processSync(mapper);
7915
7916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7917 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7918 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7919 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7920 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7921 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7922 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7923
7924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007925 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007926 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7927 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7928 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7929 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7930 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7931 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7932 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7933 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7934 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7935
7936 // Move.
7937 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7938 processSlot(mapper, 0);
7939 processPosition(mapper, x1, y1);
7940 processSlot(mapper, 1);
7941 processPosition(mapper, x2, y2);
7942 processSync(mapper);
7943
7944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7945 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7946 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7947 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7948 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7949 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7950 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7951 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7952 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7953 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7954 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7955
7956 // First finger up.
7957 x2 += 15; y2 -= 20;
7958 processSlot(mapper, 0);
7959 processId(mapper, -1);
7960 processSlot(mapper, 1);
7961 processPosition(mapper, x2, y2);
7962 processSync(mapper);
7963
7964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007965 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007966 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7967 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7968 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7969 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7970 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7971 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7972 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7973 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7974 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7975
7976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7977 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7978 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7979 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7980 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7981 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7982 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7983
7984 // Move.
7985 x2 += 20; y2 -= 25;
7986 processPosition(mapper, x2, y2);
7987 processSync(mapper);
7988
7989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7990 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7991 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7992 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7993 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7994 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7995 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7996
7997 // New finger down.
7998 int32_t x3 = 700, y3 = 300;
7999 processPosition(mapper, x2, y2);
8000 processSlot(mapper, 0);
8001 processId(mapper, 3);
8002 processPosition(mapper, x3, y3);
8003 processSync(mapper);
8004
8005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008006 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008007 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8008 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8009 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8010 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8011 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8012 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8013 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8014 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8015 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8016
8017 // Second finger up.
8018 x3 += 30; y3 -= 20;
8019 processSlot(mapper, 1);
8020 processId(mapper, -1);
8021 processSlot(mapper, 0);
8022 processPosition(mapper, x3, y3);
8023 processSync(mapper);
8024
8025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008026 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008027 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8028 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8029 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8030 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8031 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8033 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8034 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8035 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8036
8037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8038 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8039 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8040 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8041 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8042 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8043 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8044
8045 // Last finger up.
8046 processId(mapper, -1);
8047 processSync(mapper);
8048
8049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8050 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8051 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8052 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8053 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8054 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8055 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8056
8057 // Should not have sent any more keys or motions.
8058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8060}
8061
8062TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008063 addConfigurationProperty("touch.deviceType", "touchScreen");
8064 prepareDisplay(DISPLAY_ORIENTATION_0);
8065 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008066 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008067
8068 // These calculations are based on the input device calibration documentation.
8069 int32_t rawX = 100;
8070 int32_t rawY = 200;
8071 int32_t rawTouchMajor = 7;
8072 int32_t rawTouchMinor = 6;
8073 int32_t rawToolMajor = 9;
8074 int32_t rawToolMinor = 8;
8075 int32_t rawPressure = 11;
8076 int32_t rawDistance = 0;
8077 int32_t rawOrientation = 3;
8078 int32_t id = 5;
8079
8080 float x = toDisplayX(rawX);
8081 float y = toDisplayY(rawY);
8082 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8083 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8084 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8085 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8086 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8087 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8088 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8089 float distance = float(rawDistance);
8090
8091 processPosition(mapper, rawX, rawY);
8092 processTouchMajor(mapper, rawTouchMajor);
8093 processTouchMinor(mapper, rawTouchMinor);
8094 processToolMajor(mapper, rawToolMajor);
8095 processToolMinor(mapper, rawToolMinor);
8096 processPressure(mapper, rawPressure);
8097 processOrientation(mapper, rawOrientation);
8098 processDistance(mapper, rawDistance);
8099 processId(mapper, id);
8100 processMTSync(mapper);
8101 processSync(mapper);
8102
8103 NotifyMotionArgs args;
8104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8105 ASSERT_EQ(0, args.pointerProperties[0].id);
8106 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8107 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8108 orientation, distance));
8109}
8110
8111TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008112 addConfigurationProperty("touch.deviceType", "touchScreen");
8113 prepareDisplay(DISPLAY_ORIENTATION_0);
8114 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8115 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008116 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008117
8118 // These calculations are based on the input device calibration documentation.
8119 int32_t rawX = 100;
8120 int32_t rawY = 200;
8121 int32_t rawTouchMajor = 140;
8122 int32_t rawTouchMinor = 120;
8123 int32_t rawToolMajor = 180;
8124 int32_t rawToolMinor = 160;
8125
8126 float x = toDisplayX(rawX);
8127 float y = toDisplayY(rawY);
8128 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8129 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8130 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8131 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8132 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8133
8134 processPosition(mapper, rawX, rawY);
8135 processTouchMajor(mapper, rawTouchMajor);
8136 processTouchMinor(mapper, rawTouchMinor);
8137 processToolMajor(mapper, rawToolMajor);
8138 processToolMinor(mapper, rawToolMinor);
8139 processMTSync(mapper);
8140 processSync(mapper);
8141
8142 NotifyMotionArgs args;
8143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8144 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8145 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8146}
8147
8148TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008149 addConfigurationProperty("touch.deviceType", "touchScreen");
8150 prepareDisplay(DISPLAY_ORIENTATION_0);
8151 prepareAxes(POSITION | TOUCH | TOOL);
8152 addConfigurationProperty("touch.size.calibration", "diameter");
8153 addConfigurationProperty("touch.size.scale", "10");
8154 addConfigurationProperty("touch.size.bias", "160");
8155 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008156 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008157
8158 // These calculations are based on the input device calibration documentation.
8159 // Note: We only provide a single common touch/tool value because the device is assumed
8160 // not to emit separate values for each pointer (isSummed = 1).
8161 int32_t rawX = 100;
8162 int32_t rawY = 200;
8163 int32_t rawX2 = 150;
8164 int32_t rawY2 = 250;
8165 int32_t rawTouchMajor = 5;
8166 int32_t rawToolMajor = 8;
8167
8168 float x = toDisplayX(rawX);
8169 float y = toDisplayY(rawY);
8170 float x2 = toDisplayX(rawX2);
8171 float y2 = toDisplayY(rawY2);
8172 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8173 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8174 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8175
8176 processPosition(mapper, rawX, rawY);
8177 processTouchMajor(mapper, rawTouchMajor);
8178 processToolMajor(mapper, rawToolMajor);
8179 processMTSync(mapper);
8180 processPosition(mapper, rawX2, rawY2);
8181 processTouchMajor(mapper, rawTouchMajor);
8182 processToolMajor(mapper, rawToolMajor);
8183 processMTSync(mapper);
8184 processSync(mapper);
8185
8186 NotifyMotionArgs args;
8187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8188 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8189
8190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008191 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008192 ASSERT_EQ(size_t(2), args.pointerCount);
8193 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8194 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8195 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8196 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8197}
8198
8199TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008200 addConfigurationProperty("touch.deviceType", "touchScreen");
8201 prepareDisplay(DISPLAY_ORIENTATION_0);
8202 prepareAxes(POSITION | TOUCH | TOOL);
8203 addConfigurationProperty("touch.size.calibration", "area");
8204 addConfigurationProperty("touch.size.scale", "43");
8205 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008206 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008207
8208 // These calculations are based on the input device calibration documentation.
8209 int32_t rawX = 100;
8210 int32_t rawY = 200;
8211 int32_t rawTouchMajor = 5;
8212 int32_t rawToolMajor = 8;
8213
8214 float x = toDisplayX(rawX);
8215 float y = toDisplayY(rawY);
8216 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8217 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8218 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8219
8220 processPosition(mapper, rawX, rawY);
8221 processTouchMajor(mapper, rawTouchMajor);
8222 processToolMajor(mapper, rawToolMajor);
8223 processMTSync(mapper);
8224 processSync(mapper);
8225
8226 NotifyMotionArgs args;
8227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8228 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8229 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8230}
8231
8232TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008233 addConfigurationProperty("touch.deviceType", "touchScreen");
8234 prepareDisplay(DISPLAY_ORIENTATION_0);
8235 prepareAxes(POSITION | PRESSURE);
8236 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8237 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008238 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008239
Michael Wrightaa449c92017-12-13 21:21:43 +00008240 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008241 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008242 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8243 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8244 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8245
Michael Wrightd02c5b62014-02-10 15:10:22 -08008246 // These calculations are based on the input device calibration documentation.
8247 int32_t rawX = 100;
8248 int32_t rawY = 200;
8249 int32_t rawPressure = 60;
8250
8251 float x = toDisplayX(rawX);
8252 float y = toDisplayY(rawY);
8253 float pressure = float(rawPressure) * 0.01f;
8254
8255 processPosition(mapper, rawX, rawY);
8256 processPressure(mapper, rawPressure);
8257 processMTSync(mapper);
8258 processSync(mapper);
8259
8260 NotifyMotionArgs args;
8261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8262 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8263 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8264}
8265
8266TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008267 addConfigurationProperty("touch.deviceType", "touchScreen");
8268 prepareDisplay(DISPLAY_ORIENTATION_0);
8269 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008270 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008271
8272 NotifyMotionArgs motionArgs;
8273 NotifyKeyArgs keyArgs;
8274
8275 processId(mapper, 1);
8276 processPosition(mapper, 100, 200);
8277 processSync(mapper);
8278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8279 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8280 ASSERT_EQ(0, motionArgs.buttonState);
8281
8282 // press BTN_LEFT, release BTN_LEFT
8283 processKey(mapper, BTN_LEFT, 1);
8284 processSync(mapper);
8285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8286 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8287 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8288
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8290 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8291 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8292
Michael Wrightd02c5b62014-02-10 15:10:22 -08008293 processKey(mapper, BTN_LEFT, 0);
8294 processSync(mapper);
8295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008296 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008297 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008298
8299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008300 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008301 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008302
8303 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8304 processKey(mapper, BTN_RIGHT, 1);
8305 processKey(mapper, BTN_MIDDLE, 1);
8306 processSync(mapper);
8307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8308 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8309 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8310 motionArgs.buttonState);
8311
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8313 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8314 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8315
8316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8317 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8318 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8319 motionArgs.buttonState);
8320
Michael Wrightd02c5b62014-02-10 15:10:22 -08008321 processKey(mapper, BTN_RIGHT, 0);
8322 processSync(mapper);
8323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008324 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008325 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008326
8327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008328 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008329 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008330
8331 processKey(mapper, BTN_MIDDLE, 0);
8332 processSync(mapper);
8333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008334 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008335 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008336
8337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008338 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008339 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008340
8341 // press BTN_BACK, release BTN_BACK
8342 processKey(mapper, BTN_BACK, 1);
8343 processSync(mapper);
8344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8345 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8346 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008347
Michael Wrightd02c5b62014-02-10 15:10:22 -08008348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008349 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008350 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8351
8352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8353 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8354 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008355
8356 processKey(mapper, BTN_BACK, 0);
8357 processSync(mapper);
8358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008359 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008360 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008361
8362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008363 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008364 ASSERT_EQ(0, motionArgs.buttonState);
8365
Michael Wrightd02c5b62014-02-10 15:10:22 -08008366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8367 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8368 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8369
8370 // press BTN_SIDE, release BTN_SIDE
8371 processKey(mapper, BTN_SIDE, 1);
8372 processSync(mapper);
8373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8374 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8375 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008376
Michael Wrightd02c5b62014-02-10 15:10:22 -08008377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008378 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008379 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8380
8381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8382 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8383 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008384
8385 processKey(mapper, BTN_SIDE, 0);
8386 processSync(mapper);
8387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008388 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008389 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008390
8391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008392 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008393 ASSERT_EQ(0, motionArgs.buttonState);
8394
Michael Wrightd02c5b62014-02-10 15:10:22 -08008395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8396 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8397 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8398
8399 // press BTN_FORWARD, release BTN_FORWARD
8400 processKey(mapper, BTN_FORWARD, 1);
8401 processSync(mapper);
8402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8403 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8404 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008405
Michael Wrightd02c5b62014-02-10 15:10:22 -08008406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008407 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008408 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8409
8410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8411 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8412 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008413
8414 processKey(mapper, BTN_FORWARD, 0);
8415 processSync(mapper);
8416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008417 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008418 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008419
8420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008421 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008422 ASSERT_EQ(0, motionArgs.buttonState);
8423
Michael Wrightd02c5b62014-02-10 15:10:22 -08008424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8425 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8426 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8427
8428 // press BTN_EXTRA, release BTN_EXTRA
8429 processKey(mapper, BTN_EXTRA, 1);
8430 processSync(mapper);
8431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8432 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8433 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008434
Michael Wrightd02c5b62014-02-10 15:10:22 -08008435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008436 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008437 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8438
8439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8440 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8441 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008442
8443 processKey(mapper, BTN_EXTRA, 0);
8444 processSync(mapper);
8445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008446 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008447 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008448
8449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008450 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008451 ASSERT_EQ(0, motionArgs.buttonState);
8452
Michael Wrightd02c5b62014-02-10 15:10:22 -08008453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8454 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8455 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8456
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8458
Michael Wrightd02c5b62014-02-10 15:10:22 -08008459 // press BTN_STYLUS, release BTN_STYLUS
8460 processKey(mapper, BTN_STYLUS, 1);
8461 processSync(mapper);
8462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8463 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008464 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8465
8466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8467 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8468 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008469
8470 processKey(mapper, BTN_STYLUS, 0);
8471 processSync(mapper);
8472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008473 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008474 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008475
8476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008477 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008478 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008479
8480 // press BTN_STYLUS2, release BTN_STYLUS2
8481 processKey(mapper, BTN_STYLUS2, 1);
8482 processSync(mapper);
8483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8484 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008485 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8486
8487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8488 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8489 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008490
8491 processKey(mapper, BTN_STYLUS2, 0);
8492 processSync(mapper);
8493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008494 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008495 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008496
8497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008498 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008499 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008500
8501 // release touch
8502 processId(mapper, -1);
8503 processSync(mapper);
8504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8505 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8506 ASSERT_EQ(0, motionArgs.buttonState);
8507}
8508
8509TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008510 addConfigurationProperty("touch.deviceType", "touchScreen");
8511 prepareDisplay(DISPLAY_ORIENTATION_0);
8512 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008513 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008514
8515 NotifyMotionArgs motionArgs;
8516
8517 // default tool type is finger
8518 processId(mapper, 1);
8519 processPosition(mapper, 100, 200);
8520 processSync(mapper);
8521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8522 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8523 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8524
8525 // eraser
8526 processKey(mapper, BTN_TOOL_RUBBER, 1);
8527 processSync(mapper);
8528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8529 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8530 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8531
8532 // stylus
8533 processKey(mapper, BTN_TOOL_RUBBER, 0);
8534 processKey(mapper, BTN_TOOL_PEN, 1);
8535 processSync(mapper);
8536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8537 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8538 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8539
8540 // brush
8541 processKey(mapper, BTN_TOOL_PEN, 0);
8542 processKey(mapper, BTN_TOOL_BRUSH, 1);
8543 processSync(mapper);
8544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8545 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8546 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8547
8548 // pencil
8549 processKey(mapper, BTN_TOOL_BRUSH, 0);
8550 processKey(mapper, BTN_TOOL_PENCIL, 1);
8551 processSync(mapper);
8552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8553 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8554 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8555
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008556 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008557 processKey(mapper, BTN_TOOL_PENCIL, 0);
8558 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8559 processSync(mapper);
8560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8561 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8562 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8563
8564 // mouse
8565 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8566 processKey(mapper, BTN_TOOL_MOUSE, 1);
8567 processSync(mapper);
8568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8569 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8570 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8571
8572 // lens
8573 processKey(mapper, BTN_TOOL_MOUSE, 0);
8574 processKey(mapper, BTN_TOOL_LENS, 1);
8575 processSync(mapper);
8576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8577 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8578 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8579
8580 // double-tap
8581 processKey(mapper, BTN_TOOL_LENS, 0);
8582 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8583 processSync(mapper);
8584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8585 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8586 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8587
8588 // triple-tap
8589 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8590 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8591 processSync(mapper);
8592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8593 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8594 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8595
8596 // quad-tap
8597 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8598 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8599 processSync(mapper);
8600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8601 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8602 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8603
8604 // finger
8605 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8606 processKey(mapper, BTN_TOOL_FINGER, 1);
8607 processSync(mapper);
8608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8609 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8610 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8611
8612 // stylus trumps finger
8613 processKey(mapper, BTN_TOOL_PEN, 1);
8614 processSync(mapper);
8615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8616 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8617 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8618
8619 // eraser trumps stylus
8620 processKey(mapper, BTN_TOOL_RUBBER, 1);
8621 processSync(mapper);
8622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8623 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8624 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8625
8626 // mouse trumps eraser
8627 processKey(mapper, BTN_TOOL_MOUSE, 1);
8628 processSync(mapper);
8629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8630 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8631 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8632
8633 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8634 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8635 processSync(mapper);
8636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8637 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8638 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8639
8640 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8641 processToolType(mapper, MT_TOOL_PEN);
8642 processSync(mapper);
8643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8645 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8646
8647 // back to default tool type
8648 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8649 processKey(mapper, BTN_TOOL_MOUSE, 0);
8650 processKey(mapper, BTN_TOOL_RUBBER, 0);
8651 processKey(mapper, BTN_TOOL_PEN, 0);
8652 processKey(mapper, BTN_TOOL_FINGER, 0);
8653 processSync(mapper);
8654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8655 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8656 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8657}
8658
8659TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008660 addConfigurationProperty("touch.deviceType", "touchScreen");
8661 prepareDisplay(DISPLAY_ORIENTATION_0);
8662 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008663 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008664 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008665
8666 NotifyMotionArgs motionArgs;
8667
8668 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8669 processId(mapper, 1);
8670 processPosition(mapper, 100, 200);
8671 processSync(mapper);
8672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8673 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8674 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8675 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8676
8677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8678 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8679 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8680 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8681
8682 // move a little
8683 processPosition(mapper, 150, 250);
8684 processSync(mapper);
8685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8686 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8687 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8688 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8689
8690 // down when BTN_TOUCH is pressed, pressure defaults to 1
8691 processKey(mapper, BTN_TOUCH, 1);
8692 processSync(mapper);
8693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8694 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8695 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8696 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8697
8698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8699 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8701 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8702
8703 // up when BTN_TOUCH is released, hover restored
8704 processKey(mapper, BTN_TOUCH, 0);
8705 processSync(mapper);
8706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8707 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8708 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8709 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8710
8711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8712 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8713 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8714 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8715
8716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8717 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8719 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8720
8721 // exit hover when pointer goes away
8722 processId(mapper, -1);
8723 processSync(mapper);
8724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8725 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8727 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8728}
8729
8730TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008731 addConfigurationProperty("touch.deviceType", "touchScreen");
8732 prepareDisplay(DISPLAY_ORIENTATION_0);
8733 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008734 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008735
8736 NotifyMotionArgs motionArgs;
8737
8738 // initially hovering because pressure is 0
8739 processId(mapper, 1);
8740 processPosition(mapper, 100, 200);
8741 processPressure(mapper, 0);
8742 processSync(mapper);
8743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8744 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8745 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8746 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8747
8748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8749 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8750 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8751 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8752
8753 // move a little
8754 processPosition(mapper, 150, 250);
8755 processSync(mapper);
8756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8757 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8758 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8759 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8760
8761 // down when pressure becomes non-zero
8762 processPressure(mapper, RAW_PRESSURE_MAX);
8763 processSync(mapper);
8764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8765 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8767 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8768
8769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8770 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8771 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8772 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8773
8774 // up when pressure becomes 0, hover restored
8775 processPressure(mapper, 0);
8776 processSync(mapper);
8777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8778 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8780 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8781
8782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8783 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8785 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8786
8787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8788 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8789 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8790 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8791
8792 // exit hover when pointer goes away
8793 processId(mapper, -1);
8794 processSync(mapper);
8795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8796 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8797 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8798 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8799}
8800
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008801/**
8802 * Set the input device port <--> display port associations, and check that the
8803 * events are routed to the display that matches the display port.
8804 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8805 */
8806TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008807 const std::string usb2 = "USB2";
8808 const uint8_t hdmi1 = 0;
8809 const uint8_t hdmi2 = 1;
8810 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008811 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008812
8813 addConfigurationProperty("touch.deviceType", "touchScreen");
8814 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008815 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008816
8817 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8818 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8819
8820 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8821 // for this input device is specified, and the matching viewport is not present,
8822 // the input device should be disabled (at the mapper level).
8823
8824 // Add viewport for display 2 on hdmi2
8825 prepareSecondaryDisplay(type, hdmi2);
8826 // Send a touch event
8827 processPosition(mapper, 100, 100);
8828 processSync(mapper);
8829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8830
8831 // Add viewport for display 1 on hdmi1
8832 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8833 // Send a touch event again
8834 processPosition(mapper, 100, 100);
8835 processSync(mapper);
8836
8837 NotifyMotionArgs args;
8838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8839 ASSERT_EQ(DISPLAY_ID, args.displayId);
8840}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008841
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008842TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8843 addConfigurationProperty("touch.deviceType", "touchScreen");
8844 prepareAxes(POSITION);
8845 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8846
8847 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8848
8849 prepareDisplay(DISPLAY_ORIENTATION_0);
8850 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8851
8852 // Send a touch event
8853 processPosition(mapper, 100, 100);
8854 processSync(mapper);
8855
8856 NotifyMotionArgs args;
8857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8858 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8859}
8860
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008861TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008862 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008863 std::shared_ptr<FakePointerController> fakePointerController =
8864 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008865 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008866 fakePointerController->setPosition(100, 200);
8867 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008868 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008869
Garfield Tan888a6a42020-01-09 11:39:16 -08008870 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008871 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008872
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008873 prepareDisplay(DISPLAY_ORIENTATION_0);
8874 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008875 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008876
8877 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008878 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008879
8880 NotifyMotionArgs motionArgs;
8881 processPosition(mapper, 100, 100);
8882 processSync(mapper);
8883
8884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8885 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8886 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8887}
8888
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008889/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008890 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8891 */
8892TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8893 addConfigurationProperty("touch.deviceType", "touchScreen");
8894 prepareAxes(POSITION);
8895 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8896
8897 prepareDisplay(DISPLAY_ORIENTATION_0);
8898 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8899 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8900 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8901 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8902
8903 NotifyMotionArgs args;
8904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8905 ASSERT_EQ(26, args.readTime);
8906
8907 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8908 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8909 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8910
8911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8912 ASSERT_EQ(33, args.readTime);
8913}
8914
8915/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008916 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8917 * events should not be delivered to the listener.
8918 */
8919TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8920 addConfigurationProperty("touch.deviceType", "touchScreen");
8921 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8922 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8923 ViewportType::INTERNAL);
8924 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8925 prepareAxes(POSITION);
8926 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8927
8928 NotifyMotionArgs motionArgs;
8929 processPosition(mapper, 100, 100);
8930 processSync(mapper);
8931
8932 mFakeListener->assertNotifyMotionWasNotCalled();
8933}
8934
Garfield Tanc734e4f2021-01-15 20:01:39 -08008935TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8936 addConfigurationProperty("touch.deviceType", "touchScreen");
8937 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8938 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8939 ViewportType::INTERNAL);
8940 std::optional<DisplayViewport> optionalDisplayViewport =
8941 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8942 ASSERT_TRUE(optionalDisplayViewport.has_value());
8943 DisplayViewport displayViewport = *optionalDisplayViewport;
8944
8945 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8946 prepareAxes(POSITION);
8947 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8948
8949 // Finger down
8950 int32_t x = 100, y = 100;
8951 processPosition(mapper, x, y);
8952 processSync(mapper);
8953
8954 NotifyMotionArgs motionArgs;
8955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8956 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8957
8958 // Deactivate display viewport
8959 displayViewport.isActive = false;
8960 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8961 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8962
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008963 // The ongoing touch should be canceled immediately
8964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8965 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8966
8967 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008968 x += 10, y += 10;
8969 processPosition(mapper, x, y);
8970 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008972
8973 // Reactivate display viewport
8974 displayViewport.isActive = true;
8975 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8976 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8977
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008978 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008979 x += 10, y += 10;
8980 processPosition(mapper, x, y);
8981 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8983 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008984}
8985
Arthur Hung7c645402019-01-25 17:45:42 +08008986TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8987 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008988 prepareAxes(POSITION | ID | SLOT);
8989 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008990 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008991
8992 // Create the second touch screen device, and enable multi fingers.
8993 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008994 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008995 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008996 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008997 std::shared_ptr<InputDevice> device2 =
8998 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008999 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009000
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009001 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9002 0 /*flat*/, 0 /*fuzz*/);
9003 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9004 0 /*flat*/, 0 /*fuzz*/);
9005 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
9006 0 /*flat*/, 0 /*fuzz*/);
9007 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
9008 0 /*flat*/, 0 /*fuzz*/);
9009 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
9010 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9011 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009012
9013 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009014 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009015 std::list<NotifyArgs> unused =
9016 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9017 0 /*changes*/);
9018 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009019
9020 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009021 std::shared_ptr<FakePointerController> fakePointerController =
9022 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009023 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009024
9025 // Setup policy for associated displays and show touches.
9026 const uint8_t hdmi1 = 0;
9027 const uint8_t hdmi2 = 1;
9028 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9029 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9030 mFakePolicy->setShowTouches(true);
9031
9032 // Create displays.
9033 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009034 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009035
9036 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009037 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9038 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
9039 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009040
9041 // Two fingers down at default display.
9042 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9043 processPosition(mapper, x1, y1);
9044 processId(mapper, 1);
9045 processSlot(mapper, 1);
9046 processPosition(mapper, x2, y2);
9047 processId(mapper, 2);
9048 processSync(mapper);
9049
9050 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9051 fakePointerController->getSpots().find(DISPLAY_ID);
9052 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9053 ASSERT_EQ(size_t(2), iter->second.size());
9054
9055 // Two fingers down at second display.
9056 processPosition(mapper2, x1, y1);
9057 processId(mapper2, 1);
9058 processSlot(mapper2, 1);
9059 processPosition(mapper2, x2, y2);
9060 processId(mapper2, 2);
9061 processSync(mapper2);
9062
9063 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9064 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9065 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009066
9067 // Disable the show touches configuration and ensure the spots are cleared.
9068 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009069 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9070 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009071
9072 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009073}
9074
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009075TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009076 prepareAxes(POSITION);
9077 addConfigurationProperty("touch.deviceType", "touchScreen");
9078 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009079 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009080
9081 NotifyMotionArgs motionArgs;
9082 // Unrotated video frame
9083 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9084 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009085 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009086 processPosition(mapper, 100, 200);
9087 processSync(mapper);
9088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9089 ASSERT_EQ(frames, motionArgs.videoFrames);
9090
9091 // Subsequent touch events should not have any videoframes
9092 // This is implemented separately in FakeEventHub,
9093 // but that should match the behaviour of TouchVideoDevice.
9094 processPosition(mapper, 200, 200);
9095 processSync(mapper);
9096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9097 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9098}
9099
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009100TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009101 prepareAxes(POSITION);
9102 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009103 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009104 // Unrotated video frame
9105 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9106 NotifyMotionArgs motionArgs;
9107
9108 // Test all 4 orientations
9109 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009110 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
9111 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9112 clearViewports();
9113 prepareDisplay(orientation);
9114 std::vector<TouchVideoFrame> frames{frame};
9115 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9116 processPosition(mapper, 100, 200);
9117 processSync(mapper);
9118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9119 ASSERT_EQ(frames, motionArgs.videoFrames);
9120 }
9121}
9122
9123TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9124 prepareAxes(POSITION);
9125 addConfigurationProperty("touch.deviceType", "touchScreen");
9126 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9127 // orientation-aware are affected by display rotation.
9128 addConfigurationProperty("touch.orientationAware", "0");
9129 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9130 // Unrotated video frame
9131 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9132 NotifyMotionArgs motionArgs;
9133
9134 // Test all 4 orientations
9135 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009136 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
9137 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9138 clearViewports();
9139 prepareDisplay(orientation);
9140 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009141 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009142 processPosition(mapper, 100, 200);
9143 processSync(mapper);
9144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009145 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9146 // compared to the display. This is so that when the window transform (which contains the
9147 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9148 // window's coordinate space.
9149 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009150 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009151
9152 // Release finger.
9153 processSync(mapper);
9154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009155 }
9156}
9157
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009158TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009159 prepareAxes(POSITION);
9160 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009161 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009162 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9163 // so mix these.
9164 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9165 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9166 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9167 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9168 NotifyMotionArgs motionArgs;
9169
9170 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009171 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009172 processPosition(mapper, 100, 200);
9173 processSync(mapper);
9174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009175 ASSERT_EQ(frames, motionArgs.videoFrames);
9176}
9177
9178TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9179 prepareAxes(POSITION);
9180 addConfigurationProperty("touch.deviceType", "touchScreen");
9181 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9182 // orientation-aware are affected by display rotation.
9183 addConfigurationProperty("touch.orientationAware", "0");
9184 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9185 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9186 // so mix these.
9187 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9188 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9189 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9190 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9191 NotifyMotionArgs motionArgs;
9192
9193 prepareDisplay(DISPLAY_ORIENTATION_90);
9194 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9195 processPosition(mapper, 100, 200);
9196 processSync(mapper);
9197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9198 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9199 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9200 // compared to the display. This is so that when the window transform (which contains the
9201 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9202 // window's coordinate space.
9203 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
9204 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009205 ASSERT_EQ(frames, motionArgs.videoFrames);
9206}
9207
Arthur Hung9da14732019-09-02 16:16:58 +08009208/**
9209 * If we had defined port associations, but the viewport is not ready, the touch device would be
9210 * expected to be disabled, and it should be enabled after the viewport has found.
9211 */
9212TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009213 constexpr uint8_t hdmi2 = 1;
9214 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009215 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009216
9217 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9218
9219 addConfigurationProperty("touch.deviceType", "touchScreen");
9220 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009221 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009222
9223 ASSERT_EQ(mDevice->isEnabled(), false);
9224
9225 // Add display on hdmi2, the device should be enabled and can receive touch event.
9226 prepareSecondaryDisplay(type, hdmi2);
9227 ASSERT_EQ(mDevice->isEnabled(), true);
9228
9229 // Send a touch event.
9230 processPosition(mapper, 100, 100);
9231 processSync(mapper);
9232
9233 NotifyMotionArgs args;
9234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9235 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9236}
9237
Arthur Hung421eb1c2020-01-16 00:09:42 +08009238TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009239 addConfigurationProperty("touch.deviceType", "touchScreen");
9240 prepareDisplay(DISPLAY_ORIENTATION_0);
9241 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009242 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009243
9244 NotifyMotionArgs motionArgs;
9245
9246 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9247 // finger down
9248 processId(mapper, 1);
9249 processPosition(mapper, x1, y1);
9250 processSync(mapper);
9251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9252 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9253 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9254
9255 // finger move
9256 processId(mapper, 1);
9257 processPosition(mapper, x2, y2);
9258 processSync(mapper);
9259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9260 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9261 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9262
9263 // finger up.
9264 processId(mapper, -1);
9265 processSync(mapper);
9266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9267 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9268 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9269
9270 // new finger down
9271 processId(mapper, 1);
9272 processPosition(mapper, x3, y3);
9273 processSync(mapper);
9274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9275 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9276 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9277}
9278
9279/**
arthurhungcc7f9802020-04-30 17:55:40 +08009280 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9281 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009282 */
arthurhungcc7f9802020-04-30 17:55:40 +08009283TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009284 addConfigurationProperty("touch.deviceType", "touchScreen");
9285 prepareDisplay(DISPLAY_ORIENTATION_0);
9286 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009287 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009288
9289 NotifyMotionArgs motionArgs;
9290
9291 // default tool type is finger
9292 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009293 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009294 processPosition(mapper, x1, y1);
9295 processSync(mapper);
9296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9297 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9298 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9299
9300 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9301 processToolType(mapper, MT_TOOL_PALM);
9302 processSync(mapper);
9303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9304 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9305
9306 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009307 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009308 processPosition(mapper, x2, y2);
9309 processSync(mapper);
9310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9311
9312 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009313 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009314 processSync(mapper);
9315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9316
9317 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009318 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009319 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009320 processPosition(mapper, x3, y3);
9321 processSync(mapper);
9322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9323 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9324 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9325}
9326
arthurhungbf89a482020-04-17 17:37:55 +08009327/**
arthurhungcc7f9802020-04-30 17:55:40 +08009328 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9329 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009330 */
arthurhungcc7f9802020-04-30 17:55:40 +08009331TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009332 addConfigurationProperty("touch.deviceType", "touchScreen");
9333 prepareDisplay(DISPLAY_ORIENTATION_0);
9334 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9335 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9336
9337 NotifyMotionArgs motionArgs;
9338
9339 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009340 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9341 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009342 processPosition(mapper, x1, y1);
9343 processSync(mapper);
9344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9345 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9346 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9347
9348 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009349 processSlot(mapper, SECOND_SLOT);
9350 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009351 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009352 processSync(mapper);
9353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009354 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009355 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9356
9357 // If the tool type of the first finger changes to MT_TOOL_PALM,
9358 // we expect to receive ACTION_POINTER_UP with cancel flag.
9359 processSlot(mapper, FIRST_SLOT);
9360 processId(mapper, FIRST_TRACKING_ID);
9361 processToolType(mapper, MT_TOOL_PALM);
9362 processSync(mapper);
9363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009364 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009365 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9366
9367 // The following MOVE events of second finger should be processed.
9368 processSlot(mapper, SECOND_SLOT);
9369 processId(mapper, SECOND_TRACKING_ID);
9370 processPosition(mapper, x2 + 1, y2 + 1);
9371 processSync(mapper);
9372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9373 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9374 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9375
9376 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9377 // it. Second finger receive move.
9378 processSlot(mapper, FIRST_SLOT);
9379 processId(mapper, INVALID_TRACKING_ID);
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 // Second finger keeps moving.
9386 processSlot(mapper, SECOND_SLOT);
9387 processId(mapper, SECOND_TRACKING_ID);
9388 processPosition(mapper, x2 + 2, y2 + 2);
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 up.
9395 processId(mapper, INVALID_TRACKING_ID);
9396 processSync(mapper);
9397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9398 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9399 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9400}
9401
9402/**
9403 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9404 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9405 */
9406TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9407 addConfigurationProperty("touch.deviceType", "touchScreen");
9408 prepareDisplay(DISPLAY_ORIENTATION_0);
9409 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9410 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9411
9412 NotifyMotionArgs motionArgs;
9413
9414 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9415 // First finger down.
9416 processId(mapper, FIRST_TRACKING_ID);
9417 processPosition(mapper, x1, y1);
9418 processSync(mapper);
9419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9420 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9421 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9422
9423 // Second finger down.
9424 processSlot(mapper, SECOND_SLOT);
9425 processId(mapper, SECOND_TRACKING_ID);
9426 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009427 processSync(mapper);
9428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009429 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009430 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9431
arthurhungcc7f9802020-04-30 17:55:40 +08009432 // If the tool type of the first finger changes to MT_TOOL_PALM,
9433 // we expect to receive ACTION_POINTER_UP with cancel flag.
9434 processSlot(mapper, FIRST_SLOT);
9435 processId(mapper, FIRST_TRACKING_ID);
9436 processToolType(mapper, MT_TOOL_PALM);
9437 processSync(mapper);
9438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009439 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009440 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9441
9442 // Second finger keeps moving.
9443 processSlot(mapper, SECOND_SLOT);
9444 processId(mapper, SECOND_TRACKING_ID);
9445 processPosition(mapper, x2 + 1, y2 + 1);
9446 processSync(mapper);
9447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9448 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9449
9450 // second finger becomes palm, receive cancel due to only 1 finger is active.
9451 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009452 processToolType(mapper, MT_TOOL_PALM);
9453 processSync(mapper);
9454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9455 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9456
arthurhungcc7f9802020-04-30 17:55:40 +08009457 // third finger down.
9458 processSlot(mapper, THIRD_SLOT);
9459 processId(mapper, THIRD_TRACKING_ID);
9460 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009461 processPosition(mapper, x3, y3);
9462 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9464 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9465 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009466 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9467
9468 // third finger move
9469 processId(mapper, THIRD_TRACKING_ID);
9470 processPosition(mapper, x3 + 1, y3 + 1);
9471 processSync(mapper);
9472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9473 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9474
9475 // first finger up, third finger receive move.
9476 processSlot(mapper, FIRST_SLOT);
9477 processId(mapper, INVALID_TRACKING_ID);
9478 processSync(mapper);
9479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9481 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9482
9483 // second finger up, third finger receive move.
9484 processSlot(mapper, SECOND_SLOT);
9485 processId(mapper, INVALID_TRACKING_ID);
9486 processSync(mapper);
9487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9488 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9489 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9490
9491 // third finger up.
9492 processSlot(mapper, THIRD_SLOT);
9493 processId(mapper, INVALID_TRACKING_ID);
9494 processSync(mapper);
9495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9496 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9497 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9498}
9499
9500/**
9501 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9502 * and the active finger could still be allowed to receive the events
9503 */
9504TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9505 addConfigurationProperty("touch.deviceType", "touchScreen");
9506 prepareDisplay(DISPLAY_ORIENTATION_0);
9507 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9508 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9509
9510 NotifyMotionArgs motionArgs;
9511
9512 // default tool type is finger
9513 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9514 processId(mapper, FIRST_TRACKING_ID);
9515 processPosition(mapper, x1, y1);
9516 processSync(mapper);
9517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9518 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9519 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9520
9521 // Second finger down.
9522 processSlot(mapper, SECOND_SLOT);
9523 processId(mapper, SECOND_TRACKING_ID);
9524 processPosition(mapper, x2, y2);
9525 processSync(mapper);
9526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009527 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009528 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9529
9530 // If the tool type of the second finger changes to MT_TOOL_PALM,
9531 // we expect to receive ACTION_POINTER_UP with cancel flag.
9532 processId(mapper, SECOND_TRACKING_ID);
9533 processToolType(mapper, MT_TOOL_PALM);
9534 processSync(mapper);
9535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009536 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009537 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9538
9539 // The following MOVE event should be processed.
9540 processSlot(mapper, FIRST_SLOT);
9541 processId(mapper, FIRST_TRACKING_ID);
9542 processPosition(mapper, x1 + 1, y1 + 1);
9543 processSync(mapper);
9544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9545 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9546 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9547
9548 // second finger up.
9549 processSlot(mapper, SECOND_SLOT);
9550 processId(mapper, INVALID_TRACKING_ID);
9551 processSync(mapper);
9552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9553 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9554
9555 // first finger keep moving
9556 processSlot(mapper, FIRST_SLOT);
9557 processId(mapper, FIRST_TRACKING_ID);
9558 processPosition(mapper, x1 + 2, y1 + 2);
9559 processSync(mapper);
9560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9561 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9562
9563 // first finger up.
9564 processId(mapper, INVALID_TRACKING_ID);
9565 processSync(mapper);
9566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9567 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9568 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009569}
9570
Arthur Hung9ad18942021-06-19 02:04:46 +00009571/**
9572 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9573 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9574 * cause slot be valid again.
9575 */
9576TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9577 addConfigurationProperty("touch.deviceType", "touchScreen");
9578 prepareDisplay(DISPLAY_ORIENTATION_0);
9579 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9580 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9581
9582 NotifyMotionArgs motionArgs;
9583
9584 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9585 // First finger down.
9586 processId(mapper, FIRST_TRACKING_ID);
9587 processPosition(mapper, x1, y1);
9588 processPressure(mapper, RAW_PRESSURE_MAX);
9589 processSync(mapper);
9590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9591 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9592 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9593
9594 // First finger move.
9595 processId(mapper, FIRST_TRACKING_ID);
9596 processPosition(mapper, x1 + 1, y1 + 1);
9597 processPressure(mapper, RAW_PRESSURE_MAX);
9598 processSync(mapper);
9599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9600 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9601 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9602
9603 // Second finger down.
9604 processSlot(mapper, SECOND_SLOT);
9605 processId(mapper, SECOND_TRACKING_ID);
9606 processPosition(mapper, x2, y2);
9607 processPressure(mapper, RAW_PRESSURE_MAX);
9608 processSync(mapper);
9609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009610 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009611 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9612
9613 // second finger up with some unexpected data.
9614 processSlot(mapper, SECOND_SLOT);
9615 processId(mapper, INVALID_TRACKING_ID);
9616 processPosition(mapper, x2, y2);
9617 processSync(mapper);
9618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009619 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009620 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9621
9622 // first finger up with some unexpected data.
9623 processSlot(mapper, FIRST_SLOT);
9624 processId(mapper, INVALID_TRACKING_ID);
9625 processPosition(mapper, x2, y2);
9626 processPressure(mapper, RAW_PRESSURE_MAX);
9627 processSync(mapper);
9628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9629 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9630 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9631}
9632
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009633TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9634 addConfigurationProperty("touch.deviceType", "touchScreen");
9635 prepareDisplay(DISPLAY_ORIENTATION_0);
9636 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9637 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9638
9639 // First finger down.
9640 processId(mapper, FIRST_TRACKING_ID);
9641 processPosition(mapper, 100, 200);
9642 processPressure(mapper, RAW_PRESSURE_MAX);
9643 processSync(mapper);
9644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9645 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9646
9647 // Second finger down.
9648 processSlot(mapper, SECOND_SLOT);
9649 processId(mapper, SECOND_TRACKING_ID);
9650 processPosition(mapper, 300, 400);
9651 processPressure(mapper, RAW_PRESSURE_MAX);
9652 processSync(mapper);
9653 ASSERT_NO_FATAL_FAILURE(
9654 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9655
9656 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009657 // preserved. Resetting should cancel the ongoing gesture.
9658 resetMapper(mapper, ARBITRARY_TIME);
9659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9660 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009661
9662 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9663 // the existing touch state to generate a down event.
9664 processPosition(mapper, 301, 302);
9665 processSync(mapper);
9666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9667 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
9668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9669 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
9670
9671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9672}
9673
9674TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9675 addConfigurationProperty("touch.deviceType", "touchScreen");
9676 prepareDisplay(DISPLAY_ORIENTATION_0);
9677 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9678 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9679
9680 // First finger touches down and releases.
9681 processId(mapper, FIRST_TRACKING_ID);
9682 processPosition(mapper, 100, 200);
9683 processPressure(mapper, RAW_PRESSURE_MAX);
9684 processSync(mapper);
9685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9686 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9687 processId(mapper, INVALID_TRACKING_ID);
9688 processSync(mapper);
9689 ASSERT_NO_FATAL_FAILURE(
9690 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9691
9692 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9693 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009694 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9696
9697 // Send an empty sync frame. Since there are no pointers, no events are generated.
9698 processSync(mapper);
9699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9700}
9701
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009702// --- MultiTouchInputMapperTest_ExternalDevice ---
9703
9704class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9705protected:
Chris Yea52ade12020-08-27 16:49:20 -07009706 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009707};
9708
9709/**
9710 * Expect fallback to internal viewport if device is external and external viewport is not present.
9711 */
9712TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9713 prepareAxes(POSITION);
9714 addConfigurationProperty("touch.deviceType", "touchScreen");
9715 prepareDisplay(DISPLAY_ORIENTATION_0);
9716 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9717
9718 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9719
9720 NotifyMotionArgs motionArgs;
9721
9722 // Expect the event to be sent to the internal viewport,
9723 // because an external viewport is not present.
9724 processPosition(mapper, 100, 100);
9725 processSync(mapper);
9726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9727 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9728
9729 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009730 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009731 processPosition(mapper, 100, 100);
9732 processSync(mapper);
9733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9734 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9735}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009736
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009737TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9738 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9739 std::shared_ptr<FakePointerController> fakePointerController =
9740 std::make_shared<FakePointerController>();
9741 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9742 fakePointerController->setPosition(0, 0);
9743 fakePointerController->setButtonState(0);
9744
9745 // prepare device and capture
9746 prepareDisplay(DISPLAY_ORIENTATION_0);
9747 prepareAxes(POSITION | ID | SLOT);
9748 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9749 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9750 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009751 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009752 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9753
9754 // captured touchpad should be a touchpad source
9755 NotifyDeviceResetArgs resetArgs;
9756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9757 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9758
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009759 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009760
9761 const InputDeviceInfo::MotionRange* relRangeX =
9762 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9763 ASSERT_NE(relRangeX, nullptr);
9764 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9765 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9766 const InputDeviceInfo::MotionRange* relRangeY =
9767 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9768 ASSERT_NE(relRangeY, nullptr);
9769 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9770 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9771
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009772 // run captured pointer tests - note that this is unscaled, so input listener events should be
9773 // identical to what the hardware sends (accounting for any
9774 // calibration).
9775 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009776 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009777 processId(mapper, 1);
9778 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9779 processKey(mapper, BTN_TOUCH, 1);
9780 processSync(mapper);
9781
9782 // expect coord[0] to contain initial location of touch 0
9783 NotifyMotionArgs args;
9784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9785 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9786 ASSERT_EQ(1U, args.pointerCount);
9787 ASSERT_EQ(0, args.pointerProperties[0].id);
9788 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9789 ASSERT_NO_FATAL_FAILURE(
9790 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9791
9792 // FINGER 1 DOWN
9793 processSlot(mapper, 1);
9794 processId(mapper, 2);
9795 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9796 processSync(mapper);
9797
9798 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009800 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009801 ASSERT_EQ(2U, args.pointerCount);
9802 ASSERT_EQ(0, args.pointerProperties[0].id);
9803 ASSERT_EQ(1, args.pointerProperties[1].id);
9804 ASSERT_NO_FATAL_FAILURE(
9805 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9806 ASSERT_NO_FATAL_FAILURE(
9807 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9808
9809 // FINGER 1 MOVE
9810 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9811 processSync(mapper);
9812
9813 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9814 // from move
9815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9816 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9817 ASSERT_NO_FATAL_FAILURE(
9818 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9819 ASSERT_NO_FATAL_FAILURE(
9820 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9821
9822 // FINGER 0 MOVE
9823 processSlot(mapper, 0);
9824 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9825 processSync(mapper);
9826
9827 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9829 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9830 ASSERT_NO_FATAL_FAILURE(
9831 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9832 ASSERT_NO_FATAL_FAILURE(
9833 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9834
9835 // BUTTON DOWN
9836 processKey(mapper, BTN_LEFT, 1);
9837 processSync(mapper);
9838
9839 // touchinputmapper design sends a move before button press
9840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9841 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9843 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9844
9845 // BUTTON UP
9846 processKey(mapper, BTN_LEFT, 0);
9847 processSync(mapper);
9848
9849 // touchinputmapper design sends a move after button release
9850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9851 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9853 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9854
9855 // FINGER 0 UP
9856 processId(mapper, -1);
9857 processSync(mapper);
9858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9859 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9860
9861 // FINGER 1 MOVE
9862 processSlot(mapper, 1);
9863 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9864 processSync(mapper);
9865
9866 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9868 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9869 ASSERT_EQ(1U, args.pointerCount);
9870 ASSERT_EQ(1, args.pointerProperties[0].id);
9871 ASSERT_NO_FATAL_FAILURE(
9872 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9873
9874 // FINGER 1 UP
9875 processId(mapper, -1);
9876 processKey(mapper, BTN_TOUCH, 0);
9877 processSync(mapper);
9878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9879 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9880
9881 // non captured touchpad should be a mouse source
9882 mFakePolicy->setPointerCapture(false);
9883 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9885 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9886}
9887
9888TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9889 std::shared_ptr<FakePointerController> fakePointerController =
9890 std::make_shared<FakePointerController>();
9891 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9892 fakePointerController->setPosition(0, 0);
9893 fakePointerController->setButtonState(0);
9894
9895 // prepare device and capture
9896 prepareDisplay(DISPLAY_ORIENTATION_0);
9897 prepareAxes(POSITION | ID | SLOT);
9898 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9899 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009900 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009901 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9902 // run uncaptured pointer tests - pushes out generic events
9903 // FINGER 0 DOWN
9904 processId(mapper, 3);
9905 processPosition(mapper, 100, 100);
9906 processKey(mapper, BTN_TOUCH, 1);
9907 processSync(mapper);
9908
9909 // start at (100,100), cursor should be at (0,0) * scale
9910 NotifyMotionArgs args;
9911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9912 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9913 ASSERT_NO_FATAL_FAILURE(
9914 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9915
9916 // FINGER 0 MOVE
9917 processPosition(mapper, 200, 200);
9918 processSync(mapper);
9919
9920 // compute scaling to help with touch position checking
9921 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9922 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9923 float scale =
9924 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9925
9926 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9928 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9930 0, 0, 0, 0, 0, 0, 0));
9931}
9932
9933TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9934 std::shared_ptr<FakePointerController> fakePointerController =
9935 std::make_shared<FakePointerController>();
9936
9937 prepareDisplay(DISPLAY_ORIENTATION_0);
9938 prepareAxes(POSITION | ID | SLOT);
9939 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009940 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009941 mFakePolicy->setPointerCapture(false);
9942 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9943
9944 // uncaptured touchpad should be a pointer device
9945 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9946
9947 // captured touchpad should be a touchpad device
9948 mFakePolicy->setPointerCapture(true);
9949 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9950 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9951}
9952
HQ Liue6983c72022-04-19 22:14:56 +00009953class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9954protected:
9955 float mPointerMovementScale;
9956 float mPointerXZoomScale;
9957 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9958 addConfigurationProperty("touch.deviceType", "pointer");
9959 std::shared_ptr<FakePointerController> fakePointerController =
9960 std::make_shared<FakePointerController>();
9961 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9962 fakePointerController->setPosition(0, 0);
9963 fakePointerController->setButtonState(0);
9964 prepareDisplay(DISPLAY_ORIENTATION_0);
9965
9966 prepareAxes(POSITION);
9967 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9968 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9969 // needs to be disabled, and the pointer gesture needs to be enabled.
9970 mFakePolicy->setPointerCapture(false);
9971 mFakePolicy->setPointerGestureEnabled(true);
9972 mFakePolicy->setPointerController(fakePointerController);
9973
9974 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9975 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9976 mPointerMovementScale =
9977 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9978 mPointerXZoomScale =
9979 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9980 }
9981
9982 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9983 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9984 /*flat*/ 0,
9985 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9986 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9987 /*flat*/ 0,
9988 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9989 }
9990};
9991
9992/**
9993 * Two fingers down on a pointer mode touch pad. The width
9994 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9995 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9996 * be greater than the both value to be freeform gesture, so that after two
9997 * fingers start to move downwards, the gesture should be swipe.
9998 */
9999TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10000 // The min freeform gesture width is 25units/mm x 30mm = 750
10001 // which is greater than fraction of the diagnal length of the touchpad (349).
10002 // Thus, MaxSwipWidth is 750.
10003 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10004 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10005 NotifyMotionArgs motionArgs;
10006
10007 // Two fingers down at once.
10008 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10009 // Pointer's initial position is used the [0,0] coordinate.
10010 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10011
10012 processId(mapper, FIRST_TRACKING_ID);
10013 processPosition(mapper, x1, y1);
10014 processMTSync(mapper);
10015 processId(mapper, SECOND_TRACKING_ID);
10016 processPosition(mapper, x2, y2);
10017 processMTSync(mapper);
10018 processSync(mapper);
10019
10020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10021 ASSERT_EQ(1U, motionArgs.pointerCount);
10022 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10023 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010024 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010025 ASSERT_NO_FATAL_FAILURE(
10026 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10027
10028 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10029 // that there should be 1 pointer.
10030 int32_t movingDistance = 200;
10031 y1 += movingDistance;
10032 y2 += movingDistance;
10033
10034 processId(mapper, FIRST_TRACKING_ID);
10035 processPosition(mapper, x1, y1);
10036 processMTSync(mapper);
10037 processId(mapper, SECOND_TRACKING_ID);
10038 processPosition(mapper, x2, y2);
10039 processMTSync(mapper);
10040 processSync(mapper);
10041
10042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10043 ASSERT_EQ(1U, motionArgs.pointerCount);
10044 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10045 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010046 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010047 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10048 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10049 0, 0, 0, 0));
10050}
10051
10052/**
10053 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10054 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10055 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10056 * value to be freeform gesture, so that after two fingers start to move downwards,
10057 * the gesture should be swipe.
10058 */
10059TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10060 // The min freeform gesture width is 5units/mm x 30mm = 150
10061 // which is greater than fraction of the diagnal length of the touchpad (349).
10062 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
10063 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
10064 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10065 NotifyMotionArgs motionArgs;
10066
10067 // Two fingers down at once.
10068 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10069 // Pointer's initial position is used the [0,0] coordinate.
10070 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10071
10072 processId(mapper, FIRST_TRACKING_ID);
10073 processPosition(mapper, x1, y1);
10074 processMTSync(mapper);
10075 processId(mapper, SECOND_TRACKING_ID);
10076 processPosition(mapper, x2, y2);
10077 processMTSync(mapper);
10078 processSync(mapper);
10079
10080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10081 ASSERT_EQ(1U, motionArgs.pointerCount);
10082 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10083 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010084 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010085 ASSERT_NO_FATAL_FAILURE(
10086 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10087
10088 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10089 // and there should be 1 pointer.
10090 int32_t movingDistance = 200;
10091 y1 += movingDistance;
10092 y2 += movingDistance;
10093
10094 processId(mapper, FIRST_TRACKING_ID);
10095 processPosition(mapper, x1, y1);
10096 processMTSync(mapper);
10097 processId(mapper, SECOND_TRACKING_ID);
10098 processPosition(mapper, x2, y2);
10099 processMTSync(mapper);
10100 processSync(mapper);
10101
10102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10103 ASSERT_EQ(1U, motionArgs.pointerCount);
10104 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10105 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010106 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010107 // New coordinate is the scaled relative coordinate from the initial coordinate.
10108 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10109 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10110 0, 0, 0, 0));
10111}
10112
10113/**
10114 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10115 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10116 * freeform gestures after two fingers start to move downwards.
10117 */
10118TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
10119 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10120 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10121
10122 NotifyMotionArgs motionArgs;
10123
10124 // Two fingers down at once. Wider than the max swipe width.
10125 // The gesture is expected to be PRESS, then transformed to FREEFORM
10126 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10127
10128 processId(mapper, FIRST_TRACKING_ID);
10129 processPosition(mapper, x1, y1);
10130 processMTSync(mapper);
10131 processId(mapper, SECOND_TRACKING_ID);
10132 processPosition(mapper, x2, y2);
10133 processMTSync(mapper);
10134 processSync(mapper);
10135
10136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10137 ASSERT_EQ(1U, motionArgs.pointerCount);
10138 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10139 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010140 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010141 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10142 ASSERT_NO_FATAL_FAILURE(
10143 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10144
10145 int32_t movingDistance = 200;
10146
10147 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10148 // then two down events for two pointers.
10149 y1 += movingDistance;
10150 y2 += movingDistance;
10151
10152 processId(mapper, FIRST_TRACKING_ID);
10153 processPosition(mapper, x1, y1);
10154 processMTSync(mapper);
10155 processId(mapper, SECOND_TRACKING_ID);
10156 processPosition(mapper, x2, y2);
10157 processMTSync(mapper);
10158 processSync(mapper);
10159
10160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10161 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10162 ASSERT_EQ(1U, motionArgs.pointerCount);
10163 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10165 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10166 ASSERT_EQ(1U, motionArgs.pointerCount);
10167 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10169 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010170 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010171 ASSERT_EQ(2U, motionArgs.pointerCount);
10172 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
10173 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010174 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010175 // Two pointers' scaled relative coordinates from their initial centroid.
10176 // Initial y coordinates are 0 as y1 and y2 have the same value.
10177 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10178 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10179 // When pointers move, the new coordinates equal to the initial coordinates plus
10180 // scaled moving distance.
10181 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10182 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10183 0, 0, 0, 0));
10184 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10185 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10186 0, 0, 0, 0));
10187
10188 // Move two fingers down again, expect one MOVE motion event.
10189 y1 += movingDistance;
10190 y2 += movingDistance;
10191
10192 processId(mapper, FIRST_TRACKING_ID);
10193 processPosition(mapper, x1, y1);
10194 processMTSync(mapper);
10195 processId(mapper, SECOND_TRACKING_ID);
10196 processPosition(mapper, x2, y2);
10197 processMTSync(mapper);
10198 processSync(mapper);
10199
10200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10201 ASSERT_EQ(2U, motionArgs.pointerCount);
10202 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10203 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010204 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010205 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10206 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10207 0, 0, 0, 0, 0));
10208 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10209 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10210 0, 0, 0, 0, 0));
10211}
10212
Harry Cutts39b7ca22022-10-05 15:55:48 +000010213TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
10214 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10215 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10216 NotifyMotionArgs motionArgs;
10217
10218 // Place two fingers down.
10219 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10220
10221 processId(mapper, FIRST_TRACKING_ID);
10222 processPosition(mapper, x1, y1);
10223 processMTSync(mapper);
10224 processId(mapper, SECOND_TRACKING_ID);
10225 processPosition(mapper, x2, y2);
10226 processMTSync(mapper);
10227 processSync(mapper);
10228
10229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10230 ASSERT_EQ(1U, motionArgs.pointerCount);
10231 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10232 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10233 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10234 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10235
10236 // Move the two fingers down and to the left.
10237 int32_t movingDistance = 200;
10238 x1 -= movingDistance;
10239 y1 += movingDistance;
10240 x2 -= movingDistance;
10241 y2 += movingDistance;
10242
10243 processId(mapper, FIRST_TRACKING_ID);
10244 processPosition(mapper, x1, y1);
10245 processMTSync(mapper);
10246 processId(mapper, SECOND_TRACKING_ID);
10247 processPosition(mapper, x2, y2);
10248 processMTSync(mapper);
10249 processSync(mapper);
10250
10251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10252 ASSERT_EQ(1U, motionArgs.pointerCount);
10253 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10254 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10255 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10256 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10257}
10258
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010259// --- JoystickInputMapperTest ---
10260
10261class JoystickInputMapperTest : public InputMapperTest {
10262protected:
10263 static const int32_t RAW_X_MIN;
10264 static const int32_t RAW_X_MAX;
10265 static const int32_t RAW_Y_MIN;
10266 static const int32_t RAW_Y_MAX;
10267
10268 void SetUp() override {
10269 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10270 }
10271 void prepareAxes() {
10272 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10273 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10274 }
10275
10276 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10277 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10278 }
10279
10280 void processSync(JoystickInputMapper& mapper) {
10281 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10282 }
10283
10284 void prepareVirtualDisplay(int32_t orientation) {
10285 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10286 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10287 NO_PORT, ViewportType::VIRTUAL);
10288 }
10289};
10290
10291const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10292const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10293const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10294const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10295
10296TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10297 prepareAxes();
10298 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10299
10300 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10301
10302 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
10303
10304 // Send an axis event
10305 processAxis(mapper, ABS_X, 100);
10306 processSync(mapper);
10307
10308 NotifyMotionArgs args;
10309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10310 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10311
10312 // Send another axis event
10313 processAxis(mapper, ABS_Y, 100);
10314 processSync(mapper);
10315
10316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10317 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10318}
10319
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010320// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010321
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010322class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010323protected:
10324 static const char* DEVICE_NAME;
10325 static const char* DEVICE_LOCATION;
10326 static const int32_t DEVICE_ID;
10327 static const int32_t DEVICE_GENERATION;
10328 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010329 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010330 static const int32_t EVENTHUB_ID;
10331
10332 std::shared_ptr<FakeEventHub> mFakeEventHub;
10333 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010334 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010335 std::unique_ptr<InstrumentedInputReader> mReader;
10336 std::shared_ptr<InputDevice> mDevice;
10337
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010338 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010339 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010340 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010341 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010342 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010343 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010344 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10345 }
10346
10347 void SetUp() override { SetUp(DEVICE_CLASSES); }
10348
10349 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010350 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010351 mFakePolicy.clear();
10352 }
10353
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010354 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010355 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10356 mReader->requestRefreshConfiguration(changes);
10357 mReader->loopOnce();
10358 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010359 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010360 }
10361
10362 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10363 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010364 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010365 InputDeviceIdentifier identifier;
10366 identifier.name = name;
10367 identifier.location = location;
10368 std::shared_ptr<InputDevice> device =
10369 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10370 identifier);
10371 mReader->pushNextDevice(device);
10372 mFakeEventHub->addDevice(eventHubId, name, classes);
10373 mReader->loopOnce();
10374 return device;
10375 }
10376
10377 template <class T, typename... Args>
10378 T& addControllerAndConfigure(Args... args) {
10379 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10380
10381 return controller;
10382 }
10383};
10384
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010385const char* PeripheralControllerTest::DEVICE_NAME = "device";
10386const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10387const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10388const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10389const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010390const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10391 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010392const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010393
10394// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010395class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010396protected:
10397 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010398 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010399 }
10400};
10401
10402TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010403 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010404
10405 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
10406 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
10407}
10408
10409TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010410 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010411
10412 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
10413 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
10414}
10415
10416// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010417class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010418protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010419 void SetUp() override {
10420 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10421 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010422};
10423
Chris Ye85758332021-05-16 23:05:17 -070010424TEST_F(LightControllerTest, MonoLight) {
10425 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010426 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010427 .maxBrightness = 255,
10428 .flags = InputLightClass::BRIGHTNESS,
10429 .path = ""};
10430 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010431
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010432 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010433 InputDeviceInfo info;
10434 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010435 std::vector<InputDeviceLightInfo> lights = info.getLights();
10436 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010437 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10438 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10439
10440 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10441 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10442}
10443
10444TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10445 RawLightInfo infoMono = {.id = 1,
10446 .name = "mono_keyboard_backlight",
10447 .maxBrightness = 255,
10448 .flags = InputLightClass::BRIGHTNESS |
10449 InputLightClass::KEYBOARD_BACKLIGHT,
10450 .path = ""};
10451 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10452
10453 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10454 InputDeviceInfo info;
10455 controller.populateDeviceInfo(&info);
10456 std::vector<InputDeviceLightInfo> lights = info.getLights();
10457 ASSERT_EQ(1U, lights.size());
10458 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10459 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010460
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010461 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10462 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010463}
10464
10465TEST_F(LightControllerTest, RGBLight) {
10466 RawLightInfo infoRed = {.id = 1,
10467 .name = "red",
10468 .maxBrightness = 255,
10469 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10470 .path = ""};
10471 RawLightInfo infoGreen = {.id = 2,
10472 .name = "green",
10473 .maxBrightness = 255,
10474 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10475 .path = ""};
10476 RawLightInfo infoBlue = {.id = 3,
10477 .name = "blue",
10478 .maxBrightness = 255,
10479 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10480 .path = ""};
10481 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10482 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10483 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10484
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010485 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010486 InputDeviceInfo info;
10487 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010488 std::vector<InputDeviceLightInfo> lights = info.getLights();
10489 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010490 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10491 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10492 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10493
10494 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10495 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10496}
10497
10498TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10499 RawLightInfo infoRed = {.id = 1,
10500 .name = "red_keyboard_backlight",
10501 .maxBrightness = 255,
10502 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10503 InputLightClass::KEYBOARD_BACKLIGHT,
10504 .path = ""};
10505 RawLightInfo infoGreen = {.id = 2,
10506 .name = "green_keyboard_backlight",
10507 .maxBrightness = 255,
10508 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10509 InputLightClass::KEYBOARD_BACKLIGHT,
10510 .path = ""};
10511 RawLightInfo infoBlue = {.id = 3,
10512 .name = "blue_keyboard_backlight",
10513 .maxBrightness = 255,
10514 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10515 InputLightClass::KEYBOARD_BACKLIGHT,
10516 .path = ""};
10517 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10518 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10519 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10520
10521 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10522 InputDeviceInfo info;
10523 controller.populateDeviceInfo(&info);
10524 std::vector<InputDeviceLightInfo> lights = info.getLights();
10525 ASSERT_EQ(1U, lights.size());
10526 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10527 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10528 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10529
10530 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10531 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10532}
10533
10534TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10535 RawLightInfo infoRed = {.id = 1,
10536 .name = "red",
10537 .maxBrightness = 255,
10538 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10539 .path = ""};
10540 RawLightInfo infoGreen = {.id = 2,
10541 .name = "green",
10542 .maxBrightness = 255,
10543 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10544 .path = ""};
10545 RawLightInfo infoBlue = {.id = 3,
10546 .name = "blue",
10547 .maxBrightness = 255,
10548 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10549 .path = ""};
10550 RawLightInfo infoGlobal = {.id = 3,
10551 .name = "global_keyboard_backlight",
10552 .maxBrightness = 255,
10553 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10554 InputLightClass::KEYBOARD_BACKLIGHT,
10555 .path = ""};
10556 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10557 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10558 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10559 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10560
10561 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10562 InputDeviceInfo info;
10563 controller.populateDeviceInfo(&info);
10564 std::vector<InputDeviceLightInfo> lights = info.getLights();
10565 ASSERT_EQ(1U, lights.size());
10566 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10567 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10568 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010569
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010570 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10571 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010572}
10573
10574TEST_F(LightControllerTest, MultiColorRGBLight) {
10575 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010576 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010577 .maxBrightness = 255,
10578 .flags = InputLightClass::BRIGHTNESS |
10579 InputLightClass::MULTI_INTENSITY |
10580 InputLightClass::MULTI_INDEX,
10581 .path = ""};
10582
10583 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10584
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010585 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010586 InputDeviceInfo info;
10587 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010588 std::vector<InputDeviceLightInfo> lights = info.getLights();
10589 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010590 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10591 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10592 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10593
10594 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10595 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10596}
10597
10598TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10599 RawLightInfo infoColor = {.id = 1,
10600 .name = "multi_color_keyboard_backlight",
10601 .maxBrightness = 255,
10602 .flags = InputLightClass::BRIGHTNESS |
10603 InputLightClass::MULTI_INTENSITY |
10604 InputLightClass::MULTI_INDEX |
10605 InputLightClass::KEYBOARD_BACKLIGHT,
10606 .path = ""};
10607
10608 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10609
10610 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10611 InputDeviceInfo info;
10612 controller.populateDeviceInfo(&info);
10613 std::vector<InputDeviceLightInfo> lights = info.getLights();
10614 ASSERT_EQ(1U, lights.size());
10615 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10616 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10617 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010618
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010619 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10620 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010621}
10622
10623TEST_F(LightControllerTest, PlayerIdLight) {
10624 RawLightInfo info1 = {.id = 1,
10625 .name = "player1",
10626 .maxBrightness = 255,
10627 .flags = InputLightClass::BRIGHTNESS,
10628 .path = ""};
10629 RawLightInfo info2 = {.id = 2,
10630 .name = "player2",
10631 .maxBrightness = 255,
10632 .flags = InputLightClass::BRIGHTNESS,
10633 .path = ""};
10634 RawLightInfo info3 = {.id = 3,
10635 .name = "player3",
10636 .maxBrightness = 255,
10637 .flags = InputLightClass::BRIGHTNESS,
10638 .path = ""};
10639 RawLightInfo info4 = {.id = 4,
10640 .name = "player4",
10641 .maxBrightness = 255,
10642 .flags = InputLightClass::BRIGHTNESS,
10643 .path = ""};
10644 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10645 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10646 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10647 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10648
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010649 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010650 InputDeviceInfo info;
10651 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010652 std::vector<InputDeviceLightInfo> lights = info.getLights();
10653 ASSERT_EQ(1U, lights.size());
10654 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010655 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10656 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010657
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010658 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10659 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10660 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010661}
10662
Michael Wrightd02c5b62014-02-10 15:10:22 -080010663} // namespace android