blob: 54bd49538a5f393c615ed9cbdc0848dbc2dd172a [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
19
Prabir Pradhan2770d242019-09-02 18:07:11 -070020#include <CursorInputMapper.h>
21#include <InputDevice.h>
22#include <InputMapper.h>
23#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080024#include <InputReaderBase.h>
25#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000026#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070027#include <KeyboardInputMapper.h>
28#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070029#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070030#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070031#include <SingleTouchInputMapper.h>
32#include <SwitchInputMapper.h>
33#include <TestInputListener.h>
Prabir Pradhan739dca42022-09-09 20:12:01 +000034#include <TestInputListenerMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070035#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080036#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000037#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070038#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080039#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050040#include <gui/constants.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080041
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000042#include "android/hardware/input/InputDeviceCountryCode.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000043#include "input/DisplayViewport.h"
44#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010045
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000046using android::hardware::input::InputDeviceCountryCode;
47
Michael Wrightd02c5b62014-02-10 15:10:22 -080048namespace android {
49
Dominik Laskowski2f01d772022-03-23 16:01:29 -070050using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000051using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070052using std::chrono_literals::operator""ms;
53
54// Timeout for waiting for an expected event
55static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
56
Michael Wrightd02c5b62014-02-10 15:10:22 -080057// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000058static constexpr nsecs_t ARBITRARY_TIME = 1234;
59static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080060
61// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080062static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000063static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080064static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000065static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080066static constexpr int32_t DISPLAY_WIDTH = 480;
67static constexpr int32_t DISPLAY_HEIGHT = 800;
68static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
69static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
70static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070071static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070072static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080073
arthurhungcc7f9802020-04-30 17:55:40 +080074static constexpr int32_t FIRST_SLOT = 0;
75static constexpr int32_t SECOND_SLOT = 1;
76static constexpr int32_t THIRD_SLOT = 2;
77static constexpr int32_t INVALID_TRACKING_ID = -1;
78static constexpr int32_t FIRST_TRACKING_ID = 0;
79static constexpr int32_t SECOND_TRACKING_ID = 1;
80static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080081static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080082static constexpr int32_t BATTERY_STATUS = 4;
83static constexpr int32_t BATTERY_CAPACITY = 66;
Prabir Pradhane287ecd2022-09-07 21:18:05 +000084static const std::string BATTERY_DEVPATH = "/sys/devices/mydevice/power_supply/mybattery";
Chris Ye3fdbfef2021-01-06 18:45:18 -080085static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
86static constexpr int32_t LIGHT_COLOR = 0x7F448866;
87static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080088
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080089static constexpr int32_t ACTION_POINTER_0_DOWN =
90 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
91static constexpr int32_t ACTION_POINTER_0_UP =
92 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
93static constexpr int32_t ACTION_POINTER_1_DOWN =
94 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
95static constexpr int32_t ACTION_POINTER_1_UP =
96 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
97
Michael Wrightd02c5b62014-02-10 15:10:22 -080098// Error tolerance for floating point assertions.
99static const float EPSILON = 0.001f;
100
101template<typename T>
102static inline T min(T a, T b) {
103 return a < b ? a : b;
104}
105
106static inline float avg(float x, float y) {
107 return (x + y) / 2;
108}
109
Chris Ye3fdbfef2021-01-06 18:45:18 -0800110// Mapping for light color name and the light color
111const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
112 {"green", LightColor::GREEN},
113 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800114
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700115static int32_t getInverseRotation(int32_t orientation) {
116 switch (orientation) {
117 case DISPLAY_ORIENTATION_90:
118 return DISPLAY_ORIENTATION_270;
119 case DISPLAY_ORIENTATION_270:
120 return DISPLAY_ORIENTATION_90;
121 default:
122 return orientation;
123 }
124}
125
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800126static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
127 InputDeviceInfo info;
128 mapper.populateDeviceInfo(&info);
129
130 const InputDeviceInfo::MotionRange* motionRange =
131 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
132 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
133}
134
135static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
136 InputDeviceInfo info;
137 mapper.populateDeviceInfo(&info);
138
139 const InputDeviceInfo::MotionRange* motionRange =
140 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
141 ASSERT_EQ(nullptr, motionRange);
142}
143
Michael Wrightd02c5b62014-02-10 15:10:22 -0800144// --- FakePointerController ---
145
146class FakePointerController : public PointerControllerInterface {
147 bool mHaveBounds;
148 float mMinX, mMinY, mMaxX, mMaxY;
149 float mX, mY;
150 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800151 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152
Michael Wrightd02c5b62014-02-10 15:10:22 -0800153public:
154 FakePointerController() :
155 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800156 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800157 }
158
Michael Wright17db18e2020-06-26 20:51:44 +0100159 virtual ~FakePointerController() {}
160
Michael Wrightd02c5b62014-02-10 15:10:22 -0800161 void setBounds(float minX, float minY, float maxX, float maxY) {
162 mHaveBounds = true;
163 mMinX = minX;
164 mMinY = minY;
165 mMaxX = maxX;
166 mMaxY = maxY;
167 }
168
Chris Yea52ade12020-08-27 16:49:20 -0700169 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170 mX = x;
171 mY = y;
172 }
173
Chris Yea52ade12020-08-27 16:49:20 -0700174 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800175
Chris Yea52ade12020-08-27 16:49:20 -0700176 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800177
Chris Yea52ade12020-08-27 16:49:20 -0700178 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800179 *outX = mX;
180 *outY = mY;
181 }
182
Chris Yea52ade12020-08-27 16:49:20 -0700183 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800184
Chris Yea52ade12020-08-27 16:49:20 -0700185 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800186 mDisplayId = viewport.displayId;
187 }
188
Arthur Hung7c645402019-01-25 17:45:42 +0800189 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
190 return mSpotsByDisplay;
191 }
192
Michael Wrightd02c5b62014-02-10 15:10:22 -0800193private:
Chris Yea52ade12020-08-27 16:49:20 -0700194 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800195 *outMinX = mMinX;
196 *outMinY = mMinY;
197 *outMaxX = mMaxX;
198 *outMaxY = mMaxY;
199 return mHaveBounds;
200 }
201
Chris Yea52ade12020-08-27 16:49:20 -0700202 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800203 mX += deltaX;
204 if (mX < mMinX) mX = mMinX;
205 if (mX > mMaxX) mX = mMaxX;
206 mY += deltaY;
207 if (mY < mMinY) mY = mMinY;
208 if (mY > mMaxY) mY = mMaxY;
209 }
210
Chris Yea52ade12020-08-27 16:49:20 -0700211 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800212
Chris Yea52ade12020-08-27 16:49:20 -0700213 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800214
Chris Yea52ade12020-08-27 16:49:20 -0700215 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800216
Chris Yea52ade12020-08-27 16:49:20 -0700217 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
218 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800219 std::vector<int32_t> newSpots;
220 // Add spots for fingers that are down.
221 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
222 uint32_t id = idBits.clearFirstMarkedBit();
223 newSpots.push_back(id);
224 }
225
226 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800227 }
228
Prabir Pradhan197e0862022-07-01 14:28:00 +0000229 void clearSpots() override { mSpotsByDisplay.clear(); }
Arthur Hung7c645402019-01-25 17:45:42 +0800230
231 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800232};
233
234
235// --- FakeInputReaderPolicy ---
236
237class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700238 std::mutex mLock;
239 std::condition_variable mDevicesChangedCondition;
240
Michael Wrightd02c5b62014-02-10 15:10:22 -0800241 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000242 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700243 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
244 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100245 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700246 TouchAffineTransformation transform;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000247 std::optional<int32_t /*deviceId*/> mStylusGestureNotified GUARDED_BY(mLock){};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800248
249protected:
Chris Yea52ade12020-08-27 16:49:20 -0700250 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800251
252public:
253 FakeInputReaderPolicy() {
254 }
255
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700256 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800257 waitForInputDevices([](bool devicesChanged) {
258 if (!devicesChanged) {
259 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
260 }
261 });
262 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700263
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800264 void assertInputDevicesNotChanged() {
265 waitForInputDevices([](bool devicesChanged) {
266 if (devicesChanged) {
267 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
268 }
269 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700270 }
271
Prabir Pradhanda20b172022-09-26 17:01:18 +0000272 void assertStylusGestureNotified(int32_t deviceId) {
273 std::scoped_lock lock(mLock);
274 ASSERT_TRUE(mStylusGestureNotified);
275 ASSERT_EQ(deviceId, *mStylusGestureNotified);
276 mStylusGestureNotified.reset();
277 }
278
279 void assertStylusGestureNotNotified() {
280 std::scoped_lock lock(mLock);
281 ASSERT_FALSE(mStylusGestureNotified);
282 }
283
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700284 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100285 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100286 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700287 }
288
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700289 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
290 return mConfig.getDisplayViewportByUniqueId(uniqueId);
291 }
292 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
293 return mConfig.getDisplayViewportByType(type);
294 }
295
296 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
297 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700298 }
299
Prabir Pradhan5632d622021-09-06 07:57:20 -0700300 void addDisplayViewport(DisplayViewport viewport) {
301 mViewports.push_back(std::move(viewport));
302 mConfig.setDisplayViewports(mViewports);
303 }
304
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700305 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000306 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700307 std::optional<uint8_t> physicalPort, ViewportType type) {
308 const bool isRotated =
309 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
310 DisplayViewport v;
311 v.displayId = displayId;
312 v.orientation = orientation;
313 v.logicalLeft = 0;
314 v.logicalTop = 0;
315 v.logicalRight = isRotated ? height : width;
316 v.logicalBottom = isRotated ? width : height;
317 v.physicalLeft = 0;
318 v.physicalTop = 0;
319 v.physicalRight = isRotated ? height : width;
320 v.physicalBottom = isRotated ? width : height;
321 v.deviceWidth = isRotated ? height : width;
322 v.deviceHeight = isRotated ? width : height;
323 v.isActive = isActive;
324 v.uniqueId = uniqueId;
325 v.physicalPort = physicalPort;
326 v.type = type;
327
328 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329 }
330
Arthur Hung6cd19a42019-08-30 19:04:12 +0800331 bool updateViewport(const DisplayViewport& viewport) {
332 size_t count = mViewports.size();
333 for (size_t i = 0; i < count; i++) {
334 const DisplayViewport& currentViewport = mViewports[i];
335 if (currentViewport.displayId == viewport.displayId) {
336 mViewports[i] = viewport;
337 mConfig.setDisplayViewports(mViewports);
338 return true;
339 }
340 }
341 // no viewport found.
342 return false;
343 }
344
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100345 void addExcludedDeviceName(const std::string& deviceName) {
346 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800347 }
348
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700349 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
350 mConfig.portAssociations.insert({inputPort, displayPort});
351 }
352
Christine Franks1ba71cc2021-04-07 14:37:42 -0700353 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
354 const std::string& displayUniqueId) {
355 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
356 }
357
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000358 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700359
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000360 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700361
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000362 void setPointerController(std::shared_ptr<FakePointerController> controller) {
363 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800364 }
365
366 const InputReaderConfiguration* getReaderConfiguration() const {
367 return &mConfig;
368 }
369
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800370 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800371 return mInputDevices;
372 }
373
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100374 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700375 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700376 return transform;
377 }
378
379 void setTouchAffineTransformation(const TouchAffineTransformation t) {
380 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800381 }
382
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000383 PointerCaptureRequest setPointerCapture(bool enabled) {
384 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
385 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800386 }
387
Arthur Hung7c645402019-01-25 17:45:42 +0800388 void setShowTouches(bool enabled) {
389 mConfig.showTouches = enabled;
390 }
391
Garfield Tan888a6a42020-01-09 11:39:16 -0800392 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
393 mConfig.defaultPointerDisplayId = pointerDisplayId;
394 }
395
HQ Liue6983c72022-04-19 22:14:56 +0000396 void setPointerGestureEnabled(bool enabled) { mConfig.pointerGesturesEnabled = enabled; }
397
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800398 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
399
HQ Liue6983c72022-04-19 22:14:56 +0000400 float getPointerGestureZoomSpeedRatio() { return mConfig.pointerGestureZoomSpeedRatio; }
401
Prabir Pradhanf99d6e72022-04-21 15:28:35 +0000402 void setVelocityControlParams(const VelocityControlParameters& params) {
403 mConfig.pointerVelocityControlParameters = params;
404 mConfig.wheelVelocityControlParameters = params;
405 }
406
Michael Wrightd02c5b62014-02-10 15:10:22 -0800407private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000408 uint32_t mNextPointerCaptureSequenceNumber = 0;
409
Chris Yea52ade12020-08-27 16:49:20 -0700410 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800411 *outConfig = mConfig;
412 }
413
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000414 std::shared_ptr<PointerControllerInterface> obtainPointerController(
415 int32_t /*deviceId*/) override {
416 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800417 }
418
Chris Yea52ade12020-08-27 16:49:20 -0700419 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700420 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800421 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700422 mInputDevicesChanged = true;
423 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800424 }
425
Chris Yea52ade12020-08-27 16:49:20 -0700426 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
427 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700428 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800429 }
430
Chris Yea52ade12020-08-27 16:49:20 -0700431 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800432
433 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
434 std::unique_lock<std::mutex> lock(mLock);
435 base::ScopedLockAssertion assumeLocked(mLock);
436
437 const bool devicesChanged =
438 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
439 return mInputDevicesChanged;
440 });
441 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
442 mInputDevicesChanged = false;
443 }
Prabir Pradhanda20b172022-09-26 17:01:18 +0000444
445 void notifyStylusGestureStarted(int32_t deviceId, nsecs_t eventTime) override {
446 std::scoped_lock<std::mutex> lock(mLock);
447 mStylusGestureNotified = deviceId;
448 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800449};
450
Michael Wrightd02c5b62014-02-10 15:10:22 -0800451// --- FakeEventHub ---
452
453class FakeEventHub : public EventHubInterface {
454 struct KeyInfo {
455 int32_t keyCode;
456 uint32_t flags;
457 };
458
Chris Yef59a2f42020-10-16 12:55:26 -0700459 struct SensorInfo {
460 InputDeviceSensorType sensorType;
461 int32_t sensorDataIndex;
462 };
463
Michael Wrightd02c5b62014-02-10 15:10:22 -0800464 struct Device {
465 InputDeviceIdentifier identifier;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700466 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800467 PropertyMap configuration;
468 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
469 KeyedVector<int, bool> relativeAxes;
470 KeyedVector<int32_t, int32_t> keyCodeStates;
471 KeyedVector<int32_t, int32_t> scanCodeStates;
472 KeyedVector<int32_t, int32_t> switchStates;
473 KeyedVector<int32_t, int32_t> absoluteAxisValue;
474 KeyedVector<int32_t, KeyInfo> keysByScanCode;
475 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
476 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100477 // fake mapping which would normally come from keyCharacterMap
478 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700479 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
480 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800481 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700482 bool enabled;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000483 InputDeviceCountryCode countryCode;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700484
485 status_t enable() {
486 enabled = true;
487 return OK;
488 }
489
490 status_t disable() {
491 enabled = false;
492 return OK;
493 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800494
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700495 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800496 };
497
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700498 std::mutex mLock;
499 std::condition_variable mEventsCondition;
500
Michael Wrightd02c5b62014-02-10 15:10:22 -0800501 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100502 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000503 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600504 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000505 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800506 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
507 // Simulates a device light brightness, from light id to light brightness.
508 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
509 // Simulates a device light intensities, from light id to light intensities map.
510 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
511 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800512
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700513public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800514 virtual ~FakeEventHub() {
515 for (size_t i = 0; i < mDevices.size(); i++) {
516 delete mDevices.valueAt(i);
517 }
518 }
519
Michael Wrightd02c5b62014-02-10 15:10:22 -0800520 FakeEventHub() { }
521
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700522 void addDevice(int32_t deviceId, const std::string& name,
523 ftl::Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800524 Device* device = new Device(classes);
525 device->identifier.name = name;
526 mDevices.add(deviceId, device);
527
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000528 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800529 }
530
531 void removeDevice(int32_t deviceId) {
532 delete mDevices.valueFor(deviceId);
533 mDevices.removeItem(deviceId);
534
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000535 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800536 }
537
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000538 bool isDeviceEnabled(int32_t deviceId) const override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700539 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700540 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700541 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
542 return false;
543 }
544 return device->enabled;
545 }
546
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000547 status_t enableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700548 status_t result;
549 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700550 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700551 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
552 return BAD_VALUE;
553 }
554 if (device->enabled) {
555 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
556 return OK;
557 }
558 result = device->enable();
559 return result;
560 }
561
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000562 status_t disableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700563 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700564 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700565 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
566 return BAD_VALUE;
567 }
568 if (!device->enabled) {
569 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
570 return OK;
571 }
572 return device->disable();
573 }
574
Michael Wrightd02c5b62014-02-10 15:10:22 -0800575 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000576 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800577 }
578
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700579 void addConfigurationProperty(int32_t deviceId, const char* key, const char* value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800580 Device* device = getDevice(deviceId);
581 device->configuration.addProperty(key, value);
582 }
583
584 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
585 Device* device = getDevice(deviceId);
586 device->configuration.addAll(configuration);
587 }
588
589 void addAbsoluteAxis(int32_t deviceId, int axis,
590 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
591 Device* device = getDevice(deviceId);
592
593 RawAbsoluteAxisInfo info;
594 info.valid = true;
595 info.minValue = minValue;
596 info.maxValue = maxValue;
597 info.flat = flat;
598 info.fuzz = fuzz;
599 info.resolution = resolution;
600 device->absoluteAxes.add(axis, info);
601 }
602
603 void addRelativeAxis(int32_t deviceId, int32_t axis) {
604 Device* device = getDevice(deviceId);
605 device->relativeAxes.add(axis, true);
606 }
607
608 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
609 Device* device = getDevice(deviceId);
610 device->keyCodeStates.replaceValueFor(keyCode, state);
611 }
612
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000613 void setCountryCode(int32_t deviceId, InputDeviceCountryCode countryCode) {
614 Device* device = getDevice(deviceId);
615 device->countryCode = countryCode;
616 }
617
Michael Wrightd02c5b62014-02-10 15:10:22 -0800618 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
619 Device* device = getDevice(deviceId);
620 device->scanCodeStates.replaceValueFor(scanCode, state);
621 }
622
623 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
624 Device* device = getDevice(deviceId);
625 device->switchStates.replaceValueFor(switchCode, state);
626 }
627
628 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
629 Device* device = getDevice(deviceId);
630 device->absoluteAxisValue.replaceValueFor(axis, value);
631 }
632
633 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
634 int32_t keyCode, uint32_t flags) {
635 Device* device = getDevice(deviceId);
636 KeyInfo info;
637 info.keyCode = keyCode;
638 info.flags = flags;
639 if (scanCode) {
640 device->keysByScanCode.add(scanCode, info);
641 }
642 if (usageCode) {
643 device->keysByUsageCode.add(usageCode, info);
644 }
645 }
646
Philip Junker4af3b3d2021-12-14 10:36:55 +0100647 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
648 Device* device = getDevice(deviceId);
649 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
650 }
651
Michael Wrightd02c5b62014-02-10 15:10:22 -0800652 void addLed(int32_t deviceId, int32_t led, bool initialState) {
653 Device* device = getDevice(deviceId);
654 device->leds.add(led, initialState);
655 }
656
Chris Yef59a2f42020-10-16 12:55:26 -0700657 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
658 int32_t sensorDataIndex) {
659 Device* device = getDevice(deviceId);
660 SensorInfo info;
661 info.sensorType = sensorType;
662 info.sensorDataIndex = sensorDataIndex;
663 device->sensorsByAbsCode.emplace(absCode, info);
664 }
665
666 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
667 Device* device = getDevice(deviceId);
668 typename BitArray<MSC_MAX>::Buffer buffer;
669 buffer[mscEvent / 32] = 1 << mscEvent % 32;
670 device->mscBitmask.loadFromBuffer(buffer);
671 }
672
Chris Ye3fdbfef2021-01-06 18:45:18 -0800673 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
674 mRawLightInfos.emplace(rawId, std::move(info));
675 }
676
677 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
678 mLightBrightness.emplace(rawId, brightness);
679 }
680
681 void fakeLightIntensities(int32_t rawId,
682 const std::unordered_map<LightColor, int32_t> intensities) {
683 mLightIntensities.emplace(rawId, std::move(intensities));
684 }
685
Michael Wrightd02c5b62014-02-10 15:10:22 -0800686 bool getLedState(int32_t deviceId, int32_t led) {
687 Device* device = getDevice(deviceId);
688 return device->leds.valueFor(led);
689 }
690
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100691 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800692 return mExcludedDevices;
693 }
694
695 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
696 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800697 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800698 }
699
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000700 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
701 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700702 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800703 RawEvent event;
704 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000705 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800706 event.deviceId = deviceId;
707 event.type = type;
708 event.code = code;
709 event.value = value;
710 mEvents.push_back(event);
711
712 if (type == EV_ABS) {
713 setAbsoluteAxisValue(deviceId, code, value);
714 }
715 }
716
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600717 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
718 std::vector<TouchVideoFrame>> videoFrames) {
719 mVideoFrames = std::move(videoFrames);
720 }
721
Michael Wrightd02c5b62014-02-10 15:10:22 -0800722 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700723 std::unique_lock<std::mutex> lock(mLock);
724 base::ScopedLockAssertion assumeLocked(mLock);
725 const bool queueIsEmpty =
726 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
727 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
728 if (!queueIsEmpty) {
729 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
730 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800731 }
732
733private:
734 Device* getDevice(int32_t deviceId) const {
735 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100736 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800737 }
738
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700739 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800740 Device* device = getDevice(deviceId);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700741 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800742 }
743
Chris Yea52ade12020-08-27 16:49:20 -0700744 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800745 Device* device = getDevice(deviceId);
746 return device ? device->identifier : InputDeviceIdentifier();
747 }
748
Chris Yea52ade12020-08-27 16:49:20 -0700749 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800750
Chris Yea52ade12020-08-27 16:49:20 -0700751 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800752 Device* device = getDevice(deviceId);
753 if (device) {
754 *outConfiguration = device->configuration;
755 }
756 }
757
Chris Yea52ade12020-08-27 16:49:20 -0700758 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
759 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800760 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800761 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800762 ssize_t index = device->absoluteAxes.indexOfKey(axis);
763 if (index >= 0) {
764 *outAxisInfo = device->absoluteAxes.valueAt(index);
765 return OK;
766 }
767 }
768 outAxisInfo->clear();
769 return -1;
770 }
771
Chris Yea52ade12020-08-27 16:49:20 -0700772 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800773 Device* device = getDevice(deviceId);
774 if (device) {
775 return device->relativeAxes.indexOfKey(axis) >= 0;
776 }
777 return false;
778 }
779
Chris Yea52ade12020-08-27 16:49:20 -0700780 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800781
Chris Yef59a2f42020-10-16 12:55:26 -0700782 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
783 Device* device = getDevice(deviceId);
784 if (device) {
785 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
786 }
787 return false;
788 }
789
Chris Yea52ade12020-08-27 16:49:20 -0700790 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
791 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800792 Device* device = getDevice(deviceId);
793 if (device) {
794 const KeyInfo* key = getKey(device, scanCode, usageCode);
795 if (key) {
796 if (outKeycode) {
797 *outKeycode = key->keyCode;
798 }
799 if (outFlags) {
800 *outFlags = key->flags;
801 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700802 if (outMetaState) {
803 *outMetaState = metaState;
804 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800805 return OK;
806 }
807 }
808 return NAME_NOT_FOUND;
809 }
810
811 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
812 if (usageCode) {
813 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
814 if (index >= 0) {
815 return &device->keysByUsageCode.valueAt(index);
816 }
817 }
818 if (scanCode) {
819 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
820 if (index >= 0) {
821 return &device->keysByScanCode.valueAt(index);
822 }
823 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700824 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800825 }
826
Chris Yea52ade12020-08-27 16:49:20 -0700827 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800828
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000829 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
830 int32_t deviceId, int32_t absCode) const override {
Chris Yef59a2f42020-10-16 12:55:26 -0700831 Device* device = getDevice(deviceId);
832 if (!device) {
833 return Errorf("Sensor device not found.");
834 }
835 auto it = device->sensorsByAbsCode.find(absCode);
836 if (it == device->sensorsByAbsCode.end()) {
837 return Errorf("Sensor map not found.");
838 }
839 const SensorInfo& info = it->second;
840 return std::make_pair(info.sensorType, info.sensorDataIndex);
841 }
842
Chris Yea52ade12020-08-27 16:49:20 -0700843 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800844 mExcludedDevices = devices;
845 }
846
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700847 std::vector<RawEvent> getEvents(int) override {
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000848 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800849
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700850 std::vector<RawEvent> buffer;
851 std::swap(buffer, mEvents);
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000852
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700853 mEventsCondition.notify_all();
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700854 return buffer;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800855 }
856
Chris Yea52ade12020-08-27 16:49:20 -0700857 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600858 auto it = mVideoFrames.find(deviceId);
859 if (it != mVideoFrames.end()) {
860 std::vector<TouchVideoFrame> frames = std::move(it->second);
861 mVideoFrames.erase(deviceId);
862 return frames;
863 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800864 return {};
865 }
866
Chris Yea52ade12020-08-27 16:49:20 -0700867 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800868 Device* device = getDevice(deviceId);
869 if (device) {
870 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
871 if (index >= 0) {
872 return device->scanCodeStates.valueAt(index);
873 }
874 }
875 return AKEY_STATE_UNKNOWN;
876 }
877
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000878 InputDeviceCountryCode getCountryCode(int32_t deviceId) const override {
879 Device* device = getDevice(deviceId);
880 if (device) {
881 return device->countryCode;
882 }
883 return InputDeviceCountryCode::INVALID;
884 }
885
Chris Yea52ade12020-08-27 16:49:20 -0700886 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800887 Device* device = getDevice(deviceId);
888 if (device) {
889 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
890 if (index >= 0) {
891 return device->keyCodeStates.valueAt(index);
892 }
893 }
894 return AKEY_STATE_UNKNOWN;
895 }
896
Chris Yea52ade12020-08-27 16:49:20 -0700897 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800898 Device* device = getDevice(deviceId);
899 if (device) {
900 ssize_t index = device->switchStates.indexOfKey(sw);
901 if (index >= 0) {
902 return device->switchStates.valueAt(index);
903 }
904 }
905 return AKEY_STATE_UNKNOWN;
906 }
907
Chris Yea52ade12020-08-27 16:49:20 -0700908 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
909 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800910 Device* device = getDevice(deviceId);
911 if (device) {
912 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
913 if (index >= 0) {
914 *outValue = device->absoluteAxisValue.valueAt(index);
915 return OK;
916 }
917 }
918 *outValue = 0;
919 return -1;
920 }
921
Philip Junker4af3b3d2021-12-14 10:36:55 +0100922 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
923 Device* device = getDevice(deviceId);
924 if (!device) {
925 return AKEYCODE_UNKNOWN;
926 }
927 auto it = device->keyCodeMapping.find(locationKeyCode);
928 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
929 }
930
Chris Yea52ade12020-08-27 16:49:20 -0700931 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700932 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700933 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800934 bool result = false;
935 Device* device = getDevice(deviceId);
936 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700937 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700938 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800939 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
940 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
941 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800942 }
943 }
944 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
945 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
946 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800947 }
948 }
949 }
950 }
951 return result;
952 }
953
Chris Yea52ade12020-08-27 16:49:20 -0700954 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800955 Device* device = getDevice(deviceId);
956 if (device) {
957 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
958 return index >= 0;
959 }
960 return false;
961 }
962
Arthur Hungcb40a002021-08-03 14:31:01 +0000963 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
964 Device* device = getDevice(deviceId);
965 if (!device) {
966 return false;
967 }
968 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
969 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
970 return true;
971 }
972 }
973 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
974 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
975 return true;
976 }
977 }
978 return false;
979 }
980
Chris Yea52ade12020-08-27 16:49:20 -0700981 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800982 Device* device = getDevice(deviceId);
983 return device && device->leds.indexOfKey(led) >= 0;
984 }
985
Chris Yea52ade12020-08-27 16:49:20 -0700986 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800987 Device* device = getDevice(deviceId);
988 if (device) {
989 ssize_t index = device->leds.indexOfKey(led);
990 if (index >= 0) {
991 device->leds.replaceValueAt(led, on);
992 } else {
993 ADD_FAILURE()
994 << "Attempted to set the state of an LED that the EventHub declared "
995 "was not present. led=" << led;
996 }
997 }
998 }
999
Chris Yea52ade12020-08-27 16:49:20 -07001000 void getVirtualKeyDefinitions(
1001 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001002 outVirtualKeys.clear();
1003
1004 Device* device = getDevice(deviceId);
1005 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001006 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001007 }
1008 }
1009
Chris Yea52ade12020-08-27 16:49:20 -07001010 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -07001011 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001012 }
1013
Chris Yea52ade12020-08-27 16:49:20 -07001014 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001015 return false;
1016 }
1017
Chris Yea52ade12020-08-27 16:49:20 -07001018 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001019
Chris Yea52ade12020-08-27 16:49:20 -07001020 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001021
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001022 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override { return mVibrators; };
Chris Ye87143712020-11-10 05:05:58 +00001023
Chris Yee2b1e5c2021-03-10 22:45:12 -08001024 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
1025 return BATTERY_CAPACITY;
1026 }
Kim Low03ea0352020-11-06 12:45:07 -08001027
Chris Yee2b1e5c2021-03-10 22:45:12 -08001028 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
1029 return BATTERY_STATUS;
1030 }
1031
Andy Chenf9f1a022022-08-29 20:07:10 -04001032 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override {
1033 return {DEFAULT_BATTERY};
1034 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001035
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001036 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
1037 int32_t batteryId) const override {
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001038 if (batteryId != DEFAULT_BATTERY) return {};
1039 static const auto BATTERY_INFO = RawBatteryInfo{.id = DEFAULT_BATTERY,
1040 .name = "default battery",
1041 .flags = InputBatteryClass::CAPACITY,
1042 .path = BATTERY_DEVPATH};
1043 return BATTERY_INFO;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001044 }
Kim Low03ea0352020-11-06 12:45:07 -08001045
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001046 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001047 std::vector<int32_t> ids;
1048 for (const auto& [rawId, info] : mRawLightInfos) {
1049 ids.push_back(rawId);
1050 }
1051 return ids;
1052 }
1053
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001054 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001055 auto it = mRawLightInfos.find(lightId);
1056 if (it == mRawLightInfos.end()) {
1057 return std::nullopt;
1058 }
1059 return it->second;
1060 }
1061
1062 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1063 mLightBrightness.emplace(lightId, brightness);
1064 }
1065
1066 void setLightIntensities(int32_t deviceId, int32_t lightId,
1067 std::unordered_map<LightColor, int32_t> intensities) override {
1068 mLightIntensities.emplace(lightId, intensities);
1069 };
1070
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001071 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001072 auto lightIt = mLightBrightness.find(lightId);
1073 if (lightIt == mLightBrightness.end()) {
1074 return std::nullopt;
1075 }
1076 return lightIt->second;
1077 }
1078
1079 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001080 int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001081 auto lightIt = mLightIntensities.find(lightId);
1082 if (lightIt == mLightIntensities.end()) {
1083 return std::nullopt;
1084 }
1085 return lightIt->second;
1086 };
1087
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001088 void dump(std::string&) const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001089
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001090 void monitor() const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001091
Chris Yea52ade12020-08-27 16:49:20 -07001092 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001093
Chris Yea52ade12020-08-27 16:49:20 -07001094 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001095};
1096
Michael Wrightd02c5b62014-02-10 15:10:22 -08001097// --- FakeInputMapper ---
1098
1099class FakeInputMapper : public InputMapper {
1100 uint32_t mSources;
1101 int32_t mKeyboardType;
1102 int32_t mMetaState;
1103 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1104 KeyedVector<int32_t, int32_t> mScanCodeStates;
1105 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001106 // fake mapping which would normally come from keyCharacterMap
1107 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001108 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001109
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001110 std::mutex mLock;
1111 std::condition_variable mStateChangedCondition;
1112 bool mConfigureWasCalled GUARDED_BY(mLock);
1113 bool mResetWasCalled GUARDED_BY(mLock);
1114 bool mProcessWasCalled GUARDED_BY(mLock);
1115 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001116
Arthur Hungc23540e2018-11-29 20:42:11 +08001117 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001118public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001119 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1120 : InputMapper(deviceContext),
1121 mSources(sources),
1122 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001123 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001124 mConfigureWasCalled(false),
1125 mResetWasCalled(false),
1126 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001127
Chris Yea52ade12020-08-27 16:49:20 -07001128 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001129
1130 void setKeyboardType(int32_t keyboardType) {
1131 mKeyboardType = keyboardType;
1132 }
1133
1134 void setMetaState(int32_t metaState) {
1135 mMetaState = metaState;
1136 }
1137
1138 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001139 std::unique_lock<std::mutex> lock(mLock);
1140 base::ScopedLockAssertion assumeLocked(mLock);
1141 const bool configureCalled =
1142 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1143 return mConfigureWasCalled;
1144 });
1145 if (!configureCalled) {
1146 FAIL() << "Expected configure() to have been called.";
1147 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001148 mConfigureWasCalled = false;
1149 }
1150
1151 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001152 std::unique_lock<std::mutex> lock(mLock);
1153 base::ScopedLockAssertion assumeLocked(mLock);
1154 const bool resetCalled =
1155 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1156 return mResetWasCalled;
1157 });
1158 if (!resetCalled) {
1159 FAIL() << "Expected reset() to have been called.";
1160 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001161 mResetWasCalled = false;
1162 }
1163
Yi Kong9b14ac62018-07-17 13:48:38 -07001164 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001165 std::unique_lock<std::mutex> lock(mLock);
1166 base::ScopedLockAssertion assumeLocked(mLock);
1167 const bool processCalled =
1168 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1169 return mProcessWasCalled;
1170 });
1171 if (!processCalled) {
1172 FAIL() << "Expected process() to have been called.";
1173 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001174 if (outLastEvent) {
1175 *outLastEvent = mLastEvent;
1176 }
1177 mProcessWasCalled = false;
1178 }
1179
1180 void setKeyCodeState(int32_t keyCode, int32_t state) {
1181 mKeyCodeStates.replaceValueFor(keyCode, state);
1182 }
1183
1184 void setScanCodeState(int32_t scanCode, int32_t state) {
1185 mScanCodeStates.replaceValueFor(scanCode, state);
1186 }
1187
1188 void setSwitchState(int32_t switchCode, int32_t state) {
1189 mSwitchStates.replaceValueFor(switchCode, state);
1190 }
1191
1192 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001193 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001194 }
1195
Philip Junker4af3b3d2021-12-14 10:36:55 +01001196 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1197 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1198 }
1199
Michael Wrightd02c5b62014-02-10 15:10:22 -08001200private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001201 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001202
Chris Yea52ade12020-08-27 16:49:20 -07001203 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001204 InputMapper::populateDeviceInfo(deviceInfo);
1205
1206 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1207 deviceInfo->setKeyboardType(mKeyboardType);
1208 }
1209 }
1210
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001211 std::list<NotifyArgs> configure(nsecs_t, const InputReaderConfiguration* config,
1212 uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001213 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001214 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001215
1216 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001217 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001218 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1219 mViewport = config->getDisplayViewportByPort(*displayPort);
1220 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001221
1222 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001223 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001224 }
1225
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001226 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001227 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001228 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001229 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001230 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001231 }
1232
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001233 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001234 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001235 mLastEvent = *rawEvent;
1236 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001237 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001238 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001239 }
1240
Chris Yea52ade12020-08-27 16:49:20 -07001241 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001242 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1243 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1244 }
1245
Philip Junker4af3b3d2021-12-14 10:36:55 +01001246 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1247 auto it = mKeyCodeMapping.find(locationKeyCode);
1248 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1249 }
1250
Chris Yea52ade12020-08-27 16:49:20 -07001251 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001252 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1253 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1254 }
1255
Chris Yea52ade12020-08-27 16:49:20 -07001256 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001257 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1258 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1259 }
1260
Chris Yea52ade12020-08-27 16:49:20 -07001261 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001262 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -07001263 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001264 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001265 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1266 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1267 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001268 }
1269 }
1270 }
Chris Yea52ade12020-08-27 16:49:20 -07001271 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001272 return result;
1273 }
1274
1275 virtual int32_t getMetaState() {
1276 return mMetaState;
1277 }
1278
1279 virtual void fadePointer() {
1280 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001281
1282 virtual std::optional<int32_t> getAssociatedDisplay() {
1283 if (mViewport) {
1284 return std::make_optional(mViewport->displayId);
1285 }
1286 return std::nullopt;
1287 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001288};
1289
1290
1291// --- InstrumentedInputReader ---
1292
1293class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001294 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001295
1296public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001297 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1298 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001299 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001300 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001301
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001302 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001303
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001304 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001305
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001306 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001307 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001308 InputDeviceIdentifier identifier;
1309 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001310 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001311 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001312 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001313 }
1314
Prabir Pradhan28efc192019-11-05 01:10:04 +00001315 // Make the protected loopOnce method accessible to tests.
1316 using InputReader::loopOnce;
1317
Michael Wrightd02c5b62014-02-10 15:10:22 -08001318protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001319 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1320 const InputDeviceIdentifier& identifier)
1321 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001322 if (!mNextDevices.empty()) {
1323 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1324 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001325 return device;
1326 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001327 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001328 }
1329
arthurhungdcef2dc2020-08-11 14:47:50 +08001330 // --- FakeInputReaderContext ---
1331 class FakeInputReaderContext : public ContextImpl {
1332 int32_t mGlobalMetaState;
1333 bool mUpdateGlobalMetaStateWasCalled;
1334 int32_t mGeneration;
1335
1336 public:
1337 FakeInputReaderContext(InputReader* reader)
1338 : ContextImpl(reader),
1339 mGlobalMetaState(0),
1340 mUpdateGlobalMetaStateWasCalled(false),
1341 mGeneration(1) {}
1342
1343 virtual ~FakeInputReaderContext() {}
1344
1345 void assertUpdateGlobalMetaStateWasCalled() {
1346 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1347 << "Expected updateGlobalMetaState() to have been called.";
1348 mUpdateGlobalMetaStateWasCalled = false;
1349 }
1350
1351 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1352
1353 uint32_t getGeneration() { return mGeneration; }
1354
1355 void updateGlobalMetaState() override {
1356 mUpdateGlobalMetaStateWasCalled = true;
1357 ContextImpl::updateGlobalMetaState();
1358 }
1359
1360 int32_t getGlobalMetaState() override {
1361 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1362 }
1363
1364 int32_t bumpGeneration() override {
1365 mGeneration = ContextImpl::bumpGeneration();
1366 return mGeneration;
1367 }
1368 } mFakeContext;
1369
Michael Wrightd02c5b62014-02-10 15:10:22 -08001370 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001371
1372public:
1373 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001374};
1375
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001376// --- InputReaderPolicyTest ---
1377class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001378protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001379 sp<FakeInputReaderPolicy> mFakePolicy;
1380
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001381 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -07001382 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001383};
1384
1385/**
1386 * Check that empty set of viewports is an acceptable configuration.
1387 * Also try to get internal viewport two different ways - by type and by uniqueId.
1388 *
1389 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1390 * Such configuration is not currently allowed.
1391 */
1392TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001393 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001394
1395 // We didn't add any viewports yet, so there shouldn't be any.
1396 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001397 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001398 ASSERT_FALSE(internalViewport);
1399
1400 // Add an internal viewport, then clear it
1401 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001402 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001403 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001404
1405 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001406 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001407 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001408 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001409
1410 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001411 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001412 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001413 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001414
1415 mFakePolicy->clearViewports();
1416 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001417 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001418 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001419 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001420 ASSERT_FALSE(internalViewport);
1421}
1422
1423TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1424 const std::string internalUniqueId = "local:0";
1425 const std::string externalUniqueId = "local:1";
1426 const std::string virtualUniqueId1 = "virtual:2";
1427 const std::string virtualUniqueId2 = "virtual:3";
1428 constexpr int32_t virtualDisplayId1 = 2;
1429 constexpr int32_t virtualDisplayId2 = 3;
1430
1431 // Add an internal viewport
1432 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001433 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1434 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001435 // Add an external viewport
1436 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001437 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1438 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001439 // Add an virtual viewport
1440 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001441 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1442 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001443 // Add another virtual viewport
1444 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001445 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1446 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001447
1448 // Check matching by type for internal
1449 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001450 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001451 ASSERT_TRUE(internalViewport);
1452 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1453
1454 // Check matching by type for external
1455 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001456 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001457 ASSERT_TRUE(externalViewport);
1458 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1459
1460 // Check matching by uniqueId for virtual viewport #1
1461 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001462 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001463 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001464 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001465 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1466 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1467
1468 // Check matching by uniqueId for virtual viewport #2
1469 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001470 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001471 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001472 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001473 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1474 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1475}
1476
1477
1478/**
1479 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1480 * that lookup works by checking display id.
1481 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1482 */
1483TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1484 const std::string uniqueId1 = "uniqueId1";
1485 const std::string uniqueId2 = "uniqueId2";
1486 constexpr int32_t displayId1 = 2;
1487 constexpr int32_t displayId2 = 3;
1488
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001489 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1490 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001491 for (const ViewportType& type : types) {
1492 mFakePolicy->clearViewports();
1493 // Add a viewport
1494 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001495 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1496 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001497 // Add another viewport
1498 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001499 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1500 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001501
1502 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001503 std::optional<DisplayViewport> viewport1 =
1504 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001505 ASSERT_TRUE(viewport1);
1506 ASSERT_EQ(displayId1, viewport1->displayId);
1507 ASSERT_EQ(type, viewport1->type);
1508
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001509 std::optional<DisplayViewport> viewport2 =
1510 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001511 ASSERT_TRUE(viewport2);
1512 ASSERT_EQ(displayId2, viewport2->displayId);
1513 ASSERT_EQ(type, viewport2->type);
1514
1515 // When there are multiple viewports of the same kind, and uniqueId is not specified
1516 // in the call to getDisplayViewport, then that situation is not supported.
1517 // The viewports can be stored in any order, so we cannot rely on the order, since that
1518 // is just implementation detail.
1519 // However, we can check that it still returns *a* viewport, we just cannot assert
1520 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001521 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001522 ASSERT_TRUE(someViewport);
1523 }
1524}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001525
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001526/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001527 * When we have multiple internal displays make sure we always return the default display when
1528 * querying by type.
1529 */
1530TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1531 const std::string uniqueId1 = "uniqueId1";
1532 const std::string uniqueId2 = "uniqueId2";
1533 constexpr int32_t nonDefaultDisplayId = 2;
1534 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1535 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1536
1537 // Add the default display first and ensure it gets returned.
1538 mFakePolicy->clearViewports();
1539 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001540 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001541 ViewportType::INTERNAL);
1542 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001543 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001544 ViewportType::INTERNAL);
1545
1546 std::optional<DisplayViewport> viewport =
1547 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1548 ASSERT_TRUE(viewport);
1549 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1550 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1551
1552 // Add the default display second to make sure order doesn't matter.
1553 mFakePolicy->clearViewports();
1554 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001555 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001556 ViewportType::INTERNAL);
1557 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001558 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001559 ViewportType::INTERNAL);
1560
1561 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1562 ASSERT_TRUE(viewport);
1563 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1564 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1565}
1566
1567/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001568 * Check getDisplayViewportByPort
1569 */
1570TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001571 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001572 const std::string uniqueId1 = "uniqueId1";
1573 const std::string uniqueId2 = "uniqueId2";
1574 constexpr int32_t displayId1 = 1;
1575 constexpr int32_t displayId2 = 2;
1576 const uint8_t hdmi1 = 0;
1577 const uint8_t hdmi2 = 1;
1578 const uint8_t hdmi3 = 2;
1579
1580 mFakePolicy->clearViewports();
1581 // Add a viewport that's associated with some display port that's not of interest.
1582 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001583 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1584 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001585 // Add another viewport, connected to HDMI1 port
1586 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001587 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1588 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001589
1590 // Check that correct display viewport was returned by comparing the display ports.
1591 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1592 ASSERT_TRUE(hdmi1Viewport);
1593 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1594 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1595
1596 // Check that we can still get the same viewport using the uniqueId
1597 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1598 ASSERT_TRUE(hdmi1Viewport);
1599 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1600 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1601 ASSERT_EQ(type, hdmi1Viewport->type);
1602
1603 // Check that we cannot find a port with "HDMI2", because we never added one
1604 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1605 ASSERT_FALSE(hdmi2Viewport);
1606}
1607
Michael Wrightd02c5b62014-02-10 15:10:22 -08001608// --- InputReaderTest ---
1609
1610class InputReaderTest : public testing::Test {
1611protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001612 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001613 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001614 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001615 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001616
Chris Yea52ade12020-08-27 16:49:20 -07001617 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001618 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001619 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001620 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001621
Prabir Pradhan28efc192019-11-05 01:10:04 +00001622 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001623 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001624 }
1625
Chris Yea52ade12020-08-27 16:49:20 -07001626 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001627 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001628 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001629 }
1630
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001631 void addDevice(int32_t eventHubId, const std::string& name,
1632 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001633 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001634
1635 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001636 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001637 }
1638 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001639 mReader->loopOnce();
1640 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001641 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1642 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001643 }
1644
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001645 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001646 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001647 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001648 }
1649
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001650 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001651 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001652 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001653 }
1654
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001655 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001656 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001657 ftl::Flags<InputDeviceClass> classes,
1658 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001659 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001660 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1661 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001662 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001663 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001664 return mapper;
1665 }
1666};
1667
Chris Ye98d3f532020-10-01 21:48:59 -07001668TEST_F(InputReaderTest, PolicyGetInputDevices) {
1669 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001670 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001671 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001672
1673 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001674 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001675 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001676 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001677 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001678 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1679 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001680 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001681}
1682
Chris Yee7310032020-09-22 15:36:28 -07001683TEST_F(InputReaderTest, GetMergedInputDevices) {
1684 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1685 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1686 // Add two subdevices to device
1687 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1688 // Must add at least one mapper or the device will be ignored!
1689 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1690 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1691
1692 // Push same device instance for next device to be added, so they'll have same identifier.
1693 mReader->pushNextDevice(device);
1694 mReader->pushNextDevice(device);
1695 ASSERT_NO_FATAL_FAILURE(
1696 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1697 ASSERT_NO_FATAL_FAILURE(
1698 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1699
1700 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001701 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001702}
1703
Chris Yee14523a2020-12-19 13:46:00 -08001704TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1705 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1706 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1707 // Add two subdevices to device
1708 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1709 // Must add at least one mapper or the device will be ignored!
1710 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1711 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1712
1713 // Push same device instance for next device to be added, so they'll have same identifier.
1714 mReader->pushNextDevice(device);
1715 mReader->pushNextDevice(device);
1716 // Sensor device is initially disabled
1717 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1718 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1719 nullptr));
1720 // Device is disabled because the only sub device is a sensor device and disabled initially.
1721 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1722 ASSERT_FALSE(device->isEnabled());
1723 ASSERT_NO_FATAL_FAILURE(
1724 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1725 // The merged device is enabled if any sub device is enabled
1726 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1727 ASSERT_TRUE(device->isEnabled());
1728}
1729
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001730TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001731 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001732 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001733 constexpr int32_t eventHubId = 1;
1734 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001735 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001736 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001737 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001738 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001739
Yi Kong9b14ac62018-07-17 13:48:38 -07001740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001741
1742 NotifyDeviceResetArgs resetArgs;
1743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001744 ASSERT_EQ(deviceId, resetArgs.deviceId);
1745
1746 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001747 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001748 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001749
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001751 ASSERT_EQ(deviceId, resetArgs.deviceId);
1752 ASSERT_EQ(device->isEnabled(), false);
1753
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001754 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001755 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001758 ASSERT_EQ(device->isEnabled(), false);
1759
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001760 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001761 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001763 ASSERT_EQ(deviceId, resetArgs.deviceId);
1764 ASSERT_EQ(device->isEnabled(), true);
1765}
1766
Michael Wrightd02c5b62014-02-10 15:10:22 -08001767TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001768 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001769 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001770 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001771 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001772 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001773 AINPUT_SOURCE_KEYBOARD, nullptr);
1774 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001775
1776 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1777 AINPUT_SOURCE_ANY, AKEYCODE_A))
1778 << "Should return unknown when the device id is >= 0 but unknown.";
1779
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001780 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1781 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1782 << "Should return unknown when the device id is valid but the sources are not "
1783 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001784
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001785 ASSERT_EQ(AKEY_STATE_DOWN,
1786 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1787 AKEYCODE_A))
1788 << "Should return value provided by mapper when device id is valid and the device "
1789 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001790
1791 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1792 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1793 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1794
1795 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1796 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1797 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1798}
1799
Philip Junker4af3b3d2021-12-14 10:36:55 +01001800TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1801 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1802 constexpr int32_t eventHubId = 1;
1803 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1804 InputDeviceClass::KEYBOARD,
1805 AINPUT_SOURCE_KEYBOARD, nullptr);
1806 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1807
1808 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1809 << "Should return unknown when the device with the specified id is not found.";
1810
1811 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1812 << "Should return correct mapping when device id is valid and mapping exists.";
1813
1814 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1815 << "Should return the location key code when device id is valid and there's no "
1816 "mapping.";
1817}
1818
1819TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1820 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1821 constexpr int32_t eventHubId = 1;
1822 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1823 InputDeviceClass::JOYSTICK,
1824 AINPUT_SOURCE_GAMEPAD, nullptr);
1825 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1826
1827 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1828 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1829}
1830
Michael Wrightd02c5b62014-02-10 15:10:22 -08001831TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001832 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001833 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001834 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001835 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001836 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001837 AINPUT_SOURCE_KEYBOARD, nullptr);
1838 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001839
1840 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1841 AINPUT_SOURCE_ANY, KEY_A))
1842 << "Should return unknown when the device id is >= 0 but unknown.";
1843
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001844 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1845 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1846 << "Should return unknown when the device id is valid but the sources are not "
1847 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001848
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001849 ASSERT_EQ(AKEY_STATE_DOWN,
1850 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1851 KEY_A))
1852 << "Should return value provided by mapper when device id is valid and the device "
1853 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001854
1855 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1856 AINPUT_SOURCE_TRACKBALL, KEY_A))
1857 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1858
1859 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1860 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1861 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1862}
1863
1864TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001865 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001866 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001867 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001868 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001869 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001870 AINPUT_SOURCE_KEYBOARD, nullptr);
1871 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001872
1873 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1874 AINPUT_SOURCE_ANY, SW_LID))
1875 << "Should return unknown when the device id is >= 0 but unknown.";
1876
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001877 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1878 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1879 << "Should return unknown when the device id is valid but the sources are not "
1880 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001881
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001882 ASSERT_EQ(AKEY_STATE_DOWN,
1883 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1884 SW_LID))
1885 << "Should return value provided by mapper when device id is valid and the device "
1886 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001887
1888 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1889 AINPUT_SOURCE_TRACKBALL, SW_LID))
1890 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1891
1892 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1893 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1894 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1895}
1896
1897TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001898 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001899 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001900 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001901 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001902 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001903 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001904
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001905 mapper.addSupportedKeyCode(AKEYCODE_A);
1906 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001907
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001908 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001909 uint8_t flags[4] = { 0, 0, 0, 1 };
1910
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001911 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08001912 << "Should return false when device id is >= 0 but unknown.";
1913 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1914
1915 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001916 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001917 << "Should return false when device id is valid but the sources are not supported by "
1918 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001919 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1920
1921 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001922 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001923 keyCodes, flags))
1924 << "Should return value provided by mapper when device id is valid and the device "
1925 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001926 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1927
1928 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001929 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1930 << "Should return false when the device id is < 0 but the sources are not supported by "
1931 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001932 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1933
1934 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001935 ASSERT_TRUE(
1936 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1937 << "Should return value provided by mapper when device id is < 0 and one of the "
1938 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001939 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1940}
1941
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001942TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001943 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001944 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001945
1946 NotifyConfigurationChangedArgs args;
1947
1948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1949 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1950}
1951
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001952TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001953 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001954 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001955 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001956 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001957 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001958 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001959 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001960 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001961
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001962 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001963 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001964 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1965
1966 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001967 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001968 ASSERT_EQ(when, event.when);
1969 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001970 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001971 ASSERT_EQ(EV_KEY, event.type);
1972 ASSERT_EQ(KEY_A, event.code);
1973 ASSERT_EQ(1, event.value);
1974}
1975
Garfield Tan1c7bc862020-01-28 13:24:04 -08001976TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001977 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001978 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001979 constexpr int32_t eventHubId = 1;
1980 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001981 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001982 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001983 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001984 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001985
1986 NotifyDeviceResetArgs resetArgs;
1987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001988 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001989
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001990 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001991 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001993 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001994 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001995
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001996 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001997 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001999 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002000 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002001
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002002 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002003 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08002005 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002006 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002007}
2008
Garfield Tan1c7bc862020-01-28 13:24:04 -08002009TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
2010 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002011 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002012 constexpr int32_t eventHubId = 1;
2013 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2014 // Must add at least one mapper or the device will be ignored!
2015 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002016 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002017 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
2018
2019 NotifyDeviceResetArgs resetArgs;
2020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2021 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
2022}
2023
Arthur Hungc23540e2018-11-29 20:42:11 +08002024TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002025 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002026 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002027 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08002028 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002029 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2030 FakeInputMapper& mapper =
2031 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002032 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08002033
2034 const uint8_t hdmi1 = 1;
2035
2036 // Associated touch screen with second display.
2037 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2038
2039 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00002040 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08002041 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002042 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002043 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002044 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002045 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002046 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002047 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002048 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002049
2050 // Add the device, and make sure all of the callbacks are triggered.
2051 // The device is added after the input port associations are processed since
2052 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002053 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002056 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002057
Arthur Hung2c9a3342019-07-23 14:18:59 +08002058 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002059 ASSERT_EQ(deviceId, device->getId());
2060 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2061 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002062
2063 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002064 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002065 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002066 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002067}
2068
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002069TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2070 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002071 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002072 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2073 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2074 // Must add at least one mapper or the device will be ignored!
2075 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2076 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2077 mReader->pushNextDevice(device);
2078 mReader->pushNextDevice(device);
2079 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2080 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2081
2082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2083
2084 NotifyDeviceResetArgs resetArgs;
2085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2086 ASSERT_EQ(deviceId, resetArgs.deviceId);
2087 ASSERT_TRUE(device->isEnabled());
2088 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2089 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2090
2091 disableDevice(deviceId);
2092 mReader->loopOnce();
2093
2094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2095 ASSERT_EQ(deviceId, resetArgs.deviceId);
2096 ASSERT_FALSE(device->isEnabled());
2097 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2098 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2099
2100 enableDevice(deviceId);
2101 mReader->loopOnce();
2102
2103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2104 ASSERT_EQ(deviceId, resetArgs.deviceId);
2105 ASSERT_TRUE(device->isEnabled());
2106 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2107 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2108}
2109
2110TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2111 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002112 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002113 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2114 // Add two subdevices to device
2115 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2116 FakeInputMapper& mapperDevice1 =
2117 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2118 FakeInputMapper& mapperDevice2 =
2119 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2120 mReader->pushNextDevice(device);
2121 mReader->pushNextDevice(device);
2122 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2123 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2124
2125 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2126 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2127
2128 ASSERT_EQ(AKEY_STATE_DOWN,
2129 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2130 ASSERT_EQ(AKEY_STATE_DOWN,
2131 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2132 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2133 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2134}
2135
Prabir Pradhan7e186182020-11-10 13:56:45 -08002136TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2137 NotifyPointerCaptureChangedArgs args;
2138
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002139 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002140 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2141 mReader->loopOnce();
2142 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002143 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2144 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002145
2146 mFakePolicy->setPointerCapture(false);
2147 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2148 mReader->loopOnce();
2149 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002150 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002151
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002152 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002153 // does not change.
2154 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2155 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002156 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002157}
2158
Chris Ye87143712020-11-10 05:05:58 +00002159class FakeVibratorInputMapper : public FakeInputMapper {
2160public:
2161 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2162 : FakeInputMapper(deviceContext, sources) {}
2163
2164 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2165};
2166
2167TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2168 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002169 ftl::Flags<InputDeviceClass> deviceClass =
2170 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002171 constexpr int32_t eventHubId = 1;
2172 const char* DEVICE_LOCATION = "BLUETOOTH";
2173 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2174 FakeVibratorInputMapper& mapper =
2175 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2176 mReader->pushNextDevice(device);
2177
2178 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2179 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2180
2181 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2182 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2183}
2184
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002185// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002186
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002187class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002188public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002189 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002190
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002191 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002192
Andy Chenf9f1a022022-08-29 20:07:10 -04002193 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
2194
Chris Yee2b1e5c2021-03-10 22:45:12 -08002195 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2196
2197 void dump(std::string& dump) override {}
2198
2199 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2200 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002201 }
2202
Chris Yee2b1e5c2021-03-10 22:45:12 -08002203 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2204 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002205 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002206
2207 bool setLightColor(int32_t lightId, int32_t color) override {
2208 getDeviceContext().setLightBrightness(lightId, color >> 24);
2209 return true;
2210 }
2211
2212 std::optional<int32_t> getLightColor(int32_t lightId) override {
2213 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2214 if (!result.has_value()) {
2215 return std::nullopt;
2216 }
2217 return result.value() << 24;
2218 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002219
2220 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2221
2222 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2223
2224private:
2225 InputDeviceContext& mDeviceContext;
2226 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2227 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04002228 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002229};
2230
Chris Yee2b1e5c2021-03-10 22:45:12 -08002231TEST_F(InputReaderTest, BatteryGetCapacity) {
2232 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002233 ftl::Flags<InputDeviceClass> deviceClass =
2234 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002235 constexpr int32_t eventHubId = 1;
2236 const char* DEVICE_LOCATION = "BLUETOOTH";
2237 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002238 FakePeripheralController& controller =
2239 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002240 mReader->pushNextDevice(device);
2241
2242 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2243
2244 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2245 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2246}
2247
2248TEST_F(InputReaderTest, BatteryGetStatus) {
2249 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002250 ftl::Flags<InputDeviceClass> deviceClass =
2251 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002252 constexpr int32_t eventHubId = 1;
2253 const char* DEVICE_LOCATION = "BLUETOOTH";
2254 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002255 FakePeripheralController& controller =
2256 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002257 mReader->pushNextDevice(device);
2258
2259 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2260
2261 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2262 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2263}
2264
Prabir Pradhane287ecd2022-09-07 21:18:05 +00002265TEST_F(InputReaderTest, BatteryGetDevicePath) {
2266 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2267 ftl::Flags<InputDeviceClass> deviceClass =
2268 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2269 constexpr int32_t eventHubId = 1;
2270 const char* DEVICE_LOCATION = "BLUETOOTH";
2271 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2272 device->addController<FakePeripheralController>(eventHubId);
2273 mReader->pushNextDevice(device);
2274
2275 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2276
2277 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), BATTERY_DEVPATH);
2278}
2279
Chris Ye3fdbfef2021-01-06 18:45:18 -08002280TEST_F(InputReaderTest, LightGetColor) {
2281 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002282 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002283 constexpr int32_t eventHubId = 1;
2284 const char* DEVICE_LOCATION = "BLUETOOTH";
2285 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002286 FakePeripheralController& controller =
2287 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002288 mReader->pushNextDevice(device);
2289 RawLightInfo info = {.id = 1,
2290 .name = "Mono",
2291 .maxBrightness = 255,
2292 .flags = InputLightClass::BRIGHTNESS,
2293 .path = ""};
2294 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2295 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2296
2297 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002298
Chris Yee2b1e5c2021-03-10 22:45:12 -08002299 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2300 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002301 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2302 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2303}
2304
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002305// --- InputReaderIntegrationTest ---
2306
2307// These tests create and interact with the InputReader only through its interface.
2308// The InputReader is started during SetUp(), which starts its processing in its own
2309// thread. The tests use linux uinput to emulate input devices.
2310// NOTE: Interacting with the physical device while these tests are running may cause
2311// the tests to fail.
2312class InputReaderIntegrationTest : public testing::Test {
2313protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002314 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002315 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002316 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002317
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002318 std::shared_ptr<FakePointerController> mFakePointerController;
2319
Chris Yea52ade12020-08-27 16:49:20 -07002320 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002321#if !defined(__ANDROID__)
2322 GTEST_SKIP();
2323#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002324 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002325 mFakePointerController = std::make_shared<FakePointerController>();
2326 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002327 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2328 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002329
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002330 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2331 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002332 ASSERT_EQ(mReader->start(), OK);
2333
2334 // Since this test is run on a real device, all the input devices connected
2335 // to the test device will show up in mReader. We wait for those input devices to
2336 // show up before beginning the tests.
2337 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2338 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2339 }
2340
Chris Yea52ade12020-08-27 16:49:20 -07002341 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002342#if !defined(__ANDROID__)
2343 return;
2344#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002345 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002346 mReader.reset();
2347 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002348 mFakePolicy.clear();
2349 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00002350
2351 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
2352 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
2353 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
2354 [&name](const InputDeviceInfo& info) {
2355 return info.getIdentifier().name == name;
2356 });
2357 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
2358 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002359};
2360
2361TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2362 // An invalid input device that is only used for this test.
2363 class InvalidUinputDevice : public UinputDevice {
2364 public:
2365 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2366
2367 private:
2368 void configureDevice(int fd, uinput_user_dev* device) override {}
2369 };
2370
2371 const size_t numDevices = mFakePolicy->getInputDevices().size();
2372
2373 // UinputDevice does not set any event or key bits, so InputReader should not
2374 // consider it as a valid device.
2375 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2376 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2377 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2378 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2379
2380 invalidDevice.reset();
2381 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2382 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2383 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2384}
2385
2386TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2387 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2388
2389 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2390 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2391 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2392 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2393
2394 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002395 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002396 const auto& it =
2397 std::find_if(inputDevices.begin(), inputDevices.end(),
2398 [&keyboard](const InputDeviceInfo& info) {
2399 return info.getIdentifier().name == keyboard->getName();
2400 });
2401
2402 ASSERT_NE(it, inputDevices.end());
2403 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2404 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2405 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002406
2407 keyboard.reset();
2408 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2409 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2410 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2411}
2412
2413TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2414 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2415 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2416
2417 NotifyConfigurationChangedArgs configChangedArgs;
2418 ASSERT_NO_FATAL_FAILURE(
2419 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002420 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002421 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2422
2423 NotifyKeyArgs keyArgs;
2424 keyboard->pressAndReleaseHomeKey();
2425 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2426 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002427 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002428 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002429 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002430 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002431 prevTimestamp = keyArgs.eventTime;
2432
2433 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2434 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002435 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002436 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002437 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002438}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002439
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002440/**
2441 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2442 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2443 * are passed to the listener.
2444 */
2445static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2446TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2447 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2448 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2449 NotifyKeyArgs keyArgs;
2450
2451 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2452 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2453 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2454 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2455
2456 controller->pressAndReleaseKey(BTN_GEAR_UP);
2457 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2458 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2459 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2460}
2461
Arthur Hungaab25622020-01-16 11:22:11 +08002462// --- TouchProcessTest ---
2463class TouchIntegrationTest : public InputReaderIntegrationTest {
2464protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002465 const std::string UNIQUE_ID = "local:0";
2466
Chris Yea52ade12020-08-27 16:49:20 -07002467 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002468#if !defined(__ANDROID__)
2469 GTEST_SKIP();
2470#endif
Arthur Hungaab25622020-01-16 11:22:11 +08002471 InputReaderIntegrationTest::SetUp();
2472 // At least add an internal display.
2473 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2474 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002475 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002476
2477 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2478 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2479 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00002480 const auto info = findDeviceByName(mDevice->getName());
2481 ASSERT_TRUE(info);
2482 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08002483 }
2484
2485 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2486 int32_t orientation, const std::string& uniqueId,
2487 std::optional<uint8_t> physicalPort,
2488 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002489 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2490 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002491 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2492 }
2493
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002494 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2495 NotifyMotionArgs args;
2496 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2497 EXPECT_EQ(action, args.action);
2498 ASSERT_EQ(points.size(), args.pointerCount);
2499 for (size_t i = 0; i < args.pointerCount; i++) {
2500 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2501 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2502 }
2503 }
2504
Arthur Hungaab25622020-01-16 11:22:11 +08002505 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00002506 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08002507};
2508
2509TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2510 NotifyMotionArgs args;
2511 const Point centerPoint = mDevice->getCenterPoint();
2512
2513 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002514 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002515 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002516 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002517 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2518 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2519
2520 // ACTION_MOVE
2521 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002522 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002523 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2524 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2525
2526 // ACTION_UP
2527 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002528 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002529 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2530 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2531}
2532
2533TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2534 NotifyMotionArgs args;
2535 const Point centerPoint = mDevice->getCenterPoint();
2536
2537 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002538 mDevice->sendSlot(FIRST_SLOT);
2539 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002540 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002541 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002542 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2543 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2544
2545 // ACTION_POINTER_DOWN (Second slot)
2546 const Point secondPoint = centerPoint + Point(100, 100);
2547 mDevice->sendSlot(SECOND_SLOT);
2548 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002549 mDevice->sendDown(secondPoint);
2550 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002551 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002552 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002553
2554 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002555 mDevice->sendMove(secondPoint + Point(1, 1));
2556 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002557 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2558 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2559
2560 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002561 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002562 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002563 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002564 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002565
2566 // ACTION_UP
2567 mDevice->sendSlot(FIRST_SLOT);
2568 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002569 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002570 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2571 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2572}
2573
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002574/**
2575 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2576 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2577 * data?
2578 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2579 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2580 * for Pointer 0 only is generated after.
2581 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2582 * events, we will not miss any information.
2583 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2584 * event generated afterwards that contains the newest movement of pointer 0.
2585 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2586 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2587 * losing information about non-palm pointers.
2588 */
2589TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2590 NotifyMotionArgs args;
2591 const Point centerPoint = mDevice->getCenterPoint();
2592
2593 // ACTION_DOWN
2594 mDevice->sendSlot(FIRST_SLOT);
2595 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2596 mDevice->sendDown(centerPoint);
2597 mDevice->sendSync();
2598 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2599
2600 // ACTION_POINTER_DOWN (Second slot)
2601 const Point secondPoint = centerPoint + Point(100, 100);
2602 mDevice->sendSlot(SECOND_SLOT);
2603 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2604 mDevice->sendDown(secondPoint);
2605 mDevice->sendSync();
2606 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2607
2608 // ACTION_MOVE (First slot)
2609 mDevice->sendSlot(FIRST_SLOT);
2610 mDevice->sendMove(centerPoint + Point(5, 5));
2611 // ACTION_POINTER_UP (Second slot)
2612 mDevice->sendSlot(SECOND_SLOT);
2613 mDevice->sendPointerUp();
2614 // Send a single sync for the above 2 pointer updates
2615 mDevice->sendSync();
2616
2617 // First, we should get POINTER_UP for the second pointer
2618 assertReceivedMotion(ACTION_POINTER_1_UP,
2619 {/*first pointer */ centerPoint + Point(5, 5),
2620 /*second pointer*/ secondPoint});
2621
2622 // Next, the MOVE event for the first pointer
2623 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2624}
2625
2626/**
2627 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2628 * move, and then it will go up, all in the same frame.
2629 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2630 * gets sent to the listener.
2631 */
2632TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2633 NotifyMotionArgs args;
2634 const Point centerPoint = mDevice->getCenterPoint();
2635
2636 // ACTION_DOWN
2637 mDevice->sendSlot(FIRST_SLOT);
2638 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2639 mDevice->sendDown(centerPoint);
2640 mDevice->sendSync();
2641 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2642
2643 // ACTION_POINTER_DOWN (Second slot)
2644 const Point secondPoint = centerPoint + Point(100, 100);
2645 mDevice->sendSlot(SECOND_SLOT);
2646 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2647 mDevice->sendDown(secondPoint);
2648 mDevice->sendSync();
2649 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2650
2651 // ACTION_MOVE (First slot)
2652 mDevice->sendSlot(FIRST_SLOT);
2653 mDevice->sendMove(centerPoint + Point(5, 5));
2654 // ACTION_POINTER_UP (Second slot)
2655 mDevice->sendSlot(SECOND_SLOT);
2656 mDevice->sendMove(secondPoint + Point(6, 6));
2657 mDevice->sendPointerUp();
2658 // Send a single sync for the above 2 pointer updates
2659 mDevice->sendSync();
2660
2661 // First, we should get POINTER_UP for the second pointer
2662 // The movement of the second pointer during the liftoff frame is ignored.
2663 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2664 assertReceivedMotion(ACTION_POINTER_1_UP,
2665 {/*first pointer */ centerPoint + Point(5, 5),
2666 /*second pointer*/ secondPoint});
2667
2668 // Next, the MOVE event for the first pointer
2669 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2670}
2671
Arthur Hungaab25622020-01-16 11:22:11 +08002672TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2673 NotifyMotionArgs args;
2674 const Point centerPoint = mDevice->getCenterPoint();
2675
2676 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002677 mDevice->sendSlot(FIRST_SLOT);
2678 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002679 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002680 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002681 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2682 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2683
arthurhungcc7f9802020-04-30 17:55:40 +08002684 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002685 const Point secondPoint = centerPoint + Point(100, 100);
2686 mDevice->sendSlot(SECOND_SLOT);
2687 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2688 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002689 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002690 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002691 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002692
arthurhungcc7f9802020-04-30 17:55:40 +08002693 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002694 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002695 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002696 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2697 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2698
arthurhungcc7f9802020-04-30 17:55:40 +08002699 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2700 // a palm event.
2701 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002702 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002703 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002704 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002705 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002706 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002707
arthurhungcc7f9802020-04-30 17:55:40 +08002708 // Send up to second slot, expect first slot send moving.
2709 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002710 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002711 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2712 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002713
arthurhungcc7f9802020-04-30 17:55:40 +08002714 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002715 mDevice->sendSlot(FIRST_SLOT);
2716 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002717 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002718
arthurhungcc7f9802020-04-30 17:55:40 +08002719 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2720 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002721}
2722
Prabir Pradhanda20b172022-09-26 17:01:18 +00002723TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
2724 const Point centerPoint = mDevice->getCenterPoint();
2725
2726 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
2727 mDevice->sendSlot(FIRST_SLOT);
2728 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2729 mDevice->sendToolType(MT_TOOL_PEN);
2730 mDevice->sendDown(centerPoint);
2731 mDevice->sendSync();
2732 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2733 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2734 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
2735
2736 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2737
2738 // Release the stylus touch.
2739 mDevice->sendUp();
2740 mDevice->sendSync();
2741 ASSERT_NO_FATAL_FAILURE(
2742 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2743
2744 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2745
2746 // Touch down with the finger, without the pen tool selected. The policy is not notified.
2747 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2748 mDevice->sendToolType(MT_TOOL_FINGER);
2749 mDevice->sendDown(centerPoint);
2750 mDevice->sendSync();
2751 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2752 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2753 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
2754
2755 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2756
2757 mDevice->sendUp();
2758 mDevice->sendSync();
2759 ASSERT_NO_FATAL_FAILURE(
2760 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2761
2762 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
2763 // The policy should be notified of the stylus presence.
2764 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2765 mDevice->sendToolType(MT_TOOL_PEN);
2766 mDevice->sendMove(centerPoint);
2767 mDevice->sendSync();
2768 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2769 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2770 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
2771
2772 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2773}
2774
Michael Wrightd02c5b62014-02-10 15:10:22 -08002775// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002776class InputDeviceTest : public testing::Test {
2777protected:
2778 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002779 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002780 static const int32_t DEVICE_ID;
2781 static const int32_t DEVICE_GENERATION;
2782 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002783 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002784 static const int32_t EVENTHUB_ID;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002785 static const std::string DEVICE_BLUETOOTH_ADDRESS;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002786
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002787 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002788 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002789 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002790 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002791 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002792
Chris Yea52ade12020-08-27 16:49:20 -07002793 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002794 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002795 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002796 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002797 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002798 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002799 InputDeviceIdentifier identifier;
2800 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002801 identifier.location = DEVICE_LOCATION;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002802 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
arthurhungdcef2dc2020-08-11 14:47:50 +08002803 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002804 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002805 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002806 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002807 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002808 }
2809
Chris Yea52ade12020-08-27 16:49:20 -07002810 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002811 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002812 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002813 }
2814};
2815
2816const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002817const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002818const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002819const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2820const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002821const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002822 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002823const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002824const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002825
2826TEST_F(InputDeviceTest, ImmutableProperties) {
2827 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002828 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002829 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002830}
2831
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002832TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
2833 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
2834
2835 // Configuration
2836 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2837 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002838 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002839
2840 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
2841}
2842
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002843TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2844 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002845}
2846
Michael Wrightd02c5b62014-02-10 15:10:22 -08002847TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2848 // Configuration.
2849 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002850 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002851
2852 // Reset.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002853 unused += mDevice->reset(ARBITRARY_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002854
2855 NotifyDeviceResetArgs resetArgs;
2856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2857 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2858 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2859
2860 // Metadata.
2861 ASSERT_TRUE(mDevice->isIgnored());
2862 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2863
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002864 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002865 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002866 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002867 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2868 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2869
2870 // State queries.
2871 ASSERT_EQ(0, mDevice->getMetaState());
2872
2873 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2874 << "Ignored device should return unknown key code state.";
2875 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2876 << "Ignored device should return unknown scan code state.";
2877 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2878 << "Ignored device should return unknown switch state.";
2879
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002880 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002881 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002882 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002883 << "Ignored device should never mark any key codes.";
2884 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2885 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2886}
2887
2888TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2889 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002890 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08002891
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002892 FakeInputMapper& mapper1 =
2893 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002894 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2895 mapper1.setMetaState(AMETA_ALT_ON);
2896 mapper1.addSupportedKeyCode(AKEYCODE_A);
2897 mapper1.addSupportedKeyCode(AKEYCODE_B);
2898 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2899 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2900 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2901 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2902 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002903
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002904 FakeInputMapper& mapper2 =
2905 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002906 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002907
2908 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002909 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002910
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002911 std::string propertyValue;
2912 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002913 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002914 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002915
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002916 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2917 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002918
2919 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002920 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002921 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2922 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002923
2924 NotifyDeviceResetArgs resetArgs;
2925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2926 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2927 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2928
2929 // Metadata.
2930 ASSERT_FALSE(mDevice->isIgnored());
2931 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2932
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002933 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002934 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002935 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002936 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2937 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2938
2939 // State queries.
2940 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2941 << "Should query mappers and combine meta states.";
2942
2943 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2944 << "Should return unknown key code state when source not supported.";
2945 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2946 << "Should return unknown scan code state when source not supported.";
2947 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2948 << "Should return unknown switch state when source not supported.";
2949
2950 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2951 << "Should query mapper when source is supported.";
2952 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2953 << "Should query mapper when source is supported.";
2954 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2955 << "Should query mapper when source is supported.";
2956
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002957 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002958 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002959 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002960 << "Should do nothing when source is unsupported.";
2961 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2962 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2963 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2964 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2965
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002966 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002967 << "Should query mapper when source is supported.";
2968 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2969 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2970 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2971 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2972
2973 // Event handling.
2974 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002975 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002976 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002977
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002978 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2979 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002980}
2981
Arthur Hung2c9a3342019-07-23 14:18:59 +08002982// A single input device is associated with a specific display. Check that:
2983// 1. Device is disabled if the viewport corresponding to the associated display is not found
2984// 2. Device is disabled when setEnabled API is called
2985TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002986 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002987
2988 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002989 std::list<NotifyArgs> unused =
2990 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002991
2992 // Device should be enabled by default.
2993 ASSERT_TRUE(mDevice->isEnabled());
2994
2995 // Prepare associated info.
2996 constexpr uint8_t hdmi = 1;
2997 const std::string UNIQUE_ID = "local:1";
2998
2999 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003000 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3001 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003002 // Device should be disabled because it is associated with a specific display via
3003 // input port <-> display port association, but the corresponding display is not found
3004 ASSERT_FALSE(mDevice->isEnabled());
3005
3006 // Prepare displays.
3007 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003008 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
3009 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003010 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3011 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003012 ASSERT_TRUE(mDevice->isEnabled());
3013
3014 // Device should be disabled after set disable.
3015 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003016 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3017 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003018 ASSERT_FALSE(mDevice->isEnabled());
3019
3020 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003021 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3022 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003023 ASSERT_FALSE(mDevice->isEnabled());
3024}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003025
Christine Franks1ba71cc2021-04-07 14:37:42 -07003026TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
3027 // Device should be enabled by default.
3028 mFakePolicy->clearViewports();
3029 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003030 std::list<NotifyArgs> unused =
3031 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003032 ASSERT_TRUE(mDevice->isEnabled());
3033
3034 // Device should be disabled because it is associated with a specific display, but the
3035 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08003036 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003037 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3038 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003039 ASSERT_FALSE(mDevice->isEnabled());
3040
3041 // Device should be enabled when a display is found.
3042 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3043 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
3044 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003045 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3046 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003047 ASSERT_TRUE(mDevice->isEnabled());
3048
3049 // Device should be disabled after set disable.
3050 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003051 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3052 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003053 ASSERT_FALSE(mDevice->isEnabled());
3054
3055 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003056 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3057 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003058 ASSERT_FALSE(mDevice->isEnabled());
3059}
3060
Christine Franks2a2293c2022-01-18 11:51:16 -08003061TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
3062 mFakePolicy->clearViewports();
3063 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003064 std::list<NotifyArgs> unused =
3065 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks2a2293c2022-01-18 11:51:16 -08003066
Christine Franks2a2293c2022-01-18 11:51:16 -08003067 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
3068 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3069 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
3070 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003071 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3072 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08003073 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
3074}
3075
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07003076/**
3077 * This test reproduces a crash caused by a dangling reference that remains after device is added
3078 * and removed. The reference is accessed in InputDevice::dump(..);
3079 */
3080TEST_F(InputDeviceTest, DumpDoesNotCrash) {
3081 constexpr int32_t TEST_EVENTHUB_ID = 10;
3082 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
3083
3084 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
3085 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
3086 device.removeEventHubDevice(TEST_EVENTHUB_ID);
3087 std::string dumpStr, eventHubDevStr;
3088 device.dump(dumpStr, eventHubDevStr);
3089}
3090
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003091TEST_F(InputDeviceTest, GetBluetoothAddress) {
3092 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
3093 ASSERT_TRUE(address);
3094 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
3095}
3096
Michael Wrightd02c5b62014-02-10 15:10:22 -08003097// --- InputMapperTest ---
3098
3099class InputMapperTest : public testing::Test {
3100protected:
3101 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003102 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003103 static const int32_t DEVICE_ID;
3104 static const int32_t DEVICE_GENERATION;
3105 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003106 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003107 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003108
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003109 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003110 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003111 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08003112 std::unique_ptr<InstrumentedInputReader> mReader;
3113 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003114
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003115 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003116 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003117 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003118 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08003119 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003120 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08003121 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003122 // Consume the device reset notification generated when adding a new device.
3123 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003124 }
3125
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003126 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003127 SetUp(DEVICE_CLASSES);
3128 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003129
Chris Yea52ade12020-08-27 16:49:20 -07003130 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003131 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003132 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003133 }
3134
3135 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003136 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003137 }
3138
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003139 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00003140 if (!changes ||
3141 (changes &
3142 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
3143 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003144 mReader->requestRefreshConfiguration(changes);
3145 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08003146 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003147 std::list<NotifyArgs> out =
3148 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003149 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003150 for (const NotifyArgs& args : out) {
3151 mFakeListener->notify(args);
3152 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003153 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003154 return out;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003155 }
3156
arthurhungdcef2dc2020-08-11 14:47:50 +08003157 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
3158 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003159 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003160 InputDeviceIdentifier identifier;
3161 identifier.name = name;
3162 identifier.location = location;
3163 std::shared_ptr<InputDevice> device =
3164 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
3165 identifier);
3166 mReader->pushNextDevice(device);
3167 mFakeEventHub->addDevice(eventHubId, name, classes);
3168 mReader->loopOnce();
3169 return device;
3170 }
3171
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003172 template <class T, typename... Args>
3173 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003174 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003175 configureDevice(0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003176 std::list<NotifyArgs> resetArgList = mDevice->reset(ARBITRARY_TIME);
3177 resetArgList += mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003178 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003179 for (const NotifyArgs& loopArgs : resetArgList) {
3180 mFakeListener->notify(loopArgs);
3181 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003182 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003183 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003184 }
3185
3186 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003187 int32_t orientation, const std::string& uniqueId,
3188 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003189 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3190 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003191 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3192 }
3193
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003194 void clearViewports() {
3195 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003196 }
3197
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003198 std::list<NotifyArgs> process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type,
3199 int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003200 RawEvent event;
3201 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003202 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003203 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003204 event.type = type;
3205 event.code = code;
3206 event.value = value;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003207 std::list<NotifyArgs> processArgList = mapper.process(&event);
3208 for (const NotifyArgs& args : processArgList) {
3209 mFakeListener->notify(args);
3210 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003211 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003212 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003213 return processArgList;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003214 }
3215
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00003216 void resetMapper(InputMapper& mapper, nsecs_t when) {
3217 const auto resetArgs = mapper.reset(when);
3218 for (const auto args : resetArgs) {
3219 mFakeListener->notify(args);
3220 }
3221 // Loop the reader to flush the input listener queue.
3222 mReader->loopOnce();
3223 }
3224
Michael Wrightd02c5b62014-02-10 15:10:22 -08003225 static void assertMotionRange(const InputDeviceInfo& info,
3226 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3227 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003228 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003229 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3230 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3231 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3232 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3233 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3234 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3235 }
3236
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003237 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3238 float size, float touchMajor, float touchMinor, float toolMajor,
3239 float toolMinor, float orientation, float distance,
3240 float scaledAxisEpsilon = 1.f) {
3241 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3242 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003243 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3244 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003245 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3246 scaledAxisEpsilon);
3247 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3248 scaledAxisEpsilon);
3249 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3250 scaledAxisEpsilon);
3251 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3252 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003253 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3254 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3255 }
3256
Michael Wright17db18e2020-06-26 20:51:44 +01003257 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003258 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003259 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003260 ASSERT_NEAR(x, actualX, 1);
3261 ASSERT_NEAR(y, actualY, 1);
3262 }
3263};
3264
3265const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003266const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003267const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003268const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3269const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003270const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3271 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003272const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273
3274// --- SwitchInputMapperTest ---
3275
3276class SwitchInputMapperTest : public InputMapperTest {
3277protected:
3278};
3279
3280TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003281 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003283 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003284}
3285
3286TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003287 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003288
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003289 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003290 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003292 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003293 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003294}
3295
3296TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003297 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003298 std::list<NotifyArgs> out;
3299 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3300 ASSERT_TRUE(out.empty());
3301 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3302 ASSERT_TRUE(out.empty());
3303 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3304 ASSERT_TRUE(out.empty());
3305 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003306
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003307 ASSERT_EQ(1u, out.size());
3308 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003309 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003310 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3311 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003312 args.switchMask);
3313 ASSERT_EQ(uint32_t(0), args.policyFlags);
3314}
3315
Chris Ye87143712020-11-10 05:05:58 +00003316// --- VibratorInputMapperTest ---
3317class VibratorInputMapperTest : public InputMapperTest {
3318protected:
3319 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3320};
3321
3322TEST_F(VibratorInputMapperTest, GetSources) {
3323 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3324
3325 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3326}
3327
3328TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3329 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3330
3331 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3332}
3333
3334TEST_F(VibratorInputMapperTest, Vibrate) {
3335 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003336 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003337 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3338
3339 VibrationElement pattern(2);
3340 VibrationSequence sequence(2);
3341 pattern.duration = std::chrono::milliseconds(200);
3342 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3343 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3344 sequence.addElement(pattern);
3345 pattern.duration = std::chrono::milliseconds(500);
3346 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3347 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3348 sequence.addElement(pattern);
3349
3350 std::vector<int64_t> timings = {0, 1};
3351 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3352
3353 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003354 // Start vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003355 std::list<NotifyArgs> out = mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003356 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003357 // Verify vibrator state listener was notified.
3358 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003359 ASSERT_EQ(1u, out.size());
3360 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3361 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
3362 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08003363 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003364 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08003365 ASSERT_FALSE(mapper.isVibrating());
3366 // Verify vibrator state listener was notified.
3367 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003368 ASSERT_EQ(1u, out.size());
3369 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3370 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
3371 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003372}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003373
Chris Yef59a2f42020-10-16 12:55:26 -07003374// --- SensorInputMapperTest ---
3375
3376class SensorInputMapperTest : public InputMapperTest {
3377protected:
3378 static const int32_t ACCEL_RAW_MIN;
3379 static const int32_t ACCEL_RAW_MAX;
3380 static const int32_t ACCEL_RAW_FUZZ;
3381 static const int32_t ACCEL_RAW_FLAT;
3382 static const int32_t ACCEL_RAW_RESOLUTION;
3383
3384 static const int32_t GYRO_RAW_MIN;
3385 static const int32_t GYRO_RAW_MAX;
3386 static const int32_t GYRO_RAW_FUZZ;
3387 static const int32_t GYRO_RAW_FLAT;
3388 static const int32_t GYRO_RAW_RESOLUTION;
3389
3390 static const float GRAVITY_MS2_UNIT;
3391 static const float DEGREE_RADIAN_UNIT;
3392
3393 void prepareAccelAxes();
3394 void prepareGyroAxes();
3395 void setAccelProperties();
3396 void setGyroProperties();
3397 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3398};
3399
3400const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3401const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3402const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3403const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3404const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3405
3406const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3407const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3408const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3409const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3410const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3411
3412const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3413const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3414
3415void SensorInputMapperTest::prepareAccelAxes() {
3416 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3417 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3418 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3419 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3420 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3421 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3422}
3423
3424void SensorInputMapperTest::prepareGyroAxes() {
3425 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3426 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3427 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3428 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3429 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3430 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3431}
3432
3433void SensorInputMapperTest::setAccelProperties() {
3434 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3435 /* sensorDataIndex */ 0);
3436 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3437 /* sensorDataIndex */ 1);
3438 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3439 /* sensorDataIndex */ 2);
3440 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3441 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3442 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3443 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3444 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3445}
3446
3447void SensorInputMapperTest::setGyroProperties() {
3448 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3449 /* sensorDataIndex */ 0);
3450 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3451 /* sensorDataIndex */ 1);
3452 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3453 /* sensorDataIndex */ 2);
3454 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3455 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3456 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3457 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3458 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3459}
3460
3461TEST_F(SensorInputMapperTest, GetSources) {
3462 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3463
3464 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3465}
3466
3467TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3468 setAccelProperties();
3469 prepareAccelAxes();
3470 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3471
3472 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3473 std::chrono::microseconds(10000),
3474 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003475 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3477 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3478 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3479 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3480 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003481
3482 NotifySensorArgs args;
3483 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3484 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3485 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3486
3487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3488 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3489 ASSERT_EQ(args.deviceId, DEVICE_ID);
3490 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3491 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3492 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3493 ASSERT_EQ(args.values, values);
3494 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3495}
3496
3497TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3498 setGyroProperties();
3499 prepareGyroAxes();
3500 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3501
3502 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3503 std::chrono::microseconds(10000),
3504 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003505 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003506 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3507 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3508 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3509 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3510 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003511
3512 NotifySensorArgs args;
3513 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3514 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3515 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3516
3517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3518 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3519 ASSERT_EQ(args.deviceId, DEVICE_ID);
3520 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3521 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3522 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3523 ASSERT_EQ(args.values, values);
3524 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3525}
3526
Michael Wrightd02c5b62014-02-10 15:10:22 -08003527// --- KeyboardInputMapperTest ---
3528
3529class KeyboardInputMapperTest : public InputMapperTest {
3530protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003531 const std::string UNIQUE_ID = "local:0";
3532
3533 void prepareDisplay(int32_t orientation);
3534
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003535 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003536 int32_t originalKeyCode, int32_t rotatedKeyCode,
3537 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003538};
3539
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003540/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3541 * orientation.
3542 */
3543void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003544 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3545 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003546}
3547
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003548void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003549 int32_t originalScanCode, int32_t originalKeyCode,
3550 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003551 NotifyKeyArgs args;
3552
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003553 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3555 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3556 ASSERT_EQ(originalScanCode, args.scanCode);
3557 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003558 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003559
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003560 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3562 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3563 ASSERT_EQ(originalScanCode, args.scanCode);
3564 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003565 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003566}
3567
Michael Wrightd02c5b62014-02-10 15:10:22 -08003568TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003569 KeyboardInputMapper& mapper =
3570 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3571 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003572
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003573 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003574}
3575
3576TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3577 const int32_t USAGE_A = 0x070004;
3578 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003579 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3580 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003581 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3582 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3583 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003584
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003585 KeyboardInputMapper& mapper =
3586 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3587 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003588 // Initial metastate is AMETA_NONE.
3589 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003590
3591 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003592 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003593 NotifyKeyArgs args;
3594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3595 ASSERT_EQ(DEVICE_ID, args.deviceId);
3596 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3597 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3598 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3599 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3600 ASSERT_EQ(KEY_HOME, args.scanCode);
3601 ASSERT_EQ(AMETA_NONE, args.metaState);
3602 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3603 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3604 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3605
3606 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003607 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3609 ASSERT_EQ(DEVICE_ID, args.deviceId);
3610 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3611 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3612 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3613 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3614 ASSERT_EQ(KEY_HOME, args.scanCode);
3615 ASSERT_EQ(AMETA_NONE, args.metaState);
3616 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3617 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3618 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3619
3620 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003621 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3622 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3624 ASSERT_EQ(DEVICE_ID, args.deviceId);
3625 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3626 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3627 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3628 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3629 ASSERT_EQ(0, args.scanCode);
3630 ASSERT_EQ(AMETA_NONE, args.metaState);
3631 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3632 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3633 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3634
3635 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003636 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3637 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3639 ASSERT_EQ(DEVICE_ID, args.deviceId);
3640 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3641 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3642 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3643 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3644 ASSERT_EQ(0, args.scanCode);
3645 ASSERT_EQ(AMETA_NONE, args.metaState);
3646 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3647 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3648 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3649
3650 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003651 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3652 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3654 ASSERT_EQ(DEVICE_ID, args.deviceId);
3655 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3656 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3657 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3658 ASSERT_EQ(0, args.keyCode);
3659 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3660 ASSERT_EQ(AMETA_NONE, args.metaState);
3661 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3662 ASSERT_EQ(0U, args.policyFlags);
3663 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3664
3665 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003666 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3667 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3669 ASSERT_EQ(DEVICE_ID, args.deviceId);
3670 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3671 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3672 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3673 ASSERT_EQ(0, args.keyCode);
3674 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3675 ASSERT_EQ(AMETA_NONE, args.metaState);
3676 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3677 ASSERT_EQ(0U, args.policyFlags);
3678 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3679}
3680
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003681/**
3682 * Ensure that the readTime is set to the time when the EV_KEY is received.
3683 */
3684TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3685 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3686
3687 KeyboardInputMapper& mapper =
3688 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3689 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3690 NotifyKeyArgs args;
3691
3692 // Key down
3693 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3695 ASSERT_EQ(12, args.readTime);
3696
3697 // Key up
3698 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3700 ASSERT_EQ(15, args.readTime);
3701}
3702
Michael Wrightd02c5b62014-02-10 15:10:22 -08003703TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003704 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3705 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003706 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3707 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3708 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003709
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003710 KeyboardInputMapper& mapper =
3711 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3712 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003713
Arthur Hung95f68612022-04-07 14:08:22 +08003714 // Initial metastate is AMETA_NONE.
3715 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003716
3717 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003718 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003719 NotifyKeyArgs args;
3720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3721 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003722 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003723 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003724
3725 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003726 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3728 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003729 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003730
3731 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003732 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3734 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003735 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003736
3737 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003738 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3740 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003741 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003742 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003743}
3744
3745TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003746 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3747 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3748 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3749 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003750
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003751 KeyboardInputMapper& mapper =
3752 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3753 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003754
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003755 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003756 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3757 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3758 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3759 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3760 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3761 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3762 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3763 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3764}
3765
3766TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003767 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3768 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3769 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3770 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003771
Michael Wrightd02c5b62014-02-10 15:10:22 -08003772 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003773 KeyboardInputMapper& mapper =
3774 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3775 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003776
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003777 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003778 ASSERT_NO_FATAL_FAILURE(
3779 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3780 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3781 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3782 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3783 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3784 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3785 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003786
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003787 clearViewports();
3788 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003789 ASSERT_NO_FATAL_FAILURE(
3790 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3791 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3792 AKEYCODE_DPAD_UP, DISPLAY_ID));
3793 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3794 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3795 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3796 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003797
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003798 clearViewports();
3799 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003800 ASSERT_NO_FATAL_FAILURE(
3801 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3802 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3803 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3804 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3805 AKEYCODE_DPAD_UP, DISPLAY_ID));
3806 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3807 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003808
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003809 clearViewports();
3810 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003811 ASSERT_NO_FATAL_FAILURE(
3812 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3813 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3814 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3815 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3816 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3817 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3818 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003819
3820 // Special case: if orientation changes while key is down, we still emit the same keycode
3821 // in the key up as we did in the key down.
3822 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003823 clearViewports();
3824 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003825 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3827 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3828 ASSERT_EQ(KEY_UP, args.scanCode);
3829 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3830
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003831 clearViewports();
3832 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003833 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3835 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3836 ASSERT_EQ(KEY_UP, args.scanCode);
3837 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3838}
3839
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003840TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3841 // If the keyboard is not orientation aware,
3842 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003843 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003844
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003845 KeyboardInputMapper& mapper =
3846 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3847 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003848 NotifyKeyArgs args;
3849
3850 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003851 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003853 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3855 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3856
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003857 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003858 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3862 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3863}
3864
3865TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3866 // If the keyboard is orientation aware,
3867 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003868 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003869
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003870 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003871 KeyboardInputMapper& mapper =
3872 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3873 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003874 NotifyKeyArgs args;
3875
3876 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3877 // ^--- already checked by the previous test
3878
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003879 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003880 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003881 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003883 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3885 ASSERT_EQ(DISPLAY_ID, args.displayId);
3886
3887 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003888 clearViewports();
3889 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003890 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003891 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003893 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3895 ASSERT_EQ(newDisplayId, args.displayId);
3896}
3897
Michael Wrightd02c5b62014-02-10 15:10:22 -08003898TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003899 KeyboardInputMapper& mapper =
3900 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3901 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003902
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003903 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003904 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003905
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003906 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003907 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003908}
3909
Philip Junker4af3b3d2021-12-14 10:36:55 +01003910TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3911 KeyboardInputMapper& mapper =
3912 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3913 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3914
3915 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3916 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3917 << "If a mapping is available, the result is equal to the mapping";
3918
3919 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3920 << "If no mapping is available, the result is the key location";
3921}
3922
Michael Wrightd02c5b62014-02-10 15:10:22 -08003923TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003924 KeyboardInputMapper& mapper =
3925 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3926 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003927
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003928 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003929 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003930
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003931 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003932 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003933}
3934
3935TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003936 KeyboardInputMapper& mapper =
3937 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3938 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003939
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003940 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003941
Michael Wrightd02c5b62014-02-10 15:10:22 -08003942 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003943 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003944 ASSERT_TRUE(flags[0]);
3945 ASSERT_FALSE(flags[1]);
3946}
3947
3948TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003949 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3950 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3951 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3952 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3953 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3954 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003955
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003956 KeyboardInputMapper& mapper =
3957 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3958 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003959 // Initial metastate is AMETA_NONE.
3960 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003961
3962 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003963 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3964 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3965 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003966
3967 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003968 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3969 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003970 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3971 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3972 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003973 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003974
3975 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003976 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3977 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003978 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3979 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3980 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003981 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003982
3983 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003984 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3985 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003986 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3987 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3988 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003989 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003990
3991 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003992 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003994 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3995 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3996 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003997 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003998
3999 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004000 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4001 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004002 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4003 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4004 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004005 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004006
4007 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004008 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4009 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004010 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4011 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4012 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004013 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004014}
4015
Chris Yea52ade12020-08-27 16:49:20 -07004016TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
4017 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
4018 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
4019 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
4020 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
4021
4022 KeyboardInputMapper& mapper =
4023 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4024 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4025
Chris Yea52ade12020-08-27 16:49:20 -07004026 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004027 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07004028 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4029 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
4030 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
4031 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4032
4033 NotifyKeyArgs args;
4034 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004035 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07004036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4037 ASSERT_EQ(AMETA_NONE, args.metaState);
4038 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4039 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4040 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
4041
4042 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004043 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07004044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4045 ASSERT_EQ(AMETA_NONE, args.metaState);
4046 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4047 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4048 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
4049}
4050
Arthur Hung2c9a3342019-07-23 14:18:59 +08004051TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
4052 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004053 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4054 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4055 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4056 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004057
4058 // keyboard 2.
4059 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08004060 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08004061 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004062 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08004063 std::shared_ptr<InputDevice> device2 =
4064 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004065 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08004066
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004067 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4068 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4069 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4070 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004071
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004072 KeyboardInputMapper& mapper =
4073 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4074 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004075
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004076 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004077 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004078 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004079 std::list<NotifyArgs> unused =
4080 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4081 0 /*changes*/);
4082 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004083
4084 // Prepared displays and associated info.
4085 constexpr uint8_t hdmi1 = 0;
4086 constexpr uint8_t hdmi2 = 1;
4087 const std::string SECONDARY_UNIQUE_ID = "local:1";
4088
4089 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
4090 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
4091
4092 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004093 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4094 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004095 ASSERT_FALSE(device2->isEnabled());
4096
4097 // Prepare second display.
4098 constexpr int32_t newDisplayId = 2;
4099 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004100 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004101 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004102 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004103 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004104 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4105 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004106
4107 // Device should be enabled after the associated display is found.
4108 ASSERT_TRUE(mDevice->isEnabled());
4109 ASSERT_TRUE(device2->isEnabled());
4110
4111 // Test pad key events
4112 ASSERT_NO_FATAL_FAILURE(
4113 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
4114 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4115 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4116 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4117 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4118 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4119 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4120
4121 ASSERT_NO_FATAL_FAILURE(
4122 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
4123 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4124 AKEYCODE_DPAD_RIGHT, newDisplayId));
4125 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4126 AKEYCODE_DPAD_DOWN, newDisplayId));
4127 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4128 AKEYCODE_DPAD_LEFT, newDisplayId));
4129}
Michael Wrightd02c5b62014-02-10 15:10:22 -08004130
arthurhungc903df12020-08-11 15:08:42 +08004131TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
4132 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4133 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4134 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4135 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4136 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4137 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4138
4139 KeyboardInputMapper& mapper =
4140 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4141 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004142 // Initial metastate is AMETA_NONE.
4143 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004144
4145 // Initialization should have turned all of the lights off.
4146 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4147 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4148 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4149
4150 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004151 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4152 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004153 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4154 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4155
4156 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004157 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4158 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004159 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4160 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
4161
4162 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004165 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4166 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
4167
4168 mFakeEventHub->removeDevice(EVENTHUB_ID);
4169 mReader->loopOnce();
4170
4171 // keyboard 2 should default toggle keys.
4172 const std::string USB2 = "USB2";
4173 const std::string DEVICE_NAME2 = "KEYBOARD2";
4174 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4175 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4176 std::shared_ptr<InputDevice> device2 =
4177 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004178 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08004179 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4180 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4181 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4182 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4183 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4184 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4185
arthurhung6fe95782020-10-05 22:41:16 +08004186 KeyboardInputMapper& mapper2 =
4187 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4188 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004189 std::list<NotifyArgs> unused =
4190 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4191 0 /*changes*/);
4192 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08004193
4194 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4195 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4196 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004197 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4198 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004199}
4200
Arthur Hungcb40a002021-08-03 14:31:01 +00004201TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4202 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4203 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4204 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4205
4206 // Suppose we have two mappers. (DPAD + KEYBOARD)
4207 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4208 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4209 KeyboardInputMapper& mapper =
4210 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4211 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004212 // Initial metastate is AMETA_NONE.
4213 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004214
4215 mReader->toggleCapsLockState(DEVICE_ID);
4216 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4217}
4218
Arthur Hungfb3cc112022-04-13 07:39:50 +00004219TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4220 // keyboard 1.
4221 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4222 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4223 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4224 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4225 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4226 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4227
4228 KeyboardInputMapper& mapper1 =
4229 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4230 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4231
4232 // keyboard 2.
4233 const std::string USB2 = "USB2";
4234 const std::string DEVICE_NAME2 = "KEYBOARD2";
4235 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4236 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4237 std::shared_ptr<InputDevice> device2 =
4238 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4239 ftl::Flags<InputDeviceClass>(0));
4240 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4241 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4242 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4243 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4244 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4245 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4246
4247 KeyboardInputMapper& mapper2 =
4248 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4249 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004250 std::list<NotifyArgs> unused =
4251 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4252 0 /*changes*/);
4253 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004254
Arthur Hung95f68612022-04-07 14:08:22 +08004255 // Initial metastate is AMETA_NONE.
4256 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4257 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4258
4259 // Toggle num lock on and off.
4260 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4261 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004262 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4263 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4264 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4265
4266 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4267 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4268 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4269 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4270 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4271
4272 // Toggle caps lock on and off.
4273 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4274 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4275 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4276 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4277 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4278
4279 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4280 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4281 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4282 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4283 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4284
4285 // Toggle scroll lock on and off.
4286 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4287 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4288 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4289 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4290 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4291
4292 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4293 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4294 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4295 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4296 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4297}
4298
Arthur Hung2141d542022-08-23 07:45:21 +00004299TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4300 const int32_t USAGE_A = 0x070004;
4301 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4302 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4303
4304 KeyboardInputMapper& mapper =
4305 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4306 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4307 // Key down by scan code.
4308 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4309 NotifyKeyArgs args;
4310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4311 ASSERT_EQ(DEVICE_ID, args.deviceId);
4312 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4313 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4314 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4315 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4316 ASSERT_EQ(KEY_HOME, args.scanCode);
4317 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4318
4319 // Disable device, it should synthesize cancellation events for down events.
4320 mFakePolicy->addDisabledDevice(DEVICE_ID);
4321 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
4322
4323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4324 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4325 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4326 ASSERT_EQ(KEY_HOME, args.scanCode);
4327 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4328}
4329
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004330// --- KeyboardInputMapperTest_ExternalDevice ---
4331
4332class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4333protected:
Chris Yea52ade12020-08-27 16:49:20 -07004334 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004335};
4336
4337TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004338 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4339 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004340
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004341 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4342 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4343 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4344 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004345
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004346 KeyboardInputMapper& mapper =
4347 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4348 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004349
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004350 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004351 NotifyKeyArgs args;
4352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4353 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4354
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004355 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4357 ASSERT_EQ(uint32_t(0), args.policyFlags);
4358
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004359 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4361 ASSERT_EQ(uint32_t(0), args.policyFlags);
4362
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004363 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4365 ASSERT_EQ(uint32_t(0), args.policyFlags);
4366
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004367 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4369 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4370
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004371 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4373 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4374}
4375
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004376TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004377 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004378
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004379 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4380 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4381 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004382
Powei Fengd041c5d2019-05-03 17:11:33 -07004383 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004384 KeyboardInputMapper& mapper =
4385 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4386 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004387
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004388 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004389 NotifyKeyArgs args;
4390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4391 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4392
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004393 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4395 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4396
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004397 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4399 ASSERT_EQ(uint32_t(0), args.policyFlags);
4400
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004401 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4403 ASSERT_EQ(uint32_t(0), args.policyFlags);
4404
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004405 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4407 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4408
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004409 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4411 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4412}
4413
Michael Wrightd02c5b62014-02-10 15:10:22 -08004414// --- CursorInputMapperTest ---
4415
4416class CursorInputMapperTest : public InputMapperTest {
4417protected:
4418 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4419
Michael Wright17db18e2020-06-26 20:51:44 +01004420 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004421
Chris Yea52ade12020-08-27 16:49:20 -07004422 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004423 InputMapperTest::SetUp();
4424
Michael Wright17db18e2020-06-26 20:51:44 +01004425 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004426 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004427 }
4428
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004429 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4430 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004431
4432 void prepareDisplay(int32_t orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004433 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4434 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4435 }
4436
4437 void prepareSecondaryDisplay() {
4438 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4439 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
4440 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004441 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004442
4443 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4444 float pressure) {
4445 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4446 0.0f, 0.0f, 0.0f, EPSILON));
4447 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004448};
4449
4450const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4451
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004452void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4453 int32_t originalY, int32_t rotatedX,
4454 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004455 NotifyMotionArgs args;
4456
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4458 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4459 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4461 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004462 ASSERT_NO_FATAL_FAILURE(
4463 assertCursorPointerCoords(args.pointerCoords[0],
4464 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4465 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466}
4467
4468TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004470 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004471
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004472 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004473}
4474
4475TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004476 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004477 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004478
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004479 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004480}
4481
4482TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004483 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004484 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004485
4486 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004487 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488
4489 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004490 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4491 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004492 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4493 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4494
4495 // When the bounds are set, then there should be a valid motion range.
4496 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4497
4498 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004499 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004500
4501 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4502 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4503 1, 800 - 1, 0.0f, 0.0f));
4504 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4505 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4506 2, 480 - 1, 0.0f, 0.0f));
4507 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4508 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4509 0.0f, 1.0f, 0.0f, 0.0f));
4510}
4511
4512TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004513 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004514 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004515
4516 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004517 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004518
4519 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4520 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4521 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4522 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4523 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4524 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4525 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4526 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4527 0.0f, 1.0f, 0.0f, 0.0f));
4528}
4529
4530TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004531 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004532 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004533
arthurhungdcef2dc2020-08-11 14:47:50 +08004534 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004535
4536 NotifyMotionArgs args;
4537
4538 // Button press.
4539 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004540 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4541 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4543 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4544 ASSERT_EQ(DEVICE_ID, args.deviceId);
4545 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4546 ASSERT_EQ(uint32_t(0), args.policyFlags);
4547 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4548 ASSERT_EQ(0, args.flags);
4549 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4550 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4551 ASSERT_EQ(0, args.edgeFlags);
4552 ASSERT_EQ(uint32_t(1), args.pointerCount);
4553 ASSERT_EQ(0, args.pointerProperties[0].id);
4554 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004555 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004556 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4557 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4558 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4559
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4561 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4562 ASSERT_EQ(DEVICE_ID, args.deviceId);
4563 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4564 ASSERT_EQ(uint32_t(0), args.policyFlags);
4565 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4566 ASSERT_EQ(0, args.flags);
4567 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4568 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4569 ASSERT_EQ(0, args.edgeFlags);
4570 ASSERT_EQ(uint32_t(1), args.pointerCount);
4571 ASSERT_EQ(0, args.pointerProperties[0].id);
4572 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004573 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004574 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4575 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4576 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4577
Michael Wrightd02c5b62014-02-10 15:10:22 -08004578 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004579 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4580 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4582 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4583 ASSERT_EQ(DEVICE_ID, args.deviceId);
4584 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4585 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004586 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4587 ASSERT_EQ(0, args.flags);
4588 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4589 ASSERT_EQ(0, args.buttonState);
4590 ASSERT_EQ(0, args.edgeFlags);
4591 ASSERT_EQ(uint32_t(1), args.pointerCount);
4592 ASSERT_EQ(0, args.pointerProperties[0].id);
4593 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004594 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004595 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4596 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4597 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4598
4599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4600 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4601 ASSERT_EQ(DEVICE_ID, args.deviceId);
4602 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4603 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004604 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4605 ASSERT_EQ(0, args.flags);
4606 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4607 ASSERT_EQ(0, args.buttonState);
4608 ASSERT_EQ(0, args.edgeFlags);
4609 ASSERT_EQ(uint32_t(1), args.pointerCount);
4610 ASSERT_EQ(0, args.pointerProperties[0].id);
4611 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004612 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004613 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4614 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4615 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4616}
4617
4618TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004619 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004620 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004621
4622 NotifyMotionArgs args;
4623
4624 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004625 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4626 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4628 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004629 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4630 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4631 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632
4633 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004634 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4635 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4637 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004638 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4639 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004640}
4641
4642TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004643 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004644 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004645
4646 NotifyMotionArgs args;
4647
4648 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004649 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4650 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4652 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004653 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004654
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4656 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004657 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004658
Michael Wrightd02c5b62014-02-10 15:10:22 -08004659 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004660 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4661 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004663 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004664 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004665
4666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004667 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004668 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004669}
4670
4671TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004672 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004673 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004674
4675 NotifyMotionArgs args;
4676
4677 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004678 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4679 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4680 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4681 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4683 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004684 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4685 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4686 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004687
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4689 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004690 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4691 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4692 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004693
Michael Wrightd02c5b62014-02-10 15:10:22 -08004694 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004695 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4696 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4697 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4699 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004700 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4701 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4702 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004703
4704 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004705 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4706 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004708 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004709 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004710
4711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004712 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004713 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004714}
4715
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004716TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004717 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004718 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004719 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4720 // need to be rotated.
4721 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004722 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004723
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004724 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004725 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4726 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4727 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4728 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4729 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4730 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4731 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4732 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4733}
4734
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004735TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004736 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004737 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004738 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4739 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004740 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004741
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004742 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004743 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004744 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4745 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4746 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4747 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4748 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4749 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4750 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4751 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4752
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004753 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004754 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004755 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4756 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4757 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4758 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4759 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4760 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4761 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4762 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004763
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004764 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004765 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004766 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4767 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4768 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4769 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4770 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4771 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4772 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4773 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4774
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004775 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004776 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004777 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4778 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4779 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4780 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4781 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4782 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4783 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4784 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004785}
4786
4787TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004788 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004789 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004790
4791 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4792 mFakePointerController->setPosition(100, 200);
4793 mFakePointerController->setButtonState(0);
4794
4795 NotifyMotionArgs motionArgs;
4796 NotifyKeyArgs keyArgs;
4797
4798 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4800 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4802 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4803 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4804 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004805 ASSERT_NO_FATAL_FAILURE(
4806 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004807
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4809 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4810 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4811 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004812 ASSERT_NO_FATAL_FAILURE(
4813 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004814
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004815 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4816 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004818 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004819 ASSERT_EQ(0, motionArgs.buttonState);
4820 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004821 ASSERT_NO_FATAL_FAILURE(
4822 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004823
4824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004825 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004826 ASSERT_EQ(0, motionArgs.buttonState);
4827 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004828 ASSERT_NO_FATAL_FAILURE(
4829 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004830
4831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004832 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004833 ASSERT_EQ(0, motionArgs.buttonState);
4834 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004835 ASSERT_NO_FATAL_FAILURE(
4836 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004837
4838 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004839 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4840 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4841 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4843 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4844 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4845 motionArgs.buttonState);
4846 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4847 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004848 ASSERT_NO_FATAL_FAILURE(
4849 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004850
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4852 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4853 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4854 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4855 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004856 ASSERT_NO_FATAL_FAILURE(
4857 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004858
4859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4860 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4861 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4862 motionArgs.buttonState);
4863 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4864 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004865 ASSERT_NO_FATAL_FAILURE(
4866 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004867
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004868 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4869 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004871 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004872 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4873 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004874 ASSERT_NO_FATAL_FAILURE(
4875 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004876
4877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004878 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004879 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4880 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004881 ASSERT_NO_FATAL_FAILURE(
4882 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004883
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004884 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4885 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004887 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4888 ASSERT_EQ(0, motionArgs.buttonState);
4889 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004890 ASSERT_NO_FATAL_FAILURE(
4891 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004892 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4893 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004894
4895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004896 ASSERT_EQ(0, motionArgs.buttonState);
4897 ASSERT_EQ(0, mFakePointerController->getButtonState());
4898 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004899 ASSERT_NO_FATAL_FAILURE(
4900 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004901
Michael Wrightd02c5b62014-02-10 15:10:22 -08004902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4903 ASSERT_EQ(0, motionArgs.buttonState);
4904 ASSERT_EQ(0, mFakePointerController->getButtonState());
4905 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004906 ASSERT_NO_FATAL_FAILURE(
4907 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004908
4909 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004910 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4911 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4913 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4914 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004915
Michael Wrightd02c5b62014-02-10 15:10:22 -08004916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004917 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004918 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4919 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004920 ASSERT_NO_FATAL_FAILURE(
4921 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004922
4923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4924 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4925 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4926 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004927 ASSERT_NO_FATAL_FAILURE(
4928 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004929
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004930 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4931 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004933 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004934 ASSERT_EQ(0, motionArgs.buttonState);
4935 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004936 ASSERT_NO_FATAL_FAILURE(
4937 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004938
4939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004940 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004941 ASSERT_EQ(0, motionArgs.buttonState);
4942 ASSERT_EQ(0, mFakePointerController->getButtonState());
4943
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004944 ASSERT_NO_FATAL_FAILURE(
4945 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4947 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4948 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4949
4950 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004951 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4952 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4954 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4955 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004956
Michael Wrightd02c5b62014-02-10 15:10:22 -08004957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004958 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004959 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4960 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004961 ASSERT_NO_FATAL_FAILURE(
4962 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004963
4964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4965 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4966 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4967 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004968 ASSERT_NO_FATAL_FAILURE(
4969 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004970
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004971 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4972 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004974 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004975 ASSERT_EQ(0, motionArgs.buttonState);
4976 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004977 ASSERT_NO_FATAL_FAILURE(
4978 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004979
4980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4981 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4982 ASSERT_EQ(0, motionArgs.buttonState);
4983 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004984 ASSERT_NO_FATAL_FAILURE(
4985 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004986
Michael Wrightd02c5b62014-02-10 15:10:22 -08004987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4988 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4989 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4990
4991 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004992 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4995 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4996 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004997
Michael Wrightd02c5b62014-02-10 15:10:22 -08004998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004999 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005000 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5001 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005002 ASSERT_NO_FATAL_FAILURE(
5003 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005004
5005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5006 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5007 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5008 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005009 ASSERT_NO_FATAL_FAILURE(
5010 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005011
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005012 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
5013 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005015 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005016 ASSERT_EQ(0, motionArgs.buttonState);
5017 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005018 ASSERT_NO_FATAL_FAILURE(
5019 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005020
5021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5022 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5023 ASSERT_EQ(0, motionArgs.buttonState);
5024 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005025 ASSERT_NO_FATAL_FAILURE(
5026 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005027
Michael Wrightd02c5b62014-02-10 15:10:22 -08005028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5029 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5030 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5031
5032 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005033 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
5034 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5036 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5037 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005038
Michael Wrightd02c5b62014-02-10 15:10:22 -08005039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005040 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005041 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5042 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005043 ASSERT_NO_FATAL_FAILURE(
5044 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005045
5046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5047 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5048 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5049 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005050 ASSERT_NO_FATAL_FAILURE(
5051 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005052
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005053 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
5054 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005056 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005057 ASSERT_EQ(0, motionArgs.buttonState);
5058 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005059 ASSERT_NO_FATAL_FAILURE(
5060 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005061
5062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5063 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5064 ASSERT_EQ(0, motionArgs.buttonState);
5065 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005066 ASSERT_NO_FATAL_FAILURE(
5067 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005068
Michael Wrightd02c5b62014-02-10 15:10:22 -08005069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5070 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5071 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5072}
5073
5074TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005075 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005076 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005077
5078 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5079 mFakePointerController->setPosition(100, 200);
5080 mFakePointerController->setButtonState(0);
5081
5082 NotifyMotionArgs args;
5083
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005084 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5085 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5086 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005088 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5089 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5090 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5091 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01005092 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005093}
5094
5095TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005096 addConfigurationProperty("cursor.mode", "pointer");
5097 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005098 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005099
5100 NotifyDeviceResetArgs resetArgs;
5101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5102 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5103 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5104
5105 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5106 mFakePointerController->setPosition(100, 200);
5107 mFakePointerController->setButtonState(0);
5108
5109 NotifyMotionArgs args;
5110
5111 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005112 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5113 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5114 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5116 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5117 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5118 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5119 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01005120 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005121
5122 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5124 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5126 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5127 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5128 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5129 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5131 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5132 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
5133 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5134 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5135
5136 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005137 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5138 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5140 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5141 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
5142 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5143 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5145 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5146 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
5147 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5148 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5149
5150 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005151 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
5152 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
5153 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5155 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5156 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5157 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5158 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01005159 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005160
5161 // Disable pointer capture and check that the device generation got bumped
5162 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08005163 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005164 mFakePolicy->setPointerCapture(false);
5165 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08005166 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005167
5168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005169 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5170
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005171 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5172 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5173 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5175 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005176 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5177 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5178 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01005179 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005180}
5181
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00005182/**
5183 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
5184 * pointer acceleration or speed processing should not be applied.
5185 */
5186TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
5187 addConfigurationProperty("cursor.mode", "pointer");
5188 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
5189 100.f /*high threshold*/, 10.f /*acceleration*/);
5190 mFakePolicy->setVelocityControlParams(testParams);
5191 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5192
5193 NotifyDeviceResetArgs resetArgs;
5194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5195 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5196 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5197
5198 NotifyMotionArgs args;
5199
5200 // Move and verify scale is applied.
5201 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5202 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5203 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5205 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5206 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5207 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5208 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5209 ASSERT_GT(relX, 10);
5210 ASSERT_GT(relY, 20);
5211
5212 // Enable Pointer Capture
5213 mFakePolicy->setPointerCapture(true);
5214 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5215 NotifyPointerCaptureChangedArgs captureArgs;
5216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5217 ASSERT_TRUE(captureArgs.request.enable);
5218
5219 // Move and verify scale is not applied.
5220 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5221 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5222 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5224 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5225 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5226 ASSERT_EQ(10, args.pointerCoords[0].getX());
5227 ASSERT_EQ(20, args.pointerCoords[0].getY());
5228}
5229
Prabir Pradhan208360b2022-06-24 18:37:04 +00005230TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5231 addConfigurationProperty("cursor.mode", "pointer");
5232 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5233
5234 NotifyDeviceResetArgs resetArgs;
5235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5236 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5237 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5238
5239 // Ensure the display is rotated.
5240 prepareDisplay(DISPLAY_ORIENTATION_90);
5241
5242 NotifyMotionArgs args;
5243
5244 // Verify that the coordinates are rotated.
5245 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5246 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5247 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5249 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5250 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5251 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5252 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5253
5254 // Enable Pointer Capture.
5255 mFakePolicy->setPointerCapture(true);
5256 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5257 NotifyPointerCaptureChangedArgs captureArgs;
5258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5259 ASSERT_TRUE(captureArgs.request.enable);
5260
5261 // Move and verify rotation is not applied.
5262 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5264 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5266 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5267 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5268 ASSERT_EQ(10, args.pointerCoords[0].getX());
5269 ASSERT_EQ(20, args.pointerCoords[0].getY());
5270}
5271
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005272TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005273 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005274
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005275 // Set up the default display.
5276 prepareDisplay(DISPLAY_ORIENTATION_90);
5277
5278 // Set up the secondary display as the display on which the pointer should be shown.
5279 // The InputDevice is not associated with any display.
5280 prepareSecondaryDisplay();
5281 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08005282 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5283
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005284 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005285 mFakePointerController->setPosition(100, 200);
5286 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005287
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005288 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005289 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5290 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5291 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005293 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5294 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5295 WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01005296 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005297}
5298
5299TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
5300 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5301
5302 // Set up the default display.
5303 prepareDisplay(DISPLAY_ORIENTATION_90);
5304
5305 // Set up the secondary display as the display on which the pointer should be shown,
5306 // and associate the InputDevice with the secondary display.
5307 prepareSecondaryDisplay();
5308 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5309 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5310 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5311
5312 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5313 mFakePointerController->setPosition(100, 200);
5314 mFakePointerController->setButtonState(0);
5315
5316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5317 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5318 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005320 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5321 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5322 WithCoords(110.0f, 220.0f))));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005323 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
5324}
5325
5326TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
5327 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5328
5329 // Set up the default display as the display on which the pointer should be shown.
5330 prepareDisplay(DISPLAY_ORIENTATION_90);
5331 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5332
5333 // Associate the InputDevice with the secondary display.
5334 prepareSecondaryDisplay();
5335 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5336 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5337
5338 // The mapper should not generate any events because it is associated with a display that is
5339 // different from the pointer display.
5340 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5341 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5342 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005344}
5345
Michael Wrightd02c5b62014-02-10 15:10:22 -08005346// --- TouchInputMapperTest ---
5347
5348class TouchInputMapperTest : public InputMapperTest {
5349protected:
5350 static const int32_t RAW_X_MIN;
5351 static const int32_t RAW_X_MAX;
5352 static const int32_t RAW_Y_MIN;
5353 static const int32_t RAW_Y_MAX;
5354 static const int32_t RAW_TOUCH_MIN;
5355 static const int32_t RAW_TOUCH_MAX;
5356 static const int32_t RAW_TOOL_MIN;
5357 static const int32_t RAW_TOOL_MAX;
5358 static const int32_t RAW_PRESSURE_MIN;
5359 static const int32_t RAW_PRESSURE_MAX;
5360 static const int32_t RAW_ORIENTATION_MIN;
5361 static const int32_t RAW_ORIENTATION_MAX;
5362 static const int32_t RAW_DISTANCE_MIN;
5363 static const int32_t RAW_DISTANCE_MAX;
5364 static const int32_t RAW_TILT_MIN;
5365 static const int32_t RAW_TILT_MAX;
5366 static const int32_t RAW_ID_MIN;
5367 static const int32_t RAW_ID_MAX;
5368 static const int32_t RAW_SLOT_MIN;
5369 static const int32_t RAW_SLOT_MAX;
5370 static const float X_PRECISION;
5371 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005372 static const float X_PRECISION_VIRTUAL;
5373 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005374
5375 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005376 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005377
5378 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5379
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005380 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005381 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005382
Michael Wrightd02c5b62014-02-10 15:10:22 -08005383 enum Axes {
5384 POSITION = 1 << 0,
5385 TOUCH = 1 << 1,
5386 TOOL = 1 << 2,
5387 PRESSURE = 1 << 3,
5388 ORIENTATION = 1 << 4,
5389 MINOR = 1 << 5,
5390 ID = 1 << 6,
5391 DISTANCE = 1 << 7,
5392 TILT = 1 << 8,
5393 SLOT = 1 << 9,
5394 TOOL_TYPE = 1 << 10,
5395 };
5396
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005397 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5398 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005399 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005400 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005401 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005402 int32_t toRawX(float displayX);
5403 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005404 int32_t toRotatedRawX(float displayX);
5405 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005406 float toCookedX(float rawX, float rawY);
5407 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005408 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005409 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005410 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005411 float toDisplayY(int32_t rawY, int32_t displayHeight);
5412
Michael Wrightd02c5b62014-02-10 15:10:22 -08005413};
5414
5415const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5416const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5417const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5418const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5419const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5420const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5421const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5422const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005423const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5424const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005425const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5426const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5427const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5428const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5429const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5430const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5431const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5432const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5433const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5434const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5435const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5436const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005437const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5438 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5439const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5440 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005441const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5442 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005443
5444const float TouchInputMapperTest::GEOMETRIC_SCALE =
5445 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5446 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5447
5448const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5449 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5450 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5451};
5452
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005453void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005454 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5455 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005456}
5457
5458void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5459 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5460 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005461}
5462
Santos Cordonfa5cf462017-04-05 10:37:00 -07005463void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005464 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5465 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5466 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005467}
5468
Michael Wrightd02c5b62014-02-10 15:10:22 -08005469void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005470 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5471 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5472 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5473 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005474}
5475
Jason Gerecke489fda82012-09-07 17:19:40 -07005476void TouchInputMapperTest::prepareLocationCalibration() {
5477 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5478}
5479
Michael Wrightd02c5b62014-02-10 15:10:22 -08005480int32_t TouchInputMapperTest::toRawX(float displayX) {
5481 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5482}
5483
5484int32_t TouchInputMapperTest::toRawY(float displayY) {
5485 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5486}
5487
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005488int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5489 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5490}
5491
5492int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5493 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5494}
5495
Jason Gerecke489fda82012-09-07 17:19:40 -07005496float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5497 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5498 return rawX;
5499}
5500
5501float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5502 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5503 return rawY;
5504}
5505
Michael Wrightd02c5b62014-02-10 15:10:22 -08005506float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005507 return toDisplayX(rawX, DISPLAY_WIDTH);
5508}
5509
5510float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5511 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005512}
5513
5514float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005515 return toDisplayY(rawY, DISPLAY_HEIGHT);
5516}
5517
5518float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5519 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005520}
5521
5522
5523// --- SingleTouchInputMapperTest ---
5524
5525class SingleTouchInputMapperTest : public TouchInputMapperTest {
5526protected:
5527 void prepareButtons();
5528 void prepareAxes(int axes);
5529
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005530 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5531 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5532 void processUp(SingleTouchInputMapper& mappery);
5533 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5534 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5535 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5536 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5537 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5538 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005539};
5540
5541void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005542 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005543}
5544
5545void SingleTouchInputMapperTest::prepareAxes(int axes) {
5546 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005547 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5548 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005549 }
5550 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005551 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5552 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005553 }
5554 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005555 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5556 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005557 }
5558 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005559 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5560 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005561 }
5562 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005563 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5564 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005565 }
5566}
5567
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005568void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005569 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5570 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5571 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005572}
5573
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005574void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005575 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5576 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005577}
5578
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005579void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005580 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005581}
5582
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005583void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005584 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005585}
5586
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005587void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5588 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005589 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005590}
5591
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005592void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005593 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005594}
5595
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005596void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5597 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005598 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5599 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005600}
5601
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005602void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5603 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005604 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005605}
5606
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005607void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005608 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005609}
5610
Michael Wrightd02c5b62014-02-10 15:10:22 -08005611TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005612 prepareButtons();
5613 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005614 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005615
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005616 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005617}
5618
Michael Wrightd02c5b62014-02-10 15:10:22 -08005619TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005620 prepareButtons();
5621 prepareAxes(POSITION);
5622 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005623 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005624
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005625 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005626}
5627
5628TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005629 addConfigurationProperty("touch.deviceType", "touchScreen");
5630 prepareDisplay(DISPLAY_ORIENTATION_0);
5631 prepareButtons();
5632 prepareAxes(POSITION);
5633 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005634 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005635
5636 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005637 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005638
5639 // Virtual key is down.
5640 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5641 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5642 processDown(mapper, x, y);
5643 processSync(mapper);
5644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5645
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005646 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005647
5648 // Virtual key is up.
5649 processUp(mapper);
5650 processSync(mapper);
5651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5652
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005653 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005654}
5655
5656TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005657 addConfigurationProperty("touch.deviceType", "touchScreen");
5658 prepareDisplay(DISPLAY_ORIENTATION_0);
5659 prepareButtons();
5660 prepareAxes(POSITION);
5661 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005662 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005663
5664 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005665 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005666
5667 // Virtual key is down.
5668 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5669 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5670 processDown(mapper, x, y);
5671 processSync(mapper);
5672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5673
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005674 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005675
5676 // Virtual key is up.
5677 processUp(mapper);
5678 processSync(mapper);
5679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5680
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005681 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005682}
5683
5684TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005685 addConfigurationProperty("touch.deviceType", "touchScreen");
5686 prepareDisplay(DISPLAY_ORIENTATION_0);
5687 prepareButtons();
5688 prepareAxes(POSITION);
5689 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005690 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005691
Michael Wrightd02c5b62014-02-10 15:10:22 -08005692 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005693 ASSERT_TRUE(
5694 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005695 ASSERT_TRUE(flags[0]);
5696 ASSERT_FALSE(flags[1]);
5697}
5698
5699TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005700 addConfigurationProperty("touch.deviceType", "touchScreen");
5701 prepareDisplay(DISPLAY_ORIENTATION_0);
5702 prepareButtons();
5703 prepareAxes(POSITION);
5704 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005705 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005706
arthurhungdcef2dc2020-08-11 14:47:50 +08005707 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005708
5709 NotifyKeyArgs args;
5710
5711 // Press virtual key.
5712 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5713 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5714 processDown(mapper, x, y);
5715 processSync(mapper);
5716
5717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5718 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5719 ASSERT_EQ(DEVICE_ID, args.deviceId);
5720 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5721 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5722 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5723 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5724 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5725 ASSERT_EQ(KEY_HOME, args.scanCode);
5726 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5727 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5728
5729 // Release virtual key.
5730 processUp(mapper);
5731 processSync(mapper);
5732
5733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5734 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5735 ASSERT_EQ(DEVICE_ID, args.deviceId);
5736 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5737 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5738 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5739 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5740 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5741 ASSERT_EQ(KEY_HOME, args.scanCode);
5742 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5743 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5744
5745 // Should not have sent any motions.
5746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5747}
5748
5749TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005750 addConfigurationProperty("touch.deviceType", "touchScreen");
5751 prepareDisplay(DISPLAY_ORIENTATION_0);
5752 prepareButtons();
5753 prepareAxes(POSITION);
5754 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005755 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005756
arthurhungdcef2dc2020-08-11 14:47:50 +08005757 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005758
5759 NotifyKeyArgs keyArgs;
5760
5761 // Press virtual key.
5762 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5763 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5764 processDown(mapper, x, y);
5765 processSync(mapper);
5766
5767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5768 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5769 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5770 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5771 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5772 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5773 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5774 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5775 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5776 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5777 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5778
5779 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5780 // into the display area.
5781 y -= 100;
5782 processMove(mapper, x, y);
5783 processSync(mapper);
5784
5785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5786 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5787 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5788 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5789 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5790 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5791 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5792 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5793 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5794 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5795 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5796 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5797
5798 NotifyMotionArgs motionArgs;
5799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5800 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5801 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5802 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5803 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5804 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5805 ASSERT_EQ(0, motionArgs.flags);
5806 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5807 ASSERT_EQ(0, motionArgs.buttonState);
5808 ASSERT_EQ(0, motionArgs.edgeFlags);
5809 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5810 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5811 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5812 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5813 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5814 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5815 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5816 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5817
5818 // Keep moving out of bounds. Should generate a pointer move.
5819 y -= 50;
5820 processMove(mapper, x, y);
5821 processSync(mapper);
5822
5823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5824 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5825 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5826 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5827 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5828 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5829 ASSERT_EQ(0, motionArgs.flags);
5830 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5831 ASSERT_EQ(0, motionArgs.buttonState);
5832 ASSERT_EQ(0, motionArgs.edgeFlags);
5833 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5834 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5835 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5836 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5837 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5838 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5839 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5840 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5841
5842 // Release out of bounds. Should generate a pointer up.
5843 processUp(mapper);
5844 processSync(mapper);
5845
5846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5847 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5848 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5849 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5850 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5851 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5852 ASSERT_EQ(0, motionArgs.flags);
5853 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5854 ASSERT_EQ(0, motionArgs.buttonState);
5855 ASSERT_EQ(0, motionArgs.edgeFlags);
5856 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5857 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5858 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5859 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5860 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5861 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5862 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5863 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5864
5865 // Should not have sent any more keys or motions.
5866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5868}
5869
5870TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005871 addConfigurationProperty("touch.deviceType", "touchScreen");
5872 prepareDisplay(DISPLAY_ORIENTATION_0);
5873 prepareButtons();
5874 prepareAxes(POSITION);
5875 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005876 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005877
arthurhungdcef2dc2020-08-11 14:47:50 +08005878 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005879
5880 NotifyMotionArgs motionArgs;
5881
5882 // Initially go down out of bounds.
5883 int32_t x = -10;
5884 int32_t y = -10;
5885 processDown(mapper, x, y);
5886 processSync(mapper);
5887
5888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5889
5890 // Move into the display area. Should generate a pointer down.
5891 x = 50;
5892 y = 75;
5893 processMove(mapper, x, y);
5894 processSync(mapper);
5895
5896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5897 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5898 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5899 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5900 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5901 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5902 ASSERT_EQ(0, motionArgs.flags);
5903 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5904 ASSERT_EQ(0, motionArgs.buttonState);
5905 ASSERT_EQ(0, motionArgs.edgeFlags);
5906 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5907 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5908 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5909 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5910 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5911 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5912 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5913 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5914
5915 // Release. Should generate a pointer up.
5916 processUp(mapper);
5917 processSync(mapper);
5918
5919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5920 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5921 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5922 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5923 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5924 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5925 ASSERT_EQ(0, motionArgs.flags);
5926 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5927 ASSERT_EQ(0, motionArgs.buttonState);
5928 ASSERT_EQ(0, motionArgs.edgeFlags);
5929 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5930 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5931 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5933 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5934 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5935 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5936 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5937
5938 // Should not have sent any more keys or motions.
5939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5941}
5942
Santos Cordonfa5cf462017-04-05 10:37:00 -07005943TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005944 addConfigurationProperty("touch.deviceType", "touchScreen");
5945 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5946
5947 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5948 prepareButtons();
5949 prepareAxes(POSITION);
5950 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005951 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005952
arthurhungdcef2dc2020-08-11 14:47:50 +08005953 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005954
5955 NotifyMotionArgs motionArgs;
5956
5957 // Down.
5958 int32_t x = 100;
5959 int32_t y = 125;
5960 processDown(mapper, x, y);
5961 processSync(mapper);
5962
5963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5964 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5965 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5966 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5967 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5968 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5969 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5970 ASSERT_EQ(0, motionArgs.flags);
5971 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5972 ASSERT_EQ(0, motionArgs.buttonState);
5973 ASSERT_EQ(0, motionArgs.edgeFlags);
5974 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5975 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5976 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5977 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5978 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5979 1, 0, 0, 0, 0, 0, 0, 0));
5980 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5981 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5982 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5983
5984 // Move.
5985 x += 50;
5986 y += 75;
5987 processMove(mapper, x, y);
5988 processSync(mapper);
5989
5990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5991 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5992 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5993 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5994 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5995 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5996 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5997 ASSERT_EQ(0, motionArgs.flags);
5998 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5999 ASSERT_EQ(0, motionArgs.buttonState);
6000 ASSERT_EQ(0, motionArgs.edgeFlags);
6001 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6002 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6003 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6004 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6005 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6006 1, 0, 0, 0, 0, 0, 0, 0));
6007 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6008 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6009 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6010
6011 // Up.
6012 processUp(mapper);
6013 processSync(mapper);
6014
6015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6016 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6017 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6018 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6019 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6020 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6021 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6022 ASSERT_EQ(0, motionArgs.flags);
6023 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6024 ASSERT_EQ(0, motionArgs.buttonState);
6025 ASSERT_EQ(0, motionArgs.edgeFlags);
6026 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6027 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6028 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6029 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6030 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6031 1, 0, 0, 0, 0, 0, 0, 0));
6032 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6033 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6034 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6035
6036 // Should not have sent any more keys or motions.
6037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6039}
6040
Michael Wrightd02c5b62014-02-10 15:10:22 -08006041TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006042 addConfigurationProperty("touch.deviceType", "touchScreen");
6043 prepareDisplay(DISPLAY_ORIENTATION_0);
6044 prepareButtons();
6045 prepareAxes(POSITION);
6046 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006047 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006048
arthurhungdcef2dc2020-08-11 14:47:50 +08006049 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006050
6051 NotifyMotionArgs motionArgs;
6052
6053 // Down.
6054 int32_t x = 100;
6055 int32_t y = 125;
6056 processDown(mapper, x, y);
6057 processSync(mapper);
6058
6059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6060 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6061 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6062 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6063 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6064 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6065 ASSERT_EQ(0, motionArgs.flags);
6066 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6067 ASSERT_EQ(0, motionArgs.buttonState);
6068 ASSERT_EQ(0, motionArgs.edgeFlags);
6069 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6070 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6071 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6072 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6073 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6074 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6075 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6076 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6077
6078 // Move.
6079 x += 50;
6080 y += 75;
6081 processMove(mapper, x, y);
6082 processSync(mapper);
6083
6084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6085 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6086 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6087 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6088 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6089 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6090 ASSERT_EQ(0, motionArgs.flags);
6091 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6092 ASSERT_EQ(0, motionArgs.buttonState);
6093 ASSERT_EQ(0, motionArgs.edgeFlags);
6094 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6095 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6096 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6097 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6098 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6099 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6100 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6101 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6102
6103 // Up.
6104 processUp(mapper);
6105 processSync(mapper);
6106
6107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6108 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6109 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6110 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6111 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6112 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6113 ASSERT_EQ(0, motionArgs.flags);
6114 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6115 ASSERT_EQ(0, motionArgs.buttonState);
6116 ASSERT_EQ(0, motionArgs.edgeFlags);
6117 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6118 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6119 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6120 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6121 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6122 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6123 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6124 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6125
6126 // Should not have sent any more keys or motions.
6127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6129}
6130
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006131TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006132 addConfigurationProperty("touch.deviceType", "touchScreen");
6133 prepareButtons();
6134 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006135 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
6136 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006137 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006138
6139 NotifyMotionArgs args;
6140
6141 // Rotation 90.
6142 prepareDisplay(DISPLAY_ORIENTATION_90);
6143 processDown(mapper, toRawX(50), toRawY(75));
6144 processSync(mapper);
6145
6146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6147 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6148 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6149
6150 processUp(mapper);
6151 processSync(mapper);
6152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6153}
6154
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006155TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006156 addConfigurationProperty("touch.deviceType", "touchScreen");
6157 prepareButtons();
6158 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006159 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6160 // orientation-aware are affected by display rotation.
6161 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006162 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006163
6164 NotifyMotionArgs args;
6165
6166 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006167 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006168 prepareDisplay(DISPLAY_ORIENTATION_0);
6169 processDown(mapper, toRawX(50), toRawY(75));
6170 processSync(mapper);
6171
6172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6173 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6174 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6175
6176 processUp(mapper);
6177 processSync(mapper);
6178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6179
6180 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006181 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006182 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006183 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006184 processSync(mapper);
6185
6186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6187 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6188 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6189
6190 processUp(mapper);
6191 processSync(mapper);
6192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6193
6194 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006195 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006196 prepareDisplay(DISPLAY_ORIENTATION_180);
6197 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6198 processSync(mapper);
6199
6200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6201 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6202 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6203
6204 processUp(mapper);
6205 processSync(mapper);
6206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6207
6208 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006209 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006210 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006211 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006212 processSync(mapper);
6213
6214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6215 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6216 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6217
6218 processUp(mapper);
6219 processSync(mapper);
6220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6221}
6222
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006223TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6224 addConfigurationProperty("touch.deviceType", "touchScreen");
6225 prepareButtons();
6226 prepareAxes(POSITION);
6227 addConfigurationProperty("touch.orientationAware", "1");
6228 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6229 clearViewports();
6230 prepareDisplay(DISPLAY_ORIENTATION_0);
6231 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6232 NotifyMotionArgs args;
6233
6234 // Orientation 0.
6235 processDown(mapper, toRawX(50), toRawY(75));
6236 processSync(mapper);
6237
6238 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6239 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6240 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6241
6242 processUp(mapper);
6243 processSync(mapper);
6244 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6245}
6246
6247TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6248 addConfigurationProperty("touch.deviceType", "touchScreen");
6249 prepareButtons();
6250 prepareAxes(POSITION);
6251 addConfigurationProperty("touch.orientationAware", "1");
6252 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6253 clearViewports();
6254 prepareDisplay(DISPLAY_ORIENTATION_0);
6255 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6256 NotifyMotionArgs args;
6257
6258 // Orientation 90.
6259 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6260 processSync(mapper);
6261
6262 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6263 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6264 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6265
6266 processUp(mapper);
6267 processSync(mapper);
6268 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6269}
6270
6271TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6272 addConfigurationProperty("touch.deviceType", "touchScreen");
6273 prepareButtons();
6274 prepareAxes(POSITION);
6275 addConfigurationProperty("touch.orientationAware", "1");
6276 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6277 clearViewports();
6278 prepareDisplay(DISPLAY_ORIENTATION_0);
6279 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6280 NotifyMotionArgs args;
6281
6282 // Orientation 180.
6283 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6284 processSync(mapper);
6285
6286 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6287 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6288 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6289
6290 processUp(mapper);
6291 processSync(mapper);
6292 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6293}
6294
6295TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6296 addConfigurationProperty("touch.deviceType", "touchScreen");
6297 prepareButtons();
6298 prepareAxes(POSITION);
6299 addConfigurationProperty("touch.orientationAware", "1");
6300 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6301 clearViewports();
6302 prepareDisplay(DISPLAY_ORIENTATION_0);
6303 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6304 NotifyMotionArgs args;
6305
6306 // Orientation 270.
6307 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6308 processSync(mapper);
6309
6310 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6311 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6312 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6313
6314 processUp(mapper);
6315 processSync(mapper);
6316 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6317}
6318
6319TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6320 addConfigurationProperty("touch.deviceType", "touchScreen");
6321 prepareButtons();
6322 prepareAxes(POSITION);
6323 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6324 // orientation-aware are affected by display rotation.
6325 addConfigurationProperty("touch.orientationAware", "0");
6326 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6327 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6328
6329 NotifyMotionArgs args;
6330
6331 // Orientation 90, Rotation 0.
6332 clearViewports();
6333 prepareDisplay(DISPLAY_ORIENTATION_0);
6334 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6335 processSync(mapper);
6336
6337 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6338 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6339 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6340
6341 processUp(mapper);
6342 processSync(mapper);
6343 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6344
6345 // Orientation 90, Rotation 90.
6346 clearViewports();
6347 prepareDisplay(DISPLAY_ORIENTATION_90);
6348 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6349 processSync(mapper);
6350
6351 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6352 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6353 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6354
6355 processUp(mapper);
6356 processSync(mapper);
6357 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6358
6359 // Orientation 90, Rotation 180.
6360 clearViewports();
6361 prepareDisplay(DISPLAY_ORIENTATION_180);
6362 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6363 processSync(mapper);
6364
6365 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6366 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6367 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6368
6369 processUp(mapper);
6370 processSync(mapper);
6371 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6372
6373 // Orientation 90, Rotation 270.
6374 clearViewports();
6375 prepareDisplay(DISPLAY_ORIENTATION_270);
6376 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6377 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6378 processSync(mapper);
6379
6380 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6381 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6382 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6383
6384 processUp(mapper);
6385 processSync(mapper);
6386 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6387}
6388
Michael Wrightd02c5b62014-02-10 15:10:22 -08006389TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006390 addConfigurationProperty("touch.deviceType", "touchScreen");
6391 prepareDisplay(DISPLAY_ORIENTATION_0);
6392 prepareButtons();
6393 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006394 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006395
6396 // These calculations are based on the input device calibration documentation.
6397 int32_t rawX = 100;
6398 int32_t rawY = 200;
6399 int32_t rawPressure = 10;
6400 int32_t rawToolMajor = 12;
6401 int32_t rawDistance = 2;
6402 int32_t rawTiltX = 30;
6403 int32_t rawTiltY = 110;
6404
6405 float x = toDisplayX(rawX);
6406 float y = toDisplayY(rawY);
6407 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6408 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6409 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6410 float distance = float(rawDistance);
6411
6412 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6413 float tiltScale = M_PI / 180;
6414 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6415 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6416 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6417 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6418
6419 processDown(mapper, rawX, rawY);
6420 processPressure(mapper, rawPressure);
6421 processToolMajor(mapper, rawToolMajor);
6422 processDistance(mapper, rawDistance);
6423 processTilt(mapper, rawTiltX, rawTiltY);
6424 processSync(mapper);
6425
6426 NotifyMotionArgs args;
6427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6428 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6429 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6430 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6431}
6432
Jason Gerecke489fda82012-09-07 17:19:40 -07006433TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006434 addConfigurationProperty("touch.deviceType", "touchScreen");
6435 prepareDisplay(DISPLAY_ORIENTATION_0);
6436 prepareLocationCalibration();
6437 prepareButtons();
6438 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006439 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006440
6441 int32_t rawX = 100;
6442 int32_t rawY = 200;
6443
6444 float x = toDisplayX(toCookedX(rawX, rawY));
6445 float y = toDisplayY(toCookedY(rawX, rawY));
6446
6447 processDown(mapper, rawX, rawY);
6448 processSync(mapper);
6449
6450 NotifyMotionArgs args;
6451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6453 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6454}
6455
Michael Wrightd02c5b62014-02-10 15:10:22 -08006456TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006457 addConfigurationProperty("touch.deviceType", "touchScreen");
6458 prepareDisplay(DISPLAY_ORIENTATION_0);
6459 prepareButtons();
6460 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006461 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006462
6463 NotifyMotionArgs motionArgs;
6464 NotifyKeyArgs keyArgs;
6465
6466 processDown(mapper, 100, 200);
6467 processSync(mapper);
6468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6469 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6470 ASSERT_EQ(0, motionArgs.buttonState);
6471
6472 // press BTN_LEFT, release BTN_LEFT
6473 processKey(mapper, BTN_LEFT, 1);
6474 processSync(mapper);
6475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6476 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6477 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6478
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6480 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6481 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6482
Michael Wrightd02c5b62014-02-10 15:10:22 -08006483 processKey(mapper, BTN_LEFT, 0);
6484 processSync(mapper);
6485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006486 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006487 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006488
6489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006490 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006491 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006492
6493 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6494 processKey(mapper, BTN_RIGHT, 1);
6495 processKey(mapper, BTN_MIDDLE, 1);
6496 processSync(mapper);
6497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6498 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6499 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6500 motionArgs.buttonState);
6501
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6503 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6504 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6505
6506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6507 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6508 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6509 motionArgs.buttonState);
6510
Michael Wrightd02c5b62014-02-10 15:10:22 -08006511 processKey(mapper, BTN_RIGHT, 0);
6512 processSync(mapper);
6513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006514 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006515 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006516
6517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006518 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006519 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006520
6521 processKey(mapper, BTN_MIDDLE, 0);
6522 processSync(mapper);
6523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006524 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006525 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006526
6527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006528 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006529 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006530
6531 // press BTN_BACK, release BTN_BACK
6532 processKey(mapper, BTN_BACK, 1);
6533 processSync(mapper);
6534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6535 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6536 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006537
Michael Wrightd02c5b62014-02-10 15:10:22 -08006538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006539 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006540 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6541
6542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6543 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6544 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006545
6546 processKey(mapper, BTN_BACK, 0);
6547 processSync(mapper);
6548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006549 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006550 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006551
6552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006553 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006554 ASSERT_EQ(0, motionArgs.buttonState);
6555
Michael Wrightd02c5b62014-02-10 15:10:22 -08006556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6557 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6558 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6559
6560 // press BTN_SIDE, release BTN_SIDE
6561 processKey(mapper, BTN_SIDE, 1);
6562 processSync(mapper);
6563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6564 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6565 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006566
Michael Wrightd02c5b62014-02-10 15:10:22 -08006567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006568 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006569 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6570
6571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6572 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6573 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006574
6575 processKey(mapper, BTN_SIDE, 0);
6576 processSync(mapper);
6577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006578 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006579 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006580
6581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006582 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006583 ASSERT_EQ(0, motionArgs.buttonState);
6584
Michael Wrightd02c5b62014-02-10 15:10:22 -08006585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6586 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6587 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6588
6589 // press BTN_FORWARD, release BTN_FORWARD
6590 processKey(mapper, BTN_FORWARD, 1);
6591 processSync(mapper);
6592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6593 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6594 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006595
Michael Wrightd02c5b62014-02-10 15:10:22 -08006596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006597 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006598 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6599
6600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6601 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6602 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006603
6604 processKey(mapper, BTN_FORWARD, 0);
6605 processSync(mapper);
6606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006607 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006608 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006609
6610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006611 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006612 ASSERT_EQ(0, motionArgs.buttonState);
6613
Michael Wrightd02c5b62014-02-10 15:10:22 -08006614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6615 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6616 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6617
6618 // press BTN_EXTRA, release BTN_EXTRA
6619 processKey(mapper, BTN_EXTRA, 1);
6620 processSync(mapper);
6621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6622 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6623 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006624
Michael Wrightd02c5b62014-02-10 15:10:22 -08006625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006626 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006627 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6628
6629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6630 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6631 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006632
6633 processKey(mapper, BTN_EXTRA, 0);
6634 processSync(mapper);
6635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006636 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006637 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006638
6639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006640 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006641 ASSERT_EQ(0, motionArgs.buttonState);
6642
Michael Wrightd02c5b62014-02-10 15:10:22 -08006643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6644 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6645 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6646
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6648
Michael Wrightd02c5b62014-02-10 15:10:22 -08006649 // press BTN_STYLUS, release BTN_STYLUS
6650 processKey(mapper, BTN_STYLUS, 1);
6651 processSync(mapper);
6652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6653 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006654 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6655
6656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6657 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6658 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006659
6660 processKey(mapper, BTN_STYLUS, 0);
6661 processSync(mapper);
6662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006663 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006664 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006665
6666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006667 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006668 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006669
6670 // press BTN_STYLUS2, release BTN_STYLUS2
6671 processKey(mapper, BTN_STYLUS2, 1);
6672 processSync(mapper);
6673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6674 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006675 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6676
6677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6678 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6679 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006680
6681 processKey(mapper, BTN_STYLUS2, 0);
6682 processSync(mapper);
6683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006684 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006685 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006686
6687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006688 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006689 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006690
6691 // release touch
6692 processUp(mapper);
6693 processSync(mapper);
6694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6695 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6696 ASSERT_EQ(0, motionArgs.buttonState);
6697}
6698
6699TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006700 addConfigurationProperty("touch.deviceType", "touchScreen");
6701 prepareDisplay(DISPLAY_ORIENTATION_0);
6702 prepareButtons();
6703 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006704 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006705
6706 NotifyMotionArgs motionArgs;
6707
6708 // default tool type is finger
6709 processDown(mapper, 100, 200);
6710 processSync(mapper);
6711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6712 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6713 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6714
6715 // eraser
6716 processKey(mapper, BTN_TOOL_RUBBER, 1);
6717 processSync(mapper);
6718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6719 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6720 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6721
6722 // stylus
6723 processKey(mapper, BTN_TOOL_RUBBER, 0);
6724 processKey(mapper, BTN_TOOL_PEN, 1);
6725 processSync(mapper);
6726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6727 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6728 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6729
6730 // brush
6731 processKey(mapper, BTN_TOOL_PEN, 0);
6732 processKey(mapper, BTN_TOOL_BRUSH, 1);
6733 processSync(mapper);
6734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6735 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6736 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6737
6738 // pencil
6739 processKey(mapper, BTN_TOOL_BRUSH, 0);
6740 processKey(mapper, BTN_TOOL_PENCIL, 1);
6741 processSync(mapper);
6742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6743 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6744 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6745
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006746 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006747 processKey(mapper, BTN_TOOL_PENCIL, 0);
6748 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6749 processSync(mapper);
6750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6751 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6752 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6753
6754 // mouse
6755 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6756 processKey(mapper, BTN_TOOL_MOUSE, 1);
6757 processSync(mapper);
6758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6759 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6760 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6761
6762 // lens
6763 processKey(mapper, BTN_TOOL_MOUSE, 0);
6764 processKey(mapper, BTN_TOOL_LENS, 1);
6765 processSync(mapper);
6766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6767 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6768 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6769
6770 // double-tap
6771 processKey(mapper, BTN_TOOL_LENS, 0);
6772 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6773 processSync(mapper);
6774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6775 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6776 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6777
6778 // triple-tap
6779 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6780 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6781 processSync(mapper);
6782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6783 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6784 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6785
6786 // quad-tap
6787 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6788 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6789 processSync(mapper);
6790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6791 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6792 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6793
6794 // finger
6795 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6796 processKey(mapper, BTN_TOOL_FINGER, 1);
6797 processSync(mapper);
6798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6799 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6800 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6801
6802 // stylus trumps finger
6803 processKey(mapper, BTN_TOOL_PEN, 1);
6804 processSync(mapper);
6805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6806 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6807 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6808
6809 // eraser trumps stylus
6810 processKey(mapper, BTN_TOOL_RUBBER, 1);
6811 processSync(mapper);
6812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6813 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6814 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6815
6816 // mouse trumps eraser
6817 processKey(mapper, BTN_TOOL_MOUSE, 1);
6818 processSync(mapper);
6819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6820 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6821 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6822
6823 // back to default tool type
6824 processKey(mapper, BTN_TOOL_MOUSE, 0);
6825 processKey(mapper, BTN_TOOL_RUBBER, 0);
6826 processKey(mapper, BTN_TOOL_PEN, 0);
6827 processKey(mapper, BTN_TOOL_FINGER, 0);
6828 processSync(mapper);
6829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6830 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6831 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6832}
6833
6834TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006835 addConfigurationProperty("touch.deviceType", "touchScreen");
6836 prepareDisplay(DISPLAY_ORIENTATION_0);
6837 prepareButtons();
6838 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006839 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006840 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006841
6842 NotifyMotionArgs motionArgs;
6843
6844 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6845 processKey(mapper, BTN_TOOL_FINGER, 1);
6846 processMove(mapper, 100, 200);
6847 processSync(mapper);
6848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6849 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6851 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6852
6853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6854 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6855 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6856 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6857
6858 // move a little
6859 processMove(mapper, 150, 250);
6860 processSync(mapper);
6861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6862 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6863 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6864 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6865
6866 // down when BTN_TOUCH is pressed, pressure defaults to 1
6867 processKey(mapper, BTN_TOUCH, 1);
6868 processSync(mapper);
6869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6870 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6871 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6872 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6873
6874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6875 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6876 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6877 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6878
6879 // up when BTN_TOUCH is released, hover restored
6880 processKey(mapper, BTN_TOUCH, 0);
6881 processSync(mapper);
6882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6883 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6884 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6885 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6886
6887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6888 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6889 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6890 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6891
6892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6893 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6894 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6895 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6896
6897 // exit hover when pointer goes away
6898 processKey(mapper, BTN_TOOL_FINGER, 0);
6899 processSync(mapper);
6900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6901 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6902 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6903 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6904}
6905
6906TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006907 addConfigurationProperty("touch.deviceType", "touchScreen");
6908 prepareDisplay(DISPLAY_ORIENTATION_0);
6909 prepareButtons();
6910 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006911 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006912
6913 NotifyMotionArgs motionArgs;
6914
6915 // initially hovering because pressure is 0
6916 processDown(mapper, 100, 200);
6917 processPressure(mapper, 0);
6918 processSync(mapper);
6919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6920 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6921 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6922 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6923
6924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6925 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6926 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6927 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6928
6929 // move a little
6930 processMove(mapper, 150, 250);
6931 processSync(mapper);
6932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6933 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6934 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6935 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6936
6937 // down when pressure is non-zero
6938 processPressure(mapper, RAW_PRESSURE_MAX);
6939 processSync(mapper);
6940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6941 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6942 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6943 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6944
6945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6946 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6947 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6948 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6949
6950 // up when pressure becomes 0, hover restored
6951 processPressure(mapper, 0);
6952 processSync(mapper);
6953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6954 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6955 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6956 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6957
6958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6959 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6960 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6961 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6962
6963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6964 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6965 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6966 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6967
6968 // exit hover when pointer goes away
6969 processUp(mapper);
6970 processSync(mapper);
6971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6972 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6973 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6974 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6975}
6976
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006977TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6978 addConfigurationProperty("touch.deviceType", "touchScreen");
6979 prepareDisplay(DISPLAY_ORIENTATION_0);
6980 prepareButtons();
6981 prepareAxes(POSITION | PRESSURE);
6982 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6983
6984 // Touch down.
6985 processDown(mapper, 100, 200);
6986 processPressure(mapper, 1);
6987 processSync(mapper);
6988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6989 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6990
6991 // Reset the mapper. This should cancel the ongoing gesture.
6992 resetMapper(mapper, ARBITRARY_TIME);
6993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6994 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6995
6996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6997}
6998
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006999TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
7000 addConfigurationProperty("touch.deviceType", "touchScreen");
7001 prepareDisplay(DISPLAY_ORIENTATION_0);
7002 prepareButtons();
7003 prepareAxes(POSITION | PRESSURE);
7004 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7005
7006 // Set the initial state for the touch pointer.
7007 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
7008 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
7009 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
7010 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7011
7012 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007013 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
7014 // does not generate any events.
7015 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007016
7017 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
7018 // the recreated touch state to generate a down event.
7019 processSync(mapper);
7020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7021 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
7022
7023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7024}
7025
lilinnan687e58f2022-07-19 16:00:50 +08007026TEST_F(SingleTouchInputMapperTest,
7027 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
7028 addConfigurationProperty("touch.deviceType", "touchScreen");
7029 prepareDisplay(DISPLAY_ORIENTATION_0);
7030 prepareButtons();
7031 prepareAxes(POSITION);
7032 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7033 NotifyMotionArgs motionArgs;
7034
7035 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00007036 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08007037 processSync(mapper);
7038
7039 // We should receive a down event
7040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7041 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7042
7043 // Change display id
7044 clearViewports();
7045 prepareSecondaryDisplay(ViewportType::INTERNAL);
7046
7047 // We should receive a cancel event
7048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7049 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7050 // Then receive reset called
7051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7052}
7053
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007054TEST_F(SingleTouchInputMapperTest,
7055 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
7056 addConfigurationProperty("touch.deviceType", "touchScreen");
7057 prepareDisplay(DISPLAY_ORIENTATION_0);
7058 prepareButtons();
7059 prepareAxes(POSITION);
7060 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7062 NotifyMotionArgs motionArgs;
7063
7064 // Start a new gesture.
7065 processDown(mapper, 100, 200);
7066 processSync(mapper);
7067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7068 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7069
7070 // Make the viewport inactive. This will put the device in disabled mode.
7071 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7072 viewport->isActive = false;
7073 mFakePolicy->updateViewport(*viewport);
7074 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7075
7076 // We should receive a cancel event for the ongoing gesture.
7077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7078 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7079 // Then we should be notified that the device was reset.
7080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7081
7082 // No events are generated while the viewport is inactive.
7083 processMove(mapper, 101, 201);
7084 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007085 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007086 processSync(mapper);
7087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7088
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007089 // Start a new gesture while the viewport is still inactive.
7090 processDown(mapper, 300, 400);
7091 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
7092 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
7093 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7094 processSync(mapper);
7095
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007096 // Make the viewport active again. The device should resume processing events.
7097 viewport->isActive = true;
7098 mFakePolicy->updateViewport(*viewport);
7099 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7100
7101 // The device is reset because it changes back to direct mode, without generating any events.
7102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7104
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007105 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007106 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7108 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007109
7110 // No more events.
7111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
7113}
7114
Prabir Pradhan211ba622022-10-31 21:09:21 +00007115TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
7116 addConfigurationProperty("touch.deviceType", "touchScreen");
7117 prepareDisplay(DISPLAY_ORIENTATION_0);
7118 prepareButtons();
7119 prepareAxes(POSITION);
7120 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7122
7123 // Press a stylus button.
7124 processKey(mapper, BTN_STYLUS, 1);
7125 processSync(mapper);
7126
7127 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
7128 processDown(mapper, 100, 200);
7129 processSync(mapper);
7130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7131 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7132 WithCoords(toDisplayX(100), toDisplayY(200)),
7133 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7135 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7136 WithCoords(toDisplayX(100), toDisplayY(200)),
7137 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7138
7139 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
7140 // the button has not actually been released, since there will be no pointers through which the
7141 // button state can be reported. The event is generated at the location of the pointer before
7142 // it went up.
7143 processUp(mapper);
7144 processSync(mapper);
7145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7146 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7147 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
7148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7149 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7150 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
7151}
7152
Prabir Pradhan5632d622021-09-06 07:57:20 -07007153// --- TouchDisplayProjectionTest ---
7154
7155class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
7156public:
7157 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
7158 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
7159 // rotated equivalent of the given un-rotated physical display bounds.
7160 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
7161 uint32_t inverseRotationFlags;
7162 auto width = DISPLAY_WIDTH;
7163 auto height = DISPLAY_HEIGHT;
7164 switch (orientation) {
7165 case DISPLAY_ORIENTATION_90:
7166 inverseRotationFlags = ui::Transform::ROT_270;
7167 std::swap(width, height);
7168 break;
7169 case DISPLAY_ORIENTATION_180:
7170 inverseRotationFlags = ui::Transform::ROT_180;
7171 break;
7172 case DISPLAY_ORIENTATION_270:
7173 inverseRotationFlags = ui::Transform::ROT_90;
7174 std::swap(width, height);
7175 break;
7176 case DISPLAY_ORIENTATION_0:
7177 inverseRotationFlags = ui::Transform::ROT_0;
7178 break;
7179 default:
7180 FAIL() << "Invalid orientation: " << orientation;
7181 }
7182
7183 const ui::Transform rotation(inverseRotationFlags, width, height);
7184 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
7185
7186 std::optional<DisplayViewport> internalViewport =
7187 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7188 DisplayViewport& v = *internalViewport;
7189 v.displayId = DISPLAY_ID;
7190 v.orientation = orientation;
7191
7192 v.logicalLeft = 0;
7193 v.logicalTop = 0;
7194 v.logicalRight = 100;
7195 v.logicalBottom = 100;
7196
7197 v.physicalLeft = rotatedPhysicalDisplay.left;
7198 v.physicalTop = rotatedPhysicalDisplay.top;
7199 v.physicalRight = rotatedPhysicalDisplay.right;
7200 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7201
7202 v.deviceWidth = width;
7203 v.deviceHeight = height;
7204
7205 v.isActive = true;
7206 v.uniqueId = UNIQUE_ID;
7207 v.type = ViewportType::INTERNAL;
7208 mFakePolicy->updateViewport(v);
7209 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7210 }
7211
7212 void assertReceivedMove(const Point& point) {
7213 NotifyMotionArgs motionArgs;
7214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7215 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7216 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7217 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7218 1, 0, 0, 0, 0, 0, 0, 0));
7219 }
7220};
7221
7222TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7223 addConfigurationProperty("touch.deviceType", "touchScreen");
7224 prepareDisplay(DISPLAY_ORIENTATION_0);
7225
7226 prepareButtons();
7227 prepareAxes(POSITION);
7228 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7229
7230 NotifyMotionArgs motionArgs;
7231
7232 // Configure the DisplayViewport such that the logical display maps to a subsection of
7233 // the display panel called the physical display. Here, the physical display is bounded by the
7234 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7235 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7236 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7237 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7238
7239 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7240 DISPLAY_ORIENTATION_270}) {
7241 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7242
7243 // Touches outside the physical display should be ignored, and should not generate any
7244 // events. Ensure touches at the following points that lie outside of the physical display
7245 // area do not generate any events.
7246 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7247 processDown(mapper, toRawX(point.x), toRawY(point.y));
7248 processSync(mapper);
7249 processUp(mapper);
7250 processSync(mapper);
7251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7252 << "Unexpected event generated for touch outside physical display at point: "
7253 << point.x << ", " << point.y;
7254 }
7255 }
7256}
7257
7258TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7259 addConfigurationProperty("touch.deviceType", "touchScreen");
7260 prepareDisplay(DISPLAY_ORIENTATION_0);
7261
7262 prepareButtons();
7263 prepareAxes(POSITION);
7264 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7265
7266 NotifyMotionArgs motionArgs;
7267
7268 // Configure the DisplayViewport such that the logical display maps to a subsection of
7269 // the display panel called the physical display. Here, the physical display is bounded by the
7270 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7271 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7272
7273 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7274 DISPLAY_ORIENTATION_270}) {
7275 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7276
7277 // Touches that start outside the physical display should be ignored until it enters the
7278 // physical display bounds, at which point it should generate a down event. Start a touch at
7279 // the point (5, 100), which is outside the physical display bounds.
7280 static const Point kOutsidePoint{5, 100};
7281 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7282 processSync(mapper);
7283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7284
7285 // Move the touch into the physical display area. This should generate a pointer down.
7286 processMove(mapper, toRawX(11), toRawY(21));
7287 processSync(mapper);
7288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7289 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7290 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7291 ASSERT_NO_FATAL_FAILURE(
7292 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7293
7294 // Move the touch inside the physical display area. This should generate a pointer move.
7295 processMove(mapper, toRawX(69), toRawY(159));
7296 processSync(mapper);
7297 assertReceivedMove({69, 159});
7298
7299 // Move outside the physical display area. Since the pointer is already down, this should
7300 // now continue generating events.
7301 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7302 processSync(mapper);
7303 assertReceivedMove(kOutsidePoint);
7304
7305 // Release. This should generate a pointer up.
7306 processUp(mapper);
7307 processSync(mapper);
7308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7309 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7310 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7311 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7312
7313 // Ensure no more events were generated.
7314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7316 }
7317}
7318
Michael Wrightd02c5b62014-02-10 15:10:22 -08007319// --- MultiTouchInputMapperTest ---
7320
7321class MultiTouchInputMapperTest : public TouchInputMapperTest {
7322protected:
7323 void prepareAxes(int axes);
7324
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007325 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7326 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7327 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7328 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7329 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7330 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7331 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7332 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7333 void processId(MultiTouchInputMapper& mapper, int32_t id);
7334 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7335 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7336 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
7337 void processMTSync(MultiTouchInputMapper& mapper);
7338 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007339};
7340
7341void MultiTouchInputMapperTest::prepareAxes(int axes) {
7342 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007343 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7344 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007345 }
7346 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007347 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7348 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007349 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007350 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7351 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007352 }
7353 }
7354 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007355 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7356 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007357 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007358 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007359 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007360 }
7361 }
7362 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007363 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7364 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007365 }
7366 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007367 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7368 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007369 }
7370 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007371 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7372 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007373 }
7374 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007375 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7376 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007377 }
7378 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007379 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7380 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007381 }
7382 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007383 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007384 }
7385}
7386
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007387void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7388 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007389 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7390 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007391}
7392
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007393void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7394 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007395 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007396}
7397
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007398void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7399 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007400 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007401}
7402
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007403void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007404 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007405}
7406
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007407void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007409}
7410
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007411void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7412 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007413 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007414}
7415
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007416void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007417 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007418}
7419
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007420void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007421 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007422}
7423
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007424void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007425 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007426}
7427
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007428void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007429 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007430}
7431
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007432void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007433 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007434}
7435
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007436void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7437 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007438 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007439}
7440
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007441void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007442 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007443}
7444
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007445void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007447}
7448
Michael Wrightd02c5b62014-02-10 15:10:22 -08007449TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007450 addConfigurationProperty("touch.deviceType", "touchScreen");
7451 prepareDisplay(DISPLAY_ORIENTATION_0);
7452 prepareAxes(POSITION);
7453 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007454 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007455
arthurhungdcef2dc2020-08-11 14:47:50 +08007456 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007457
7458 NotifyMotionArgs motionArgs;
7459
7460 // Two fingers down at once.
7461 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7462 processPosition(mapper, x1, y1);
7463 processMTSync(mapper);
7464 processPosition(mapper, x2, y2);
7465 processMTSync(mapper);
7466 processSync(mapper);
7467
7468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7469 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7470 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7471 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7472 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7473 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7474 ASSERT_EQ(0, motionArgs.flags);
7475 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7476 ASSERT_EQ(0, motionArgs.buttonState);
7477 ASSERT_EQ(0, motionArgs.edgeFlags);
7478 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7479 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7480 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7481 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7482 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7483 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7484 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7485 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7486
7487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7488 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7489 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7490 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7491 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007492 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007493 ASSERT_EQ(0, motionArgs.flags);
7494 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7495 ASSERT_EQ(0, motionArgs.buttonState);
7496 ASSERT_EQ(0, motionArgs.edgeFlags);
7497 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7498 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7499 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7500 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7501 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7502 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7503 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7504 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7505 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7506 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7507 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7508 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7509
7510 // Move.
7511 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7512 processPosition(mapper, x1, y1);
7513 processMTSync(mapper);
7514 processPosition(mapper, x2, y2);
7515 processMTSync(mapper);
7516 processSync(mapper);
7517
7518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7519 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7520 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7521 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7522 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7523 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7524 ASSERT_EQ(0, motionArgs.flags);
7525 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7526 ASSERT_EQ(0, motionArgs.buttonState);
7527 ASSERT_EQ(0, motionArgs.edgeFlags);
7528 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7529 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7530 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7531 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7532 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7534 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7535 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7536 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7537 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7538 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7539 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7540
7541 // First finger up.
7542 x2 += 15; y2 -= 20;
7543 processPosition(mapper, x2, y2);
7544 processMTSync(mapper);
7545 processSync(mapper);
7546
7547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7548 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7549 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7550 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7551 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007552 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007553 ASSERT_EQ(0, motionArgs.flags);
7554 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7555 ASSERT_EQ(0, motionArgs.buttonState);
7556 ASSERT_EQ(0, motionArgs.edgeFlags);
7557 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7558 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7559 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7560 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7561 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7562 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7563 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7564 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7565 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7566 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7567 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7568 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7569
7570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7571 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7572 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7573 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7574 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7575 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7576 ASSERT_EQ(0, motionArgs.flags);
7577 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7578 ASSERT_EQ(0, motionArgs.buttonState);
7579 ASSERT_EQ(0, motionArgs.edgeFlags);
7580 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7581 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7582 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7584 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7585 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7586 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7587 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7588
7589 // Move.
7590 x2 += 20; y2 -= 25;
7591 processPosition(mapper, x2, y2);
7592 processMTSync(mapper);
7593 processSync(mapper);
7594
7595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7596 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7597 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7598 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7599 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7600 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7601 ASSERT_EQ(0, motionArgs.flags);
7602 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7603 ASSERT_EQ(0, motionArgs.buttonState);
7604 ASSERT_EQ(0, motionArgs.edgeFlags);
7605 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7606 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7607 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7608 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7609 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7610 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7611 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7612 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7613
7614 // New finger down.
7615 int32_t x3 = 700, y3 = 300;
7616 processPosition(mapper, x2, y2);
7617 processMTSync(mapper);
7618 processPosition(mapper, x3, y3);
7619 processMTSync(mapper);
7620 processSync(mapper);
7621
7622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7623 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7624 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7625 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7626 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007627 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007628 ASSERT_EQ(0, motionArgs.flags);
7629 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7630 ASSERT_EQ(0, motionArgs.buttonState);
7631 ASSERT_EQ(0, motionArgs.edgeFlags);
7632 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7633 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7634 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7635 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7636 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7638 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7640 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7641 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7642 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7643 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7644
7645 // Second finger up.
7646 x3 += 30; y3 -= 20;
7647 processPosition(mapper, x3, y3);
7648 processMTSync(mapper);
7649 processSync(mapper);
7650
7651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7652 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7653 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7654 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7655 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007656 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007657 ASSERT_EQ(0, motionArgs.flags);
7658 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7659 ASSERT_EQ(0, motionArgs.buttonState);
7660 ASSERT_EQ(0, motionArgs.edgeFlags);
7661 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7662 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7663 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7664 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7665 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7666 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7667 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7669 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7670 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7671 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7672 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7673
7674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7675 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7676 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7677 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7678 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7679 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7680 ASSERT_EQ(0, motionArgs.flags);
7681 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7682 ASSERT_EQ(0, motionArgs.buttonState);
7683 ASSERT_EQ(0, motionArgs.edgeFlags);
7684 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7685 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7686 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7687 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7688 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7689 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7690 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7691 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7692
7693 // Last finger up.
7694 processMTSync(mapper);
7695 processSync(mapper);
7696
7697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7698 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7699 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7700 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7701 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7702 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7703 ASSERT_EQ(0, motionArgs.flags);
7704 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7705 ASSERT_EQ(0, motionArgs.buttonState);
7706 ASSERT_EQ(0, motionArgs.edgeFlags);
7707 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7708 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7709 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7710 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7711 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7712 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7713 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7714 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7715
7716 // Should not have sent any more keys or motions.
7717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7719}
7720
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007721TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7722 addConfigurationProperty("touch.deviceType", "touchScreen");
7723 prepareDisplay(DISPLAY_ORIENTATION_0);
7724
7725 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7726 /*fuzz*/ 0, /*resolution*/ 10);
7727 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7728 /*fuzz*/ 0, /*resolution*/ 11);
7729 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7730 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7731 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7732 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7733 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7734 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7735 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7736 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7737
7738 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7739
7740 // X and Y axes
7741 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7742 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7743 // Touch major and minor
7744 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7745 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7746 // Tool major and minor
7747 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7748 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7749}
7750
7751TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7752 addConfigurationProperty("touch.deviceType", "touchScreen");
7753 prepareDisplay(DISPLAY_ORIENTATION_0);
7754
7755 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7756 /*fuzz*/ 0, /*resolution*/ 10);
7757 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7758 /*fuzz*/ 0, /*resolution*/ 11);
7759
7760 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7761
7762 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7763
7764 // Touch major and minor
7765 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7766 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7767 // Tool major and minor
7768 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7769 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7770}
7771
Michael Wrightd02c5b62014-02-10 15:10:22 -08007772TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007773 addConfigurationProperty("touch.deviceType", "touchScreen");
7774 prepareDisplay(DISPLAY_ORIENTATION_0);
7775 prepareAxes(POSITION | ID);
7776 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007777 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007778
arthurhungdcef2dc2020-08-11 14:47:50 +08007779 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007780
7781 NotifyMotionArgs motionArgs;
7782
7783 // Two fingers down at once.
7784 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7785 processPosition(mapper, x1, y1);
7786 processId(mapper, 1);
7787 processMTSync(mapper);
7788 processPosition(mapper, x2, y2);
7789 processId(mapper, 2);
7790 processMTSync(mapper);
7791 processSync(mapper);
7792
7793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7794 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7795 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7796 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7797 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7798 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7799 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7800
7801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007802 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007803 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7804 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7805 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7806 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7807 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7808 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7809 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7810 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7811 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7812
7813 // Move.
7814 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7815 processPosition(mapper, x1, y1);
7816 processId(mapper, 1);
7817 processMTSync(mapper);
7818 processPosition(mapper, x2, y2);
7819 processId(mapper, 2);
7820 processMTSync(mapper);
7821 processSync(mapper);
7822
7823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7824 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7825 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7826 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7827 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7828 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7829 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7830 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7831 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7832 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7833 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7834
7835 // First finger up.
7836 x2 += 15; y2 -= 20;
7837 processPosition(mapper, x2, y2);
7838 processId(mapper, 2);
7839 processMTSync(mapper);
7840 processSync(mapper);
7841
7842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007843 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007844 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7845 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7846 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7847 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7848 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7849 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7850 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7851 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7852 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7853
7854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7855 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7856 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7857 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7858 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7859 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7860 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7861
7862 // Move.
7863 x2 += 20; y2 -= 25;
7864 processPosition(mapper, x2, y2);
7865 processId(mapper, 2);
7866 processMTSync(mapper);
7867 processSync(mapper);
7868
7869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7870 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7871 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7872 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7873 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7874 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7875 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7876
7877 // New finger down.
7878 int32_t x3 = 700, y3 = 300;
7879 processPosition(mapper, x2, y2);
7880 processId(mapper, 2);
7881 processMTSync(mapper);
7882 processPosition(mapper, x3, y3);
7883 processId(mapper, 3);
7884 processMTSync(mapper);
7885 processSync(mapper);
7886
7887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007888 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007889 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7890 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7891 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7892 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7893 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7894 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7895 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7896 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7897 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7898
7899 // Second finger up.
7900 x3 += 30; y3 -= 20;
7901 processPosition(mapper, x3, y3);
7902 processId(mapper, 3);
7903 processMTSync(mapper);
7904 processSync(mapper);
7905
7906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007907 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007908 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7909 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7910 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7911 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7912 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7913 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7914 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7915 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7916 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7917
7918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7919 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7920 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7921 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7922 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7923 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7924 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7925
7926 // Last finger up.
7927 processMTSync(mapper);
7928 processSync(mapper);
7929
7930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7931 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7932 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7933 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7934 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7935 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7936 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7937
7938 // Should not have sent any more keys or motions.
7939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7941}
7942
7943TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007944 addConfigurationProperty("touch.deviceType", "touchScreen");
7945 prepareDisplay(DISPLAY_ORIENTATION_0);
7946 prepareAxes(POSITION | ID | SLOT);
7947 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007948 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007949
arthurhungdcef2dc2020-08-11 14:47:50 +08007950 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007951
7952 NotifyMotionArgs motionArgs;
7953
7954 // Two fingers down at once.
7955 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7956 processPosition(mapper, x1, y1);
7957 processId(mapper, 1);
7958 processSlot(mapper, 1);
7959 processPosition(mapper, x2, y2);
7960 processId(mapper, 2);
7961 processSync(mapper);
7962
7963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7964 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7965 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7966 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7967 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7968 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7969 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7970
7971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007972 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007973 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7974 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7975 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7976 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7977 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7978 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7979 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7980 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7981 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7982
7983 // Move.
7984 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7985 processSlot(mapper, 0);
7986 processPosition(mapper, x1, y1);
7987 processSlot(mapper, 1);
7988 processPosition(mapper, x2, y2);
7989 processSync(mapper);
7990
7991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7992 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7993 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7994 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7995 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7996 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7997 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7998 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7999 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8000 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8001 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8002
8003 // First finger up.
8004 x2 += 15; y2 -= 20;
8005 processSlot(mapper, 0);
8006 processId(mapper, -1);
8007 processSlot(mapper, 1);
8008 processPosition(mapper, x2, y2);
8009 processSync(mapper);
8010
8011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008012 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008013 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8014 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8015 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8016 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8017 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8018 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8019 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8020 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8021 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8022
8023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8024 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8025 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8026 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8027 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8028 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8029 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8030
8031 // Move.
8032 x2 += 20; y2 -= 25;
8033 processPosition(mapper, x2, y2);
8034 processSync(mapper);
8035
8036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8037 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8038 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8039 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8040 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8041 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8042 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8043
8044 // New finger down.
8045 int32_t x3 = 700, y3 = 300;
8046 processPosition(mapper, x2, y2);
8047 processSlot(mapper, 0);
8048 processId(mapper, 3);
8049 processPosition(mapper, x3, y3);
8050 processSync(mapper);
8051
8052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008053 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008054 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8055 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8056 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8057 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8058 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8059 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8060 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8061 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8062 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8063
8064 // Second finger up.
8065 x3 += 30; y3 -= 20;
8066 processSlot(mapper, 1);
8067 processId(mapper, -1);
8068 processSlot(mapper, 0);
8069 processPosition(mapper, x3, y3);
8070 processSync(mapper);
8071
8072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008073 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008074 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8075 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8076 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8077 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8078 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8079 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8080 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8081 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8082 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8083
8084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8085 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8086 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8087 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8088 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8089 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8090 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8091
8092 // Last finger up.
8093 processId(mapper, -1);
8094 processSync(mapper);
8095
8096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8097 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8098 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8099 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8100 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8101 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8102 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8103
8104 // Should not have sent any more keys or motions.
8105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8107}
8108
8109TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008110 addConfigurationProperty("touch.deviceType", "touchScreen");
8111 prepareDisplay(DISPLAY_ORIENTATION_0);
8112 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008113 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008114
8115 // These calculations are based on the input device calibration documentation.
8116 int32_t rawX = 100;
8117 int32_t rawY = 200;
8118 int32_t rawTouchMajor = 7;
8119 int32_t rawTouchMinor = 6;
8120 int32_t rawToolMajor = 9;
8121 int32_t rawToolMinor = 8;
8122 int32_t rawPressure = 11;
8123 int32_t rawDistance = 0;
8124 int32_t rawOrientation = 3;
8125 int32_t id = 5;
8126
8127 float x = toDisplayX(rawX);
8128 float y = toDisplayY(rawY);
8129 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8130 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8131 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8132 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8133 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8134 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8135 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8136 float distance = float(rawDistance);
8137
8138 processPosition(mapper, rawX, rawY);
8139 processTouchMajor(mapper, rawTouchMajor);
8140 processTouchMinor(mapper, rawTouchMinor);
8141 processToolMajor(mapper, rawToolMajor);
8142 processToolMinor(mapper, rawToolMinor);
8143 processPressure(mapper, rawPressure);
8144 processOrientation(mapper, rawOrientation);
8145 processDistance(mapper, rawDistance);
8146 processId(mapper, id);
8147 processMTSync(mapper);
8148 processSync(mapper);
8149
8150 NotifyMotionArgs args;
8151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8152 ASSERT_EQ(0, args.pointerProperties[0].id);
8153 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8154 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8155 orientation, distance));
8156}
8157
8158TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008159 addConfigurationProperty("touch.deviceType", "touchScreen");
8160 prepareDisplay(DISPLAY_ORIENTATION_0);
8161 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8162 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008163 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008164
8165 // These calculations are based on the input device calibration documentation.
8166 int32_t rawX = 100;
8167 int32_t rawY = 200;
8168 int32_t rawTouchMajor = 140;
8169 int32_t rawTouchMinor = 120;
8170 int32_t rawToolMajor = 180;
8171 int32_t rawToolMinor = 160;
8172
8173 float x = toDisplayX(rawX);
8174 float y = toDisplayY(rawY);
8175 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8176 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8177 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8178 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8179 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8180
8181 processPosition(mapper, rawX, rawY);
8182 processTouchMajor(mapper, rawTouchMajor);
8183 processTouchMinor(mapper, rawTouchMinor);
8184 processToolMajor(mapper, rawToolMajor);
8185 processToolMinor(mapper, rawToolMinor);
8186 processMTSync(mapper);
8187 processSync(mapper);
8188
8189 NotifyMotionArgs args;
8190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8191 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8192 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8193}
8194
8195TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008196 addConfigurationProperty("touch.deviceType", "touchScreen");
8197 prepareDisplay(DISPLAY_ORIENTATION_0);
8198 prepareAxes(POSITION | TOUCH | TOOL);
8199 addConfigurationProperty("touch.size.calibration", "diameter");
8200 addConfigurationProperty("touch.size.scale", "10");
8201 addConfigurationProperty("touch.size.bias", "160");
8202 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008203 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008204
8205 // These calculations are based on the input device calibration documentation.
8206 // Note: We only provide a single common touch/tool value because the device is assumed
8207 // not to emit separate values for each pointer (isSummed = 1).
8208 int32_t rawX = 100;
8209 int32_t rawY = 200;
8210 int32_t rawX2 = 150;
8211 int32_t rawY2 = 250;
8212 int32_t rawTouchMajor = 5;
8213 int32_t rawToolMajor = 8;
8214
8215 float x = toDisplayX(rawX);
8216 float y = toDisplayY(rawY);
8217 float x2 = toDisplayX(rawX2);
8218 float y2 = toDisplayY(rawY2);
8219 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8220 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8221 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8222
8223 processPosition(mapper, rawX, rawY);
8224 processTouchMajor(mapper, rawTouchMajor);
8225 processToolMajor(mapper, rawToolMajor);
8226 processMTSync(mapper);
8227 processPosition(mapper, rawX2, rawY2);
8228 processTouchMajor(mapper, rawTouchMajor);
8229 processToolMajor(mapper, rawToolMajor);
8230 processMTSync(mapper);
8231 processSync(mapper);
8232
8233 NotifyMotionArgs args;
8234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8235 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8236
8237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008238 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008239 ASSERT_EQ(size_t(2), args.pointerCount);
8240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8241 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8242 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8243 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8244}
8245
8246TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008247 addConfigurationProperty("touch.deviceType", "touchScreen");
8248 prepareDisplay(DISPLAY_ORIENTATION_0);
8249 prepareAxes(POSITION | TOUCH | TOOL);
8250 addConfigurationProperty("touch.size.calibration", "area");
8251 addConfigurationProperty("touch.size.scale", "43");
8252 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008253 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008254
8255 // These calculations are based on the input device calibration documentation.
8256 int32_t rawX = 100;
8257 int32_t rawY = 200;
8258 int32_t rawTouchMajor = 5;
8259 int32_t rawToolMajor = 8;
8260
8261 float x = toDisplayX(rawX);
8262 float y = toDisplayY(rawY);
8263 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8264 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8265 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8266
8267 processPosition(mapper, rawX, rawY);
8268 processTouchMajor(mapper, rawTouchMajor);
8269 processToolMajor(mapper, rawToolMajor);
8270 processMTSync(mapper);
8271 processSync(mapper);
8272
8273 NotifyMotionArgs args;
8274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8276 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8277}
8278
8279TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008280 addConfigurationProperty("touch.deviceType", "touchScreen");
8281 prepareDisplay(DISPLAY_ORIENTATION_0);
8282 prepareAxes(POSITION | PRESSURE);
8283 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8284 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008285 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008286
Michael Wrightaa449c92017-12-13 21:21:43 +00008287 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008288 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008289 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8290 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8291 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8292
Michael Wrightd02c5b62014-02-10 15:10:22 -08008293 // These calculations are based on the input device calibration documentation.
8294 int32_t rawX = 100;
8295 int32_t rawY = 200;
8296 int32_t rawPressure = 60;
8297
8298 float x = toDisplayX(rawX);
8299 float y = toDisplayY(rawY);
8300 float pressure = float(rawPressure) * 0.01f;
8301
8302 processPosition(mapper, rawX, rawY);
8303 processPressure(mapper, rawPressure);
8304 processMTSync(mapper);
8305 processSync(mapper);
8306
8307 NotifyMotionArgs args;
8308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8310 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8311}
8312
8313TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008314 addConfigurationProperty("touch.deviceType", "touchScreen");
8315 prepareDisplay(DISPLAY_ORIENTATION_0);
8316 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008317 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008318
8319 NotifyMotionArgs motionArgs;
8320 NotifyKeyArgs keyArgs;
8321
8322 processId(mapper, 1);
8323 processPosition(mapper, 100, 200);
8324 processSync(mapper);
8325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8326 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8327 ASSERT_EQ(0, motionArgs.buttonState);
8328
8329 // press BTN_LEFT, release BTN_LEFT
8330 processKey(mapper, BTN_LEFT, 1);
8331 processSync(mapper);
8332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8334 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8335
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8337 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8338 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8339
Michael Wrightd02c5b62014-02-10 15:10:22 -08008340 processKey(mapper, BTN_LEFT, 0);
8341 processSync(mapper);
8342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008343 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008344 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008345
8346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008347 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008348 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008349
8350 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8351 processKey(mapper, BTN_RIGHT, 1);
8352 processKey(mapper, BTN_MIDDLE, 1);
8353 processSync(mapper);
8354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8355 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8356 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8357 motionArgs.buttonState);
8358
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8360 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8361 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8362
8363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8364 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8365 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8366 motionArgs.buttonState);
8367
Michael Wrightd02c5b62014-02-10 15:10:22 -08008368 processKey(mapper, BTN_RIGHT, 0);
8369 processSync(mapper);
8370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008371 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008372 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008373
8374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008376 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008377
8378 processKey(mapper, BTN_MIDDLE, 0);
8379 processSync(mapper);
8380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008381 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008382 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008383
8384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008385 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008386 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008387
8388 // press BTN_BACK, release BTN_BACK
8389 processKey(mapper, BTN_BACK, 1);
8390 processSync(mapper);
8391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8392 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8393 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008394
Michael Wrightd02c5b62014-02-10 15:10:22 -08008395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008396 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008397 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8398
8399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8400 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8401 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008402
8403 processKey(mapper, BTN_BACK, 0);
8404 processSync(mapper);
8405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008406 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008407 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008408
8409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008411 ASSERT_EQ(0, motionArgs.buttonState);
8412
Michael Wrightd02c5b62014-02-10 15:10:22 -08008413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8414 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8415 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8416
8417 // press BTN_SIDE, release BTN_SIDE
8418 processKey(mapper, BTN_SIDE, 1);
8419 processSync(mapper);
8420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8421 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8422 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008423
Michael Wrightd02c5b62014-02-10 15:10:22 -08008424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008425 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008426 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8427
8428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8429 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8430 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008431
8432 processKey(mapper, BTN_SIDE, 0);
8433 processSync(mapper);
8434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008435 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008436 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008437
8438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008439 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008440 ASSERT_EQ(0, motionArgs.buttonState);
8441
Michael Wrightd02c5b62014-02-10 15:10:22 -08008442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8443 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8444 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8445
8446 // press BTN_FORWARD, release BTN_FORWARD
8447 processKey(mapper, BTN_FORWARD, 1);
8448 processSync(mapper);
8449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8450 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8451 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008452
Michael Wrightd02c5b62014-02-10 15:10:22 -08008453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008454 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008455 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8456
8457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8458 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8459 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008460
8461 processKey(mapper, BTN_FORWARD, 0);
8462 processSync(mapper);
8463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008464 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008465 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008466
8467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008468 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008469 ASSERT_EQ(0, motionArgs.buttonState);
8470
Michael Wrightd02c5b62014-02-10 15:10:22 -08008471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8472 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8473 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8474
8475 // press BTN_EXTRA, release BTN_EXTRA
8476 processKey(mapper, BTN_EXTRA, 1);
8477 processSync(mapper);
8478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8479 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8480 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008481
Michael Wrightd02c5b62014-02-10 15:10:22 -08008482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008483 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008484 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8485
8486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8487 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8488 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008489
8490 processKey(mapper, BTN_EXTRA, 0);
8491 processSync(mapper);
8492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008493 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008494 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008495
8496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008497 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008498 ASSERT_EQ(0, motionArgs.buttonState);
8499
Michael Wrightd02c5b62014-02-10 15:10:22 -08008500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8501 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8502 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8503
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8505
Michael Wrightd02c5b62014-02-10 15:10:22 -08008506 // press BTN_STYLUS, release BTN_STYLUS
8507 processKey(mapper, BTN_STYLUS, 1);
8508 processSync(mapper);
8509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8510 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008511 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8512
8513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8514 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8515 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008516
8517 processKey(mapper, BTN_STYLUS, 0);
8518 processSync(mapper);
8519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008520 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008521 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008522
8523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008524 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008525 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008526
8527 // press BTN_STYLUS2, release BTN_STYLUS2
8528 processKey(mapper, BTN_STYLUS2, 1);
8529 processSync(mapper);
8530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8531 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008532 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8533
8534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8535 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8536 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008537
8538 processKey(mapper, BTN_STYLUS2, 0);
8539 processSync(mapper);
8540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008541 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008542 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008543
8544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008545 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008546 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008547
8548 // release touch
8549 processId(mapper, -1);
8550 processSync(mapper);
8551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8552 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8553 ASSERT_EQ(0, motionArgs.buttonState);
8554}
8555
8556TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008557 addConfigurationProperty("touch.deviceType", "touchScreen");
8558 prepareDisplay(DISPLAY_ORIENTATION_0);
8559 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008560 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008561
8562 NotifyMotionArgs motionArgs;
8563
8564 // default tool type is finger
8565 processId(mapper, 1);
8566 processPosition(mapper, 100, 200);
8567 processSync(mapper);
8568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8569 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8570 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8571
8572 // eraser
8573 processKey(mapper, BTN_TOOL_RUBBER, 1);
8574 processSync(mapper);
8575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8576 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8577 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8578
8579 // stylus
8580 processKey(mapper, BTN_TOOL_RUBBER, 0);
8581 processKey(mapper, BTN_TOOL_PEN, 1);
8582 processSync(mapper);
8583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8584 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8585 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8586
8587 // brush
8588 processKey(mapper, BTN_TOOL_PEN, 0);
8589 processKey(mapper, BTN_TOOL_BRUSH, 1);
8590 processSync(mapper);
8591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8592 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8593 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8594
8595 // pencil
8596 processKey(mapper, BTN_TOOL_BRUSH, 0);
8597 processKey(mapper, BTN_TOOL_PENCIL, 1);
8598 processSync(mapper);
8599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8600 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8601 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8602
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008603 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008604 processKey(mapper, BTN_TOOL_PENCIL, 0);
8605 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8606 processSync(mapper);
8607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8608 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8609 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8610
8611 // mouse
8612 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8613 processKey(mapper, BTN_TOOL_MOUSE, 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_MOUSE, motionArgs.pointerProperties[0].toolType);
8618
8619 // lens
8620 processKey(mapper, BTN_TOOL_MOUSE, 0);
8621 processKey(mapper, BTN_TOOL_LENS, 1);
8622 processSync(mapper);
8623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8624 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8625 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8626
8627 // double-tap
8628 processKey(mapper, BTN_TOOL_LENS, 0);
8629 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8630 processSync(mapper);
8631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8632 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8633 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8634
8635 // triple-tap
8636 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8637 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8638 processSync(mapper);
8639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8640 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8641 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8642
8643 // quad-tap
8644 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8645 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8646 processSync(mapper);
8647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8648 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8649 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8650
8651 // finger
8652 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8653 processKey(mapper, BTN_TOOL_FINGER, 1);
8654 processSync(mapper);
8655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8656 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8657 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8658
8659 // stylus trumps finger
8660 processKey(mapper, BTN_TOOL_PEN, 1);
8661 processSync(mapper);
8662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8663 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8664 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8665
8666 // eraser trumps stylus
8667 processKey(mapper, BTN_TOOL_RUBBER, 1);
8668 processSync(mapper);
8669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8670 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8671 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8672
8673 // mouse trumps eraser
8674 processKey(mapper, BTN_TOOL_MOUSE, 1);
8675 processSync(mapper);
8676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8677 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8678 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8679
8680 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8681 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8682 processSync(mapper);
8683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8684 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8685 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8686
8687 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8688 processToolType(mapper, MT_TOOL_PEN);
8689 processSync(mapper);
8690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8691 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8692 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8693
8694 // back to default tool type
8695 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8696 processKey(mapper, BTN_TOOL_MOUSE, 0);
8697 processKey(mapper, BTN_TOOL_RUBBER, 0);
8698 processKey(mapper, BTN_TOOL_PEN, 0);
8699 processKey(mapper, BTN_TOOL_FINGER, 0);
8700 processSync(mapper);
8701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8702 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8703 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8704}
8705
8706TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008707 addConfigurationProperty("touch.deviceType", "touchScreen");
8708 prepareDisplay(DISPLAY_ORIENTATION_0);
8709 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008710 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008711 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008712
8713 NotifyMotionArgs motionArgs;
8714
8715 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8716 processId(mapper, 1);
8717 processPosition(mapper, 100, 200);
8718 processSync(mapper);
8719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8720 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8721 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8722 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8723
8724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8725 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8727 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8728
8729 // move a little
8730 processPosition(mapper, 150, 250);
8731 processSync(mapper);
8732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8733 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8734 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8735 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8736
8737 // down when BTN_TOUCH is pressed, pressure defaults to 1
8738 processKey(mapper, BTN_TOUCH, 1);
8739 processSync(mapper);
8740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8741 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8742 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8743 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8744
8745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8746 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8747 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8748 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8749
8750 // up when BTN_TOUCH is released, hover restored
8751 processKey(mapper, BTN_TOUCH, 0);
8752 processSync(mapper);
8753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8754 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8755 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8756 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8757
8758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8759 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8760 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8761 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8762
8763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8764 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8765 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8766 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8767
8768 // exit hover when pointer goes away
8769 processId(mapper, -1);
8770 processSync(mapper);
8771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8772 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8774 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8775}
8776
8777TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008778 addConfigurationProperty("touch.deviceType", "touchScreen");
8779 prepareDisplay(DISPLAY_ORIENTATION_0);
8780 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008781 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008782
8783 NotifyMotionArgs motionArgs;
8784
8785 // initially hovering because pressure is 0
8786 processId(mapper, 1);
8787 processPosition(mapper, 100, 200);
8788 processPressure(mapper, 0);
8789 processSync(mapper);
8790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8791 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8792 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8793 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8794
8795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8796 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8797 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8798 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8799
8800 // move a little
8801 processPosition(mapper, 150, 250);
8802 processSync(mapper);
8803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8804 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8806 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8807
8808 // down when pressure becomes non-zero
8809 processPressure(mapper, RAW_PRESSURE_MAX);
8810 processSync(mapper);
8811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8812 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8814 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8815
8816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8817 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8818 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8819 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8820
8821 // up when pressure becomes 0, hover restored
8822 processPressure(mapper, 0);
8823 processSync(mapper);
8824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8825 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8826 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8827 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8828
8829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8830 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8831 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8832 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8833
8834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8835 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8836 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8837 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8838
8839 // exit hover when pointer goes away
8840 processId(mapper, -1);
8841 processSync(mapper);
8842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8843 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8844 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8845 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8846}
8847
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008848/**
8849 * Set the input device port <--> display port associations, and check that the
8850 * events are routed to the display that matches the display port.
8851 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8852 */
8853TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008854 const std::string usb2 = "USB2";
8855 const uint8_t hdmi1 = 0;
8856 const uint8_t hdmi2 = 1;
8857 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008858 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008859
8860 addConfigurationProperty("touch.deviceType", "touchScreen");
8861 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008862 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008863
8864 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8865 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8866
8867 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8868 // for this input device is specified, and the matching viewport is not present,
8869 // the input device should be disabled (at the mapper level).
8870
8871 // Add viewport for display 2 on hdmi2
8872 prepareSecondaryDisplay(type, hdmi2);
8873 // Send a touch event
8874 processPosition(mapper, 100, 100);
8875 processSync(mapper);
8876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8877
8878 // Add viewport for display 1 on hdmi1
8879 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8880 // Send a touch event again
8881 processPosition(mapper, 100, 100);
8882 processSync(mapper);
8883
8884 NotifyMotionArgs args;
8885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8886 ASSERT_EQ(DISPLAY_ID, args.displayId);
8887}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008888
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008889TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8890 addConfigurationProperty("touch.deviceType", "touchScreen");
8891 prepareAxes(POSITION);
8892 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8893
8894 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8895
8896 prepareDisplay(DISPLAY_ORIENTATION_0);
8897 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8898
8899 // Send a touch event
8900 processPosition(mapper, 100, 100);
8901 processSync(mapper);
8902
8903 NotifyMotionArgs args;
8904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8905 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8906}
8907
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008908TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008909 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008910 std::shared_ptr<FakePointerController> fakePointerController =
8911 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008912 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008913 fakePointerController->setPosition(100, 200);
8914 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008915 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008916
Garfield Tan888a6a42020-01-09 11:39:16 -08008917 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008918 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008919
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008920 prepareDisplay(DISPLAY_ORIENTATION_0);
8921 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008922 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008923
8924 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008925 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008926
8927 NotifyMotionArgs motionArgs;
8928 processPosition(mapper, 100, 100);
8929 processSync(mapper);
8930
8931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8932 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8933 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8934}
8935
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008936/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008937 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8938 */
8939TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8940 addConfigurationProperty("touch.deviceType", "touchScreen");
8941 prepareAxes(POSITION);
8942 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8943
8944 prepareDisplay(DISPLAY_ORIENTATION_0);
8945 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8946 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8947 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8948 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8949
8950 NotifyMotionArgs args;
8951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8952 ASSERT_EQ(26, args.readTime);
8953
8954 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8955 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8956 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8957
8958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8959 ASSERT_EQ(33, args.readTime);
8960}
8961
8962/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008963 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8964 * events should not be delivered to the listener.
8965 */
8966TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8967 addConfigurationProperty("touch.deviceType", "touchScreen");
8968 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8969 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8970 ViewportType::INTERNAL);
8971 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8972 prepareAxes(POSITION);
8973 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8974
8975 NotifyMotionArgs motionArgs;
8976 processPosition(mapper, 100, 100);
8977 processSync(mapper);
8978
8979 mFakeListener->assertNotifyMotionWasNotCalled();
8980}
8981
Garfield Tanc734e4f2021-01-15 20:01:39 -08008982TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8983 addConfigurationProperty("touch.deviceType", "touchScreen");
8984 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8985 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8986 ViewportType::INTERNAL);
8987 std::optional<DisplayViewport> optionalDisplayViewport =
8988 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8989 ASSERT_TRUE(optionalDisplayViewport.has_value());
8990 DisplayViewport displayViewport = *optionalDisplayViewport;
8991
8992 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8993 prepareAxes(POSITION);
8994 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8995
8996 // Finger down
8997 int32_t x = 100, y = 100;
8998 processPosition(mapper, x, y);
8999 processSync(mapper);
9000
9001 NotifyMotionArgs motionArgs;
9002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9003 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9004
9005 // Deactivate display viewport
9006 displayViewport.isActive = false;
9007 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9008 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9009
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009010 // The ongoing touch should be canceled immediately
9011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9012 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9013
9014 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009015 x += 10, y += 10;
9016 processPosition(mapper, x, y);
9017 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009019
9020 // Reactivate display viewport
9021 displayViewport.isActive = true;
9022 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9023 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9024
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009025 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009026 x += 10, y += 10;
9027 processPosition(mapper, x, y);
9028 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9030 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009031}
9032
Arthur Hung7c645402019-01-25 17:45:42 +08009033TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9034 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009035 prepareAxes(POSITION | ID | SLOT);
9036 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009037 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009038
9039 // Create the second touch screen device, and enable multi fingers.
9040 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009041 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009042 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009043 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009044 std::shared_ptr<InputDevice> device2 =
9045 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009046 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009047
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009048 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9049 0 /*flat*/, 0 /*fuzz*/);
9050 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9051 0 /*flat*/, 0 /*fuzz*/);
9052 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
9053 0 /*flat*/, 0 /*fuzz*/);
9054 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
9055 0 /*flat*/, 0 /*fuzz*/);
9056 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
9057 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9058 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009059
9060 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009061 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009062 std::list<NotifyArgs> unused =
9063 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9064 0 /*changes*/);
9065 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009066
9067 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009068 std::shared_ptr<FakePointerController> fakePointerController =
9069 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009070 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009071
9072 // Setup policy for associated displays and show touches.
9073 const uint8_t hdmi1 = 0;
9074 const uint8_t hdmi2 = 1;
9075 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9076 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9077 mFakePolicy->setShowTouches(true);
9078
9079 // Create displays.
9080 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009081 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009082
9083 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009084 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9085 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
9086 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009087
9088 // Two fingers down at default display.
9089 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9090 processPosition(mapper, x1, y1);
9091 processId(mapper, 1);
9092 processSlot(mapper, 1);
9093 processPosition(mapper, x2, y2);
9094 processId(mapper, 2);
9095 processSync(mapper);
9096
9097 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9098 fakePointerController->getSpots().find(DISPLAY_ID);
9099 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9100 ASSERT_EQ(size_t(2), iter->second.size());
9101
9102 // Two fingers down at second display.
9103 processPosition(mapper2, x1, y1);
9104 processId(mapper2, 1);
9105 processSlot(mapper2, 1);
9106 processPosition(mapper2, x2, y2);
9107 processId(mapper2, 2);
9108 processSync(mapper2);
9109
9110 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9111 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9112 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009113
9114 // Disable the show touches configuration and ensure the spots are cleared.
9115 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009116 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9117 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009118
9119 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009120}
9121
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009122TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009123 prepareAxes(POSITION);
9124 addConfigurationProperty("touch.deviceType", "touchScreen");
9125 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009126 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009127
9128 NotifyMotionArgs motionArgs;
9129 // Unrotated video frame
9130 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9131 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009132 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009133 processPosition(mapper, 100, 200);
9134 processSync(mapper);
9135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9136 ASSERT_EQ(frames, motionArgs.videoFrames);
9137
9138 // Subsequent touch events should not have any videoframes
9139 // This is implemented separately in FakeEventHub,
9140 // but that should match the behaviour of TouchVideoDevice.
9141 processPosition(mapper, 200, 200);
9142 processSync(mapper);
9143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9144 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9145}
9146
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009147TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009148 prepareAxes(POSITION);
9149 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009150 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009151 // Unrotated video frame
9152 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9153 NotifyMotionArgs motionArgs;
9154
9155 // Test all 4 orientations
9156 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009157 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
9158 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9159 clearViewports();
9160 prepareDisplay(orientation);
9161 std::vector<TouchVideoFrame> frames{frame};
9162 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9163 processPosition(mapper, 100, 200);
9164 processSync(mapper);
9165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9166 ASSERT_EQ(frames, motionArgs.videoFrames);
9167 }
9168}
9169
9170TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9171 prepareAxes(POSITION);
9172 addConfigurationProperty("touch.deviceType", "touchScreen");
9173 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9174 // orientation-aware are affected by display rotation.
9175 addConfigurationProperty("touch.orientationAware", "0");
9176 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9177 // Unrotated video frame
9178 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9179 NotifyMotionArgs motionArgs;
9180
9181 // Test all 4 orientations
9182 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009183 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
9184 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9185 clearViewports();
9186 prepareDisplay(orientation);
9187 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009188 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009189 processPosition(mapper, 100, 200);
9190 processSync(mapper);
9191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009192 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9193 // compared to the display. This is so that when the window transform (which contains the
9194 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9195 // window's coordinate space.
9196 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009197 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009198
9199 // Release finger.
9200 processSync(mapper);
9201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009202 }
9203}
9204
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009205TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009206 prepareAxes(POSITION);
9207 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009208 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009209 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9210 // so mix these.
9211 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9212 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9213 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9214 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9215 NotifyMotionArgs motionArgs;
9216
9217 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009218 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009219 processPosition(mapper, 100, 200);
9220 processSync(mapper);
9221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009222 ASSERT_EQ(frames, motionArgs.videoFrames);
9223}
9224
9225TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9226 prepareAxes(POSITION);
9227 addConfigurationProperty("touch.deviceType", "touchScreen");
9228 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9229 // orientation-aware are affected by display rotation.
9230 addConfigurationProperty("touch.orientationAware", "0");
9231 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9232 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9233 // so mix these.
9234 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9235 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9236 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9237 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9238 NotifyMotionArgs motionArgs;
9239
9240 prepareDisplay(DISPLAY_ORIENTATION_90);
9241 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9242 processPosition(mapper, 100, 200);
9243 processSync(mapper);
9244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9245 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9246 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9247 // compared to the display. This is so that when the window transform (which contains the
9248 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9249 // window's coordinate space.
9250 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
9251 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009252 ASSERT_EQ(frames, motionArgs.videoFrames);
9253}
9254
Arthur Hung9da14732019-09-02 16:16:58 +08009255/**
9256 * If we had defined port associations, but the viewport is not ready, the touch device would be
9257 * expected to be disabled, and it should be enabled after the viewport has found.
9258 */
9259TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009260 constexpr uint8_t hdmi2 = 1;
9261 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009262 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009263
9264 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9265
9266 addConfigurationProperty("touch.deviceType", "touchScreen");
9267 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009268 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009269
9270 ASSERT_EQ(mDevice->isEnabled(), false);
9271
9272 // Add display on hdmi2, the device should be enabled and can receive touch event.
9273 prepareSecondaryDisplay(type, hdmi2);
9274 ASSERT_EQ(mDevice->isEnabled(), true);
9275
9276 // Send a touch event.
9277 processPosition(mapper, 100, 100);
9278 processSync(mapper);
9279
9280 NotifyMotionArgs args;
9281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9282 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9283}
9284
Arthur Hung421eb1c2020-01-16 00:09:42 +08009285TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009286 addConfigurationProperty("touch.deviceType", "touchScreen");
9287 prepareDisplay(DISPLAY_ORIENTATION_0);
9288 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009289 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009290
9291 NotifyMotionArgs motionArgs;
9292
9293 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9294 // finger down
9295 processId(mapper, 1);
9296 processPosition(mapper, x1, y1);
9297 processSync(mapper);
9298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9299 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9300 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9301
9302 // finger move
9303 processId(mapper, 1);
9304 processPosition(mapper, x2, y2);
9305 processSync(mapper);
9306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9307 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9308 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9309
9310 // finger up.
9311 processId(mapper, -1);
9312 processSync(mapper);
9313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9314 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9315 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9316
9317 // new finger down
9318 processId(mapper, 1);
9319 processPosition(mapper, x3, y3);
9320 processSync(mapper);
9321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9322 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9323 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9324}
9325
9326/**
arthurhungcc7f9802020-04-30 17:55:40 +08009327 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9328 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009329 */
arthurhungcc7f9802020-04-30 17:55:40 +08009330TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009331 addConfigurationProperty("touch.deviceType", "touchScreen");
9332 prepareDisplay(DISPLAY_ORIENTATION_0);
9333 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009334 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009335
9336 NotifyMotionArgs motionArgs;
9337
9338 // default tool type is finger
9339 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009340 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009341 processPosition(mapper, x1, y1);
9342 processSync(mapper);
9343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9344 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9345 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9346
9347 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9348 processToolType(mapper, MT_TOOL_PALM);
9349 processSync(mapper);
9350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9351 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9352
9353 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009354 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009355 processPosition(mapper, x2, y2);
9356 processSync(mapper);
9357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9358
9359 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009360 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009361 processSync(mapper);
9362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9363
9364 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009365 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009366 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009367 processPosition(mapper, x3, y3);
9368 processSync(mapper);
9369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9370 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9371 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9372}
9373
arthurhungbf89a482020-04-17 17:37:55 +08009374/**
arthurhungcc7f9802020-04-30 17:55:40 +08009375 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9376 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009377 */
arthurhungcc7f9802020-04-30 17:55:40 +08009378TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009379 addConfigurationProperty("touch.deviceType", "touchScreen");
9380 prepareDisplay(DISPLAY_ORIENTATION_0);
9381 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9382 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9383
9384 NotifyMotionArgs motionArgs;
9385
9386 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009387 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9388 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009389 processPosition(mapper, x1, y1);
9390 processSync(mapper);
9391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9392 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9393 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9394
9395 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009396 processSlot(mapper, SECOND_SLOT);
9397 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009398 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009399 processSync(mapper);
9400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009401 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009402 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9403
9404 // If the tool type of the first finger changes to MT_TOOL_PALM,
9405 // we expect to receive ACTION_POINTER_UP with cancel flag.
9406 processSlot(mapper, FIRST_SLOT);
9407 processId(mapper, FIRST_TRACKING_ID);
9408 processToolType(mapper, MT_TOOL_PALM);
9409 processSync(mapper);
9410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009411 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009412 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9413
9414 // The following MOVE events of second finger should be processed.
9415 processSlot(mapper, SECOND_SLOT);
9416 processId(mapper, SECOND_TRACKING_ID);
9417 processPosition(mapper, x2 + 1, y2 + 1);
9418 processSync(mapper);
9419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9420 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9421 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9422
9423 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9424 // it. Second finger receive move.
9425 processSlot(mapper, FIRST_SLOT);
9426 processId(mapper, INVALID_TRACKING_ID);
9427 processSync(mapper);
9428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9429 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9430 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9431
9432 // Second finger keeps moving.
9433 processSlot(mapper, SECOND_SLOT);
9434 processId(mapper, SECOND_TRACKING_ID);
9435 processPosition(mapper, x2 + 2, y2 + 2);
9436 processSync(mapper);
9437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9438 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9439 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9440
9441 // Second finger up.
9442 processId(mapper, INVALID_TRACKING_ID);
9443 processSync(mapper);
9444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9445 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9446 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9447}
9448
9449/**
9450 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9451 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9452 */
9453TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9454 addConfigurationProperty("touch.deviceType", "touchScreen");
9455 prepareDisplay(DISPLAY_ORIENTATION_0);
9456 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9457 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9458
9459 NotifyMotionArgs motionArgs;
9460
9461 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9462 // First finger down.
9463 processId(mapper, FIRST_TRACKING_ID);
9464 processPosition(mapper, x1, y1);
9465 processSync(mapper);
9466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9467 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9468 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9469
9470 // Second finger down.
9471 processSlot(mapper, SECOND_SLOT);
9472 processId(mapper, SECOND_TRACKING_ID);
9473 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009474 processSync(mapper);
9475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009476 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009477 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9478
arthurhungcc7f9802020-04-30 17:55:40 +08009479 // If the tool type of the first finger changes to MT_TOOL_PALM,
9480 // we expect to receive ACTION_POINTER_UP with cancel flag.
9481 processSlot(mapper, FIRST_SLOT);
9482 processId(mapper, FIRST_TRACKING_ID);
9483 processToolType(mapper, MT_TOOL_PALM);
9484 processSync(mapper);
9485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009486 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009487 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9488
9489 // Second finger keeps moving.
9490 processSlot(mapper, SECOND_SLOT);
9491 processId(mapper, SECOND_TRACKING_ID);
9492 processPosition(mapper, x2 + 1, y2 + 1);
9493 processSync(mapper);
9494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9495 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9496
9497 // second finger becomes palm, receive cancel due to only 1 finger is active.
9498 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009499 processToolType(mapper, MT_TOOL_PALM);
9500 processSync(mapper);
9501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9502 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9503
arthurhungcc7f9802020-04-30 17:55:40 +08009504 // third finger down.
9505 processSlot(mapper, THIRD_SLOT);
9506 processId(mapper, THIRD_TRACKING_ID);
9507 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009508 processPosition(mapper, x3, y3);
9509 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9511 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9512 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009513 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9514
9515 // third finger move
9516 processId(mapper, THIRD_TRACKING_ID);
9517 processPosition(mapper, x3 + 1, y3 + 1);
9518 processSync(mapper);
9519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9520 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9521
9522 // first finger up, third finger receive move.
9523 processSlot(mapper, FIRST_SLOT);
9524 processId(mapper, INVALID_TRACKING_ID);
9525 processSync(mapper);
9526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9527 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9528 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9529
9530 // second finger up, third finger receive move.
9531 processSlot(mapper, SECOND_SLOT);
9532 processId(mapper, INVALID_TRACKING_ID);
9533 processSync(mapper);
9534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9535 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9536 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9537
9538 // third finger up.
9539 processSlot(mapper, THIRD_SLOT);
9540 processId(mapper, INVALID_TRACKING_ID);
9541 processSync(mapper);
9542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9543 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9544 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9545}
9546
9547/**
9548 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9549 * and the active finger could still be allowed to receive the events
9550 */
9551TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9552 addConfigurationProperty("touch.deviceType", "touchScreen");
9553 prepareDisplay(DISPLAY_ORIENTATION_0);
9554 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9555 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9556
9557 NotifyMotionArgs motionArgs;
9558
9559 // default tool type is finger
9560 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9561 processId(mapper, FIRST_TRACKING_ID);
9562 processPosition(mapper, x1, y1);
9563 processSync(mapper);
9564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9565 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9566 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9567
9568 // Second finger down.
9569 processSlot(mapper, SECOND_SLOT);
9570 processId(mapper, SECOND_TRACKING_ID);
9571 processPosition(mapper, x2, y2);
9572 processSync(mapper);
9573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009574 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009575 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9576
9577 // If the tool type of the second finger changes to MT_TOOL_PALM,
9578 // we expect to receive ACTION_POINTER_UP with cancel flag.
9579 processId(mapper, SECOND_TRACKING_ID);
9580 processToolType(mapper, MT_TOOL_PALM);
9581 processSync(mapper);
9582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009583 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009584 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9585
9586 // The following MOVE event should be processed.
9587 processSlot(mapper, FIRST_SLOT);
9588 processId(mapper, FIRST_TRACKING_ID);
9589 processPosition(mapper, x1 + 1, y1 + 1);
9590 processSync(mapper);
9591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9592 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9593 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9594
9595 // second finger up.
9596 processSlot(mapper, SECOND_SLOT);
9597 processId(mapper, INVALID_TRACKING_ID);
9598 processSync(mapper);
9599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9600 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9601
9602 // first finger keep moving
9603 processSlot(mapper, FIRST_SLOT);
9604 processId(mapper, FIRST_TRACKING_ID);
9605 processPosition(mapper, x1 + 2, y1 + 2);
9606 processSync(mapper);
9607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9608 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9609
9610 // first finger up.
9611 processId(mapper, INVALID_TRACKING_ID);
9612 processSync(mapper);
9613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9614 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9615 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009616}
9617
Arthur Hung9ad18942021-06-19 02:04:46 +00009618/**
9619 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9620 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9621 * cause slot be valid again.
9622 */
9623TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9624 addConfigurationProperty("touch.deviceType", "touchScreen");
9625 prepareDisplay(DISPLAY_ORIENTATION_0);
9626 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9627 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9628
9629 NotifyMotionArgs motionArgs;
9630
9631 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9632 // First finger down.
9633 processId(mapper, FIRST_TRACKING_ID);
9634 processPosition(mapper, x1, y1);
9635 processPressure(mapper, RAW_PRESSURE_MAX);
9636 processSync(mapper);
9637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9638 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9639 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9640
9641 // First finger move.
9642 processId(mapper, FIRST_TRACKING_ID);
9643 processPosition(mapper, x1 + 1, y1 + 1);
9644 processPressure(mapper, RAW_PRESSURE_MAX);
9645 processSync(mapper);
9646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9647 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9648 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9649
9650 // Second finger down.
9651 processSlot(mapper, SECOND_SLOT);
9652 processId(mapper, SECOND_TRACKING_ID);
9653 processPosition(mapper, x2, y2);
9654 processPressure(mapper, RAW_PRESSURE_MAX);
9655 processSync(mapper);
9656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009657 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009658 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9659
9660 // second finger up with some unexpected data.
9661 processSlot(mapper, SECOND_SLOT);
9662 processId(mapper, INVALID_TRACKING_ID);
9663 processPosition(mapper, x2, y2);
9664 processSync(mapper);
9665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009666 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009667 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9668
9669 // first finger up with some unexpected data.
9670 processSlot(mapper, FIRST_SLOT);
9671 processId(mapper, INVALID_TRACKING_ID);
9672 processPosition(mapper, x2, y2);
9673 processPressure(mapper, RAW_PRESSURE_MAX);
9674 processSync(mapper);
9675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9676 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9677 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9678}
9679
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009680TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9681 addConfigurationProperty("touch.deviceType", "touchScreen");
9682 prepareDisplay(DISPLAY_ORIENTATION_0);
9683 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9684 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9685
9686 // First finger down.
9687 processId(mapper, FIRST_TRACKING_ID);
9688 processPosition(mapper, 100, 200);
9689 processPressure(mapper, RAW_PRESSURE_MAX);
9690 processSync(mapper);
9691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9692 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9693
9694 // Second finger down.
9695 processSlot(mapper, SECOND_SLOT);
9696 processId(mapper, SECOND_TRACKING_ID);
9697 processPosition(mapper, 300, 400);
9698 processPressure(mapper, RAW_PRESSURE_MAX);
9699 processSync(mapper);
9700 ASSERT_NO_FATAL_FAILURE(
9701 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9702
9703 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009704 // preserved. Resetting should cancel the ongoing gesture.
9705 resetMapper(mapper, ARBITRARY_TIME);
9706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9707 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009708
9709 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9710 // the existing touch state to generate a down event.
9711 processPosition(mapper, 301, 302);
9712 processSync(mapper);
9713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9714 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
9715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9716 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
9717
9718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9719}
9720
9721TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9722 addConfigurationProperty("touch.deviceType", "touchScreen");
9723 prepareDisplay(DISPLAY_ORIENTATION_0);
9724 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9725 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9726
9727 // First finger touches down and releases.
9728 processId(mapper, FIRST_TRACKING_ID);
9729 processPosition(mapper, 100, 200);
9730 processPressure(mapper, RAW_PRESSURE_MAX);
9731 processSync(mapper);
9732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9733 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9734 processId(mapper, INVALID_TRACKING_ID);
9735 processSync(mapper);
9736 ASSERT_NO_FATAL_FAILURE(
9737 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9738
9739 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9740 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009741 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9743
9744 // Send an empty sync frame. Since there are no pointers, no events are generated.
9745 processSync(mapper);
9746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9747}
9748
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009749// --- MultiTouchInputMapperTest_ExternalDevice ---
9750
9751class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9752protected:
Chris Yea52ade12020-08-27 16:49:20 -07009753 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009754};
9755
9756/**
9757 * Expect fallback to internal viewport if device is external and external viewport is not present.
9758 */
9759TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9760 prepareAxes(POSITION);
9761 addConfigurationProperty("touch.deviceType", "touchScreen");
9762 prepareDisplay(DISPLAY_ORIENTATION_0);
9763 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9764
9765 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9766
9767 NotifyMotionArgs motionArgs;
9768
9769 // Expect the event to be sent to the internal viewport,
9770 // because an external viewport is not present.
9771 processPosition(mapper, 100, 100);
9772 processSync(mapper);
9773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9774 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9775
9776 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009777 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009778 processPosition(mapper, 100, 100);
9779 processSync(mapper);
9780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9781 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9782}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009783
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009784TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9785 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9786 std::shared_ptr<FakePointerController> fakePointerController =
9787 std::make_shared<FakePointerController>();
9788 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9789 fakePointerController->setPosition(0, 0);
9790 fakePointerController->setButtonState(0);
9791
9792 // prepare device and capture
9793 prepareDisplay(DISPLAY_ORIENTATION_0);
9794 prepareAxes(POSITION | ID | SLOT);
9795 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9796 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9797 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009798 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009799 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9800
9801 // captured touchpad should be a touchpad source
9802 NotifyDeviceResetArgs resetArgs;
9803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9804 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9805
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009806 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009807
9808 const InputDeviceInfo::MotionRange* relRangeX =
9809 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9810 ASSERT_NE(relRangeX, nullptr);
9811 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9812 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9813 const InputDeviceInfo::MotionRange* relRangeY =
9814 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9815 ASSERT_NE(relRangeY, nullptr);
9816 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9817 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9818
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009819 // run captured pointer tests - note that this is unscaled, so input listener events should be
9820 // identical to what the hardware sends (accounting for any
9821 // calibration).
9822 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009823 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009824 processId(mapper, 1);
9825 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9826 processKey(mapper, BTN_TOUCH, 1);
9827 processSync(mapper);
9828
9829 // expect coord[0] to contain initial location of touch 0
9830 NotifyMotionArgs args;
9831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9832 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9833 ASSERT_EQ(1U, args.pointerCount);
9834 ASSERT_EQ(0, args.pointerProperties[0].id);
9835 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9836 ASSERT_NO_FATAL_FAILURE(
9837 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9838
9839 // FINGER 1 DOWN
9840 processSlot(mapper, 1);
9841 processId(mapper, 2);
9842 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9843 processSync(mapper);
9844
9845 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009847 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009848 ASSERT_EQ(2U, args.pointerCount);
9849 ASSERT_EQ(0, args.pointerProperties[0].id);
9850 ASSERT_EQ(1, args.pointerProperties[1].id);
9851 ASSERT_NO_FATAL_FAILURE(
9852 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9853 ASSERT_NO_FATAL_FAILURE(
9854 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9855
9856 // FINGER 1 MOVE
9857 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9858 processSync(mapper);
9859
9860 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9861 // from move
9862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9863 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9864 ASSERT_NO_FATAL_FAILURE(
9865 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9866 ASSERT_NO_FATAL_FAILURE(
9867 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9868
9869 // FINGER 0 MOVE
9870 processSlot(mapper, 0);
9871 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9872 processSync(mapper);
9873
9874 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9876 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9877 ASSERT_NO_FATAL_FAILURE(
9878 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9879 ASSERT_NO_FATAL_FAILURE(
9880 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9881
9882 // BUTTON DOWN
9883 processKey(mapper, BTN_LEFT, 1);
9884 processSync(mapper);
9885
9886 // touchinputmapper design sends a move before button press
9887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9888 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9890 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9891
9892 // BUTTON UP
9893 processKey(mapper, BTN_LEFT, 0);
9894 processSync(mapper);
9895
9896 // touchinputmapper design sends a move after button release
9897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9898 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9900 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9901
9902 // FINGER 0 UP
9903 processId(mapper, -1);
9904 processSync(mapper);
9905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9906 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9907
9908 // FINGER 1 MOVE
9909 processSlot(mapper, 1);
9910 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9911 processSync(mapper);
9912
9913 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9915 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9916 ASSERT_EQ(1U, args.pointerCount);
9917 ASSERT_EQ(1, args.pointerProperties[0].id);
9918 ASSERT_NO_FATAL_FAILURE(
9919 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9920
9921 // FINGER 1 UP
9922 processId(mapper, -1);
9923 processKey(mapper, BTN_TOUCH, 0);
9924 processSync(mapper);
9925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9926 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9927
9928 // non captured touchpad should be a mouse source
9929 mFakePolicy->setPointerCapture(false);
9930 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9932 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9933}
9934
9935TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9936 std::shared_ptr<FakePointerController> fakePointerController =
9937 std::make_shared<FakePointerController>();
9938 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9939 fakePointerController->setPosition(0, 0);
9940 fakePointerController->setButtonState(0);
9941
9942 // prepare device and capture
9943 prepareDisplay(DISPLAY_ORIENTATION_0);
9944 prepareAxes(POSITION | ID | SLOT);
9945 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9946 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009947 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009948 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9949 // run uncaptured pointer tests - pushes out generic events
9950 // FINGER 0 DOWN
9951 processId(mapper, 3);
9952 processPosition(mapper, 100, 100);
9953 processKey(mapper, BTN_TOUCH, 1);
9954 processSync(mapper);
9955
9956 // start at (100,100), cursor should be at (0,0) * scale
9957 NotifyMotionArgs args;
9958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9959 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9960 ASSERT_NO_FATAL_FAILURE(
9961 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9962
9963 // FINGER 0 MOVE
9964 processPosition(mapper, 200, 200);
9965 processSync(mapper);
9966
9967 // compute scaling to help with touch position checking
9968 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9969 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9970 float scale =
9971 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9972
9973 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9975 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9976 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9977 0, 0, 0, 0, 0, 0, 0));
9978}
9979
9980TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9981 std::shared_ptr<FakePointerController> fakePointerController =
9982 std::make_shared<FakePointerController>();
9983
9984 prepareDisplay(DISPLAY_ORIENTATION_0);
9985 prepareAxes(POSITION | ID | SLOT);
9986 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009987 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009988 mFakePolicy->setPointerCapture(false);
9989 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9990
9991 // uncaptured touchpad should be a pointer device
9992 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9993
9994 // captured touchpad should be a touchpad device
9995 mFakePolicy->setPointerCapture(true);
9996 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9997 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9998}
9999
HQ Liue6983c72022-04-19 22:14:56 +000010000class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10001protected:
10002 float mPointerMovementScale;
10003 float mPointerXZoomScale;
10004 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10005 addConfigurationProperty("touch.deviceType", "pointer");
10006 std::shared_ptr<FakePointerController> fakePointerController =
10007 std::make_shared<FakePointerController>();
10008 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10009 fakePointerController->setPosition(0, 0);
10010 fakePointerController->setButtonState(0);
10011 prepareDisplay(DISPLAY_ORIENTATION_0);
10012
10013 prepareAxes(POSITION);
10014 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10015 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10016 // needs to be disabled, and the pointer gesture needs to be enabled.
10017 mFakePolicy->setPointerCapture(false);
10018 mFakePolicy->setPointerGestureEnabled(true);
10019 mFakePolicy->setPointerController(fakePointerController);
10020
10021 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10022 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10023 mPointerMovementScale =
10024 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10025 mPointerXZoomScale =
10026 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10027 }
10028
10029 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10030 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10031 /*flat*/ 0,
10032 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10033 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10034 /*flat*/ 0,
10035 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10036 }
10037};
10038
10039/**
10040 * Two fingers down on a pointer mode touch pad. The width
10041 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10042 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10043 * be greater than the both value to be freeform gesture, so that after two
10044 * fingers start to move downwards, the gesture should be swipe.
10045 */
10046TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10047 // The min freeform gesture width is 25units/mm x 30mm = 750
10048 // which is greater than fraction of the diagnal length of the touchpad (349).
10049 // Thus, MaxSwipWidth is 750.
10050 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10051 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10052 NotifyMotionArgs motionArgs;
10053
10054 // Two fingers down at once.
10055 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10056 // Pointer's initial position is used the [0,0] coordinate.
10057 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10058
10059 processId(mapper, FIRST_TRACKING_ID);
10060 processPosition(mapper, x1, y1);
10061 processMTSync(mapper);
10062 processId(mapper, SECOND_TRACKING_ID);
10063 processPosition(mapper, x2, y2);
10064 processMTSync(mapper);
10065 processSync(mapper);
10066
10067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10068 ASSERT_EQ(1U, motionArgs.pointerCount);
10069 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10070 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010071 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010072 ASSERT_NO_FATAL_FAILURE(
10073 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10074
10075 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10076 // that there should be 1 pointer.
10077 int32_t movingDistance = 200;
10078 y1 += movingDistance;
10079 y2 += movingDistance;
10080
10081 processId(mapper, FIRST_TRACKING_ID);
10082 processPosition(mapper, x1, y1);
10083 processMTSync(mapper);
10084 processId(mapper, SECOND_TRACKING_ID);
10085 processPosition(mapper, x2, y2);
10086 processMTSync(mapper);
10087 processSync(mapper);
10088
10089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10090 ASSERT_EQ(1U, motionArgs.pointerCount);
10091 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10092 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010093 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010094 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10095 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10096 0, 0, 0, 0));
10097}
10098
10099/**
10100 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10101 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10102 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10103 * value to be freeform gesture, so that after two fingers start to move downwards,
10104 * the gesture should be swipe.
10105 */
10106TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10107 // The min freeform gesture width is 5units/mm x 30mm = 150
10108 // which is greater than fraction of the diagnal length of the touchpad (349).
10109 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
10110 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
10111 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10112 NotifyMotionArgs motionArgs;
10113
10114 // Two fingers down at once.
10115 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10116 // Pointer's initial position is used the [0,0] coordinate.
10117 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10118
10119 processId(mapper, FIRST_TRACKING_ID);
10120 processPosition(mapper, x1, y1);
10121 processMTSync(mapper);
10122 processId(mapper, SECOND_TRACKING_ID);
10123 processPosition(mapper, x2, y2);
10124 processMTSync(mapper);
10125 processSync(mapper);
10126
10127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10128 ASSERT_EQ(1U, motionArgs.pointerCount);
10129 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10130 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010131 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010132 ASSERT_NO_FATAL_FAILURE(
10133 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10134
10135 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10136 // and there should be 1 pointer.
10137 int32_t movingDistance = 200;
10138 y1 += movingDistance;
10139 y2 += movingDistance;
10140
10141 processId(mapper, FIRST_TRACKING_ID);
10142 processPosition(mapper, x1, y1);
10143 processMTSync(mapper);
10144 processId(mapper, SECOND_TRACKING_ID);
10145 processPosition(mapper, x2, y2);
10146 processMTSync(mapper);
10147 processSync(mapper);
10148
10149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10150 ASSERT_EQ(1U, motionArgs.pointerCount);
10151 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10152 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010153 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010154 // New coordinate is the scaled relative coordinate from the initial coordinate.
10155 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10156 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10157 0, 0, 0, 0));
10158}
10159
10160/**
10161 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10162 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10163 * freeform gestures after two fingers start to move downwards.
10164 */
10165TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
10166 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10167 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10168
10169 NotifyMotionArgs motionArgs;
10170
10171 // Two fingers down at once. Wider than the max swipe width.
10172 // The gesture is expected to be PRESS, then transformed to FREEFORM
10173 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10174
10175 processId(mapper, FIRST_TRACKING_ID);
10176 processPosition(mapper, x1, y1);
10177 processMTSync(mapper);
10178 processId(mapper, SECOND_TRACKING_ID);
10179 processPosition(mapper, x2, y2);
10180 processMTSync(mapper);
10181 processSync(mapper);
10182
10183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10184 ASSERT_EQ(1U, motionArgs.pointerCount);
10185 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10186 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010187 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010188 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10189 ASSERT_NO_FATAL_FAILURE(
10190 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10191
10192 int32_t movingDistance = 200;
10193
10194 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10195 // then two down events for two pointers.
10196 y1 += movingDistance;
10197 y2 += movingDistance;
10198
10199 processId(mapper, FIRST_TRACKING_ID);
10200 processPosition(mapper, x1, y1);
10201 processMTSync(mapper);
10202 processId(mapper, SECOND_TRACKING_ID);
10203 processPosition(mapper, x2, y2);
10204 processMTSync(mapper);
10205 processSync(mapper);
10206
10207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10208 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10209 ASSERT_EQ(1U, motionArgs.pointerCount);
10210 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10212 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10213 ASSERT_EQ(1U, motionArgs.pointerCount);
10214 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10216 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010217 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010218 ASSERT_EQ(2U, motionArgs.pointerCount);
10219 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
10220 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010221 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010222 // Two pointers' scaled relative coordinates from their initial centroid.
10223 // Initial y coordinates are 0 as y1 and y2 have the same value.
10224 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10225 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10226 // When pointers move, the new coordinates equal to the initial coordinates plus
10227 // scaled moving distance.
10228 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10229 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10230 0, 0, 0, 0));
10231 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10232 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10233 0, 0, 0, 0));
10234
10235 // Move two fingers down again, expect one MOVE motion event.
10236 y1 += movingDistance;
10237 y2 += movingDistance;
10238
10239 processId(mapper, FIRST_TRACKING_ID);
10240 processPosition(mapper, x1, y1);
10241 processMTSync(mapper);
10242 processId(mapper, SECOND_TRACKING_ID);
10243 processPosition(mapper, x2, y2);
10244 processMTSync(mapper);
10245 processSync(mapper);
10246
10247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10248 ASSERT_EQ(2U, motionArgs.pointerCount);
10249 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10250 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010251 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010252 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10253 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10254 0, 0, 0, 0, 0));
10255 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10256 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10257 0, 0, 0, 0, 0));
10258}
10259
Harry Cutts39b7ca22022-10-05 15:55:48 +000010260TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
10261 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10262 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10263 NotifyMotionArgs motionArgs;
10264
10265 // Place two fingers down.
10266 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10267
10268 processId(mapper, FIRST_TRACKING_ID);
10269 processPosition(mapper, x1, y1);
10270 processMTSync(mapper);
10271 processId(mapper, SECOND_TRACKING_ID);
10272 processPosition(mapper, x2, y2);
10273 processMTSync(mapper);
10274 processSync(mapper);
10275
10276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10277 ASSERT_EQ(1U, motionArgs.pointerCount);
10278 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10279 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10280 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10281 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10282
10283 // Move the two fingers down and to the left.
10284 int32_t movingDistance = 200;
10285 x1 -= movingDistance;
10286 y1 += movingDistance;
10287 x2 -= movingDistance;
10288 y2 += movingDistance;
10289
10290 processId(mapper, FIRST_TRACKING_ID);
10291 processPosition(mapper, x1, y1);
10292 processMTSync(mapper);
10293 processId(mapper, SECOND_TRACKING_ID);
10294 processPosition(mapper, x2, y2);
10295 processMTSync(mapper);
10296 processSync(mapper);
10297
10298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10299 ASSERT_EQ(1U, motionArgs.pointerCount);
10300 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10301 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10302 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10303 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10304}
10305
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010306// --- JoystickInputMapperTest ---
10307
10308class JoystickInputMapperTest : public InputMapperTest {
10309protected:
10310 static const int32_t RAW_X_MIN;
10311 static const int32_t RAW_X_MAX;
10312 static const int32_t RAW_Y_MIN;
10313 static const int32_t RAW_Y_MAX;
10314
10315 void SetUp() override {
10316 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10317 }
10318 void prepareAxes() {
10319 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10320 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10321 }
10322
10323 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10324 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10325 }
10326
10327 void processSync(JoystickInputMapper& mapper) {
10328 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10329 }
10330
10331 void prepareVirtualDisplay(int32_t orientation) {
10332 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10333 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10334 NO_PORT, ViewportType::VIRTUAL);
10335 }
10336};
10337
10338const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10339const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10340const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10341const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10342
10343TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10344 prepareAxes();
10345 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10346
10347 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10348
10349 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
10350
10351 // Send an axis event
10352 processAxis(mapper, ABS_X, 100);
10353 processSync(mapper);
10354
10355 NotifyMotionArgs args;
10356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10357 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10358
10359 // Send another axis event
10360 processAxis(mapper, ABS_Y, 100);
10361 processSync(mapper);
10362
10363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10364 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10365}
10366
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010367// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010368
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010369class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010370protected:
10371 static const char* DEVICE_NAME;
10372 static const char* DEVICE_LOCATION;
10373 static const int32_t DEVICE_ID;
10374 static const int32_t DEVICE_GENERATION;
10375 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010376 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010377 static const int32_t EVENTHUB_ID;
10378
10379 std::shared_ptr<FakeEventHub> mFakeEventHub;
10380 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010381 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010382 std::unique_ptr<InstrumentedInputReader> mReader;
10383 std::shared_ptr<InputDevice> mDevice;
10384
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010385 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010386 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010387 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010388 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010389 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010390 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010391 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10392 }
10393
10394 void SetUp() override { SetUp(DEVICE_CLASSES); }
10395
10396 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010397 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010398 mFakePolicy.clear();
10399 }
10400
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010401 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010402 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10403 mReader->requestRefreshConfiguration(changes);
10404 mReader->loopOnce();
10405 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010406 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010407 }
10408
10409 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10410 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010411 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010412 InputDeviceIdentifier identifier;
10413 identifier.name = name;
10414 identifier.location = location;
10415 std::shared_ptr<InputDevice> device =
10416 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10417 identifier);
10418 mReader->pushNextDevice(device);
10419 mFakeEventHub->addDevice(eventHubId, name, classes);
10420 mReader->loopOnce();
10421 return device;
10422 }
10423
10424 template <class T, typename... Args>
10425 T& addControllerAndConfigure(Args... args) {
10426 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10427
10428 return controller;
10429 }
10430};
10431
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010432const char* PeripheralControllerTest::DEVICE_NAME = "device";
10433const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10434const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10435const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10436const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010437const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10438 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010439const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010440
10441// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010442class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010443protected:
10444 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010445 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010446 }
10447};
10448
10449TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010450 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010451
10452 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
10453 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
10454}
10455
10456TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010457 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010458
10459 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
10460 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
10461}
10462
10463// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010464class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010465protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010466 void SetUp() override {
10467 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10468 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010469};
10470
Chris Ye85758332021-05-16 23:05:17 -070010471TEST_F(LightControllerTest, MonoLight) {
10472 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010473 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010474 .maxBrightness = 255,
10475 .flags = InputLightClass::BRIGHTNESS,
10476 .path = ""};
10477 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010478
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010479 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010480 InputDeviceInfo info;
10481 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010482 std::vector<InputDeviceLightInfo> lights = info.getLights();
10483 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010484 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10485 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10486
10487 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10488 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10489}
10490
10491TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10492 RawLightInfo infoMono = {.id = 1,
10493 .name = "mono_keyboard_backlight",
10494 .maxBrightness = 255,
10495 .flags = InputLightClass::BRIGHTNESS |
10496 InputLightClass::KEYBOARD_BACKLIGHT,
10497 .path = ""};
10498 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10499
10500 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10501 InputDeviceInfo info;
10502 controller.populateDeviceInfo(&info);
10503 std::vector<InputDeviceLightInfo> lights = info.getLights();
10504 ASSERT_EQ(1U, lights.size());
10505 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10506 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010507
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010508 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10509 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010510}
10511
10512TEST_F(LightControllerTest, RGBLight) {
10513 RawLightInfo infoRed = {.id = 1,
10514 .name = "red",
10515 .maxBrightness = 255,
10516 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10517 .path = ""};
10518 RawLightInfo infoGreen = {.id = 2,
10519 .name = "green",
10520 .maxBrightness = 255,
10521 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10522 .path = ""};
10523 RawLightInfo infoBlue = {.id = 3,
10524 .name = "blue",
10525 .maxBrightness = 255,
10526 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10527 .path = ""};
10528 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10529 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10530 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10531
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010532 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010533 InputDeviceInfo info;
10534 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010535 std::vector<InputDeviceLightInfo> lights = info.getLights();
10536 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010537 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10538 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10539 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10540
10541 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10542 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10543}
10544
10545TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10546 RawLightInfo infoRed = {.id = 1,
10547 .name = "red_keyboard_backlight",
10548 .maxBrightness = 255,
10549 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10550 InputLightClass::KEYBOARD_BACKLIGHT,
10551 .path = ""};
10552 RawLightInfo infoGreen = {.id = 2,
10553 .name = "green_keyboard_backlight",
10554 .maxBrightness = 255,
10555 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10556 InputLightClass::KEYBOARD_BACKLIGHT,
10557 .path = ""};
10558 RawLightInfo infoBlue = {.id = 3,
10559 .name = "blue_keyboard_backlight",
10560 .maxBrightness = 255,
10561 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10562 InputLightClass::KEYBOARD_BACKLIGHT,
10563 .path = ""};
10564 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10565 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10566 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10567
10568 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10569 InputDeviceInfo info;
10570 controller.populateDeviceInfo(&info);
10571 std::vector<InputDeviceLightInfo> lights = info.getLights();
10572 ASSERT_EQ(1U, lights.size());
10573 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10574 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10575 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10576
10577 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10578 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10579}
10580
10581TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10582 RawLightInfo infoRed = {.id = 1,
10583 .name = "red",
10584 .maxBrightness = 255,
10585 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10586 .path = ""};
10587 RawLightInfo infoGreen = {.id = 2,
10588 .name = "green",
10589 .maxBrightness = 255,
10590 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10591 .path = ""};
10592 RawLightInfo infoBlue = {.id = 3,
10593 .name = "blue",
10594 .maxBrightness = 255,
10595 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10596 .path = ""};
10597 RawLightInfo infoGlobal = {.id = 3,
10598 .name = "global_keyboard_backlight",
10599 .maxBrightness = 255,
10600 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10601 InputLightClass::KEYBOARD_BACKLIGHT,
10602 .path = ""};
10603 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10604 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10605 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10606 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10607
10608 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10609 InputDeviceInfo info;
10610 controller.populateDeviceInfo(&info);
10611 std::vector<InputDeviceLightInfo> lights = info.getLights();
10612 ASSERT_EQ(1U, lights.size());
10613 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10614 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10615 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010616
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010617 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10618 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010619}
10620
10621TEST_F(LightControllerTest, MultiColorRGBLight) {
10622 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010623 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010624 .maxBrightness = 255,
10625 .flags = InputLightClass::BRIGHTNESS |
10626 InputLightClass::MULTI_INTENSITY |
10627 InputLightClass::MULTI_INDEX,
10628 .path = ""};
10629
10630 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10631
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010632 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010633 InputDeviceInfo info;
10634 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010635 std::vector<InputDeviceLightInfo> lights = info.getLights();
10636 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010637 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10638 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10639 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10640
10641 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10642 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10643}
10644
10645TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10646 RawLightInfo infoColor = {.id = 1,
10647 .name = "multi_color_keyboard_backlight",
10648 .maxBrightness = 255,
10649 .flags = InputLightClass::BRIGHTNESS |
10650 InputLightClass::MULTI_INTENSITY |
10651 InputLightClass::MULTI_INDEX |
10652 InputLightClass::KEYBOARD_BACKLIGHT,
10653 .path = ""};
10654
10655 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10656
10657 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10658 InputDeviceInfo info;
10659 controller.populateDeviceInfo(&info);
10660 std::vector<InputDeviceLightInfo> lights = info.getLights();
10661 ASSERT_EQ(1U, lights.size());
10662 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10663 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10664 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010665
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010666 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10667 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010668}
10669
10670TEST_F(LightControllerTest, PlayerIdLight) {
10671 RawLightInfo info1 = {.id = 1,
10672 .name = "player1",
10673 .maxBrightness = 255,
10674 .flags = InputLightClass::BRIGHTNESS,
10675 .path = ""};
10676 RawLightInfo info2 = {.id = 2,
10677 .name = "player2",
10678 .maxBrightness = 255,
10679 .flags = InputLightClass::BRIGHTNESS,
10680 .path = ""};
10681 RawLightInfo info3 = {.id = 3,
10682 .name = "player3",
10683 .maxBrightness = 255,
10684 .flags = InputLightClass::BRIGHTNESS,
10685 .path = ""};
10686 RawLightInfo info4 = {.id = 4,
10687 .name = "player4",
10688 .maxBrightness = 255,
10689 .flags = InputLightClass::BRIGHTNESS,
10690 .path = ""};
10691 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10692 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10693 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10694 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10695
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010696 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010697 InputDeviceInfo info;
10698 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010699 std::vector<InputDeviceLightInfo> lights = info.getLights();
10700 ASSERT_EQ(1U, lights.size());
10701 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010702 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10703 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010704
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010705 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10706 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10707 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010708}
10709
Michael Wrightd02c5b62014-02-10 15:10:22 -080010710} // namespace android