blob: 3cc8e9403005789144516ebc7f9a2a0970502dd8 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
19
Prabir Pradhan2770d242019-09-02 18:07:11 -070020#include <CursorInputMapper.h>
21#include <InputDevice.h>
22#include <InputMapper.h>
23#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080024#include <InputReaderBase.h>
25#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000026#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070027#include <KeyboardInputMapper.h>
28#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070029#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070030#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070031#include <SingleTouchInputMapper.h>
32#include <SwitchInputMapper.h>
33#include <TestInputListener.h>
Prabir Pradhan739dca42022-09-09 20:12:01 +000034#include <TestInputListenerMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070035#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080036#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000037#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070038#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080039#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050040#include <gui/constants.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080041
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000042#include "android/hardware/input/InputDeviceCountryCode.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000043#include "input/DisplayViewport.h"
44#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010045
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000046using android::hardware::input::InputDeviceCountryCode;
47
Michael Wrightd02c5b62014-02-10 15:10:22 -080048namespace android {
49
Dominik Laskowski2f01d772022-03-23 16:01:29 -070050using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000051using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070052using std::chrono_literals::operator""ms;
53
54// Timeout for waiting for an expected event
55static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
56
Michael Wrightd02c5b62014-02-10 15:10:22 -080057// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000058static constexpr nsecs_t ARBITRARY_TIME = 1234;
59static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080060
61// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080062static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000063static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080064static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000065static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080066static constexpr int32_t DISPLAY_WIDTH = 480;
67static constexpr int32_t DISPLAY_HEIGHT = 800;
68static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
69static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
70static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070071static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070072static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080073
arthurhungcc7f9802020-04-30 17:55:40 +080074static constexpr int32_t FIRST_SLOT = 0;
75static constexpr int32_t SECOND_SLOT = 1;
76static constexpr int32_t THIRD_SLOT = 2;
77static constexpr int32_t INVALID_TRACKING_ID = -1;
78static constexpr int32_t FIRST_TRACKING_ID = 0;
79static constexpr int32_t SECOND_TRACKING_ID = 1;
80static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080081static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080082static constexpr int32_t BATTERY_STATUS = 4;
83static constexpr int32_t BATTERY_CAPACITY = 66;
Prabir Pradhane287ecd2022-09-07 21:18:05 +000084static const std::string BATTERY_DEVPATH = "/sys/devices/mydevice/power_supply/mybattery";
Chris Ye3fdbfef2021-01-06 18:45:18 -080085static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
86static constexpr int32_t LIGHT_COLOR = 0x7F448866;
87static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080088
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080089static constexpr int32_t ACTION_POINTER_0_DOWN =
90 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
91static constexpr int32_t ACTION_POINTER_0_UP =
92 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
93static constexpr int32_t ACTION_POINTER_1_DOWN =
94 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
95static constexpr int32_t ACTION_POINTER_1_UP =
96 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
97
Michael Wrightd02c5b62014-02-10 15:10:22 -080098// Error tolerance for floating point assertions.
99static const float EPSILON = 0.001f;
100
101template<typename T>
102static inline T min(T a, T b) {
103 return a < b ? a : b;
104}
105
106static inline float avg(float x, float y) {
107 return (x + y) / 2;
108}
109
Chris Ye3fdbfef2021-01-06 18:45:18 -0800110// Mapping for light color name and the light color
111const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
112 {"green", LightColor::GREEN},
113 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800114
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700115static int32_t getInverseRotation(int32_t orientation) {
116 switch (orientation) {
117 case DISPLAY_ORIENTATION_90:
118 return DISPLAY_ORIENTATION_270;
119 case DISPLAY_ORIENTATION_270:
120 return DISPLAY_ORIENTATION_90;
121 default:
122 return orientation;
123 }
124}
125
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800126static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
127 InputDeviceInfo info;
128 mapper.populateDeviceInfo(&info);
129
130 const InputDeviceInfo::MotionRange* motionRange =
131 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
132 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
133}
134
135static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
136 InputDeviceInfo info;
137 mapper.populateDeviceInfo(&info);
138
139 const InputDeviceInfo::MotionRange* motionRange =
140 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
141 ASSERT_EQ(nullptr, motionRange);
142}
143
Michael Wrightd02c5b62014-02-10 15:10:22 -0800144// --- FakePointerController ---
145
146class FakePointerController : public PointerControllerInterface {
147 bool mHaveBounds;
148 float mMinX, mMinY, mMaxX, mMaxY;
149 float mX, mY;
150 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800151 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152
Michael Wrightd02c5b62014-02-10 15:10:22 -0800153public:
154 FakePointerController() :
155 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800156 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800157 }
158
Michael Wright17db18e2020-06-26 20:51:44 +0100159 virtual ~FakePointerController() {}
160
Michael Wrightd02c5b62014-02-10 15:10:22 -0800161 void setBounds(float minX, float minY, float maxX, float maxY) {
162 mHaveBounds = true;
163 mMinX = minX;
164 mMinY = minY;
165 mMaxX = maxX;
166 mMaxY = maxY;
167 }
168
Chris Yea52ade12020-08-27 16:49:20 -0700169 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170 mX = x;
171 mY = y;
172 }
173
Chris Yea52ade12020-08-27 16:49:20 -0700174 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800175
Chris Yea52ade12020-08-27 16:49:20 -0700176 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800177
Chris Yea52ade12020-08-27 16:49:20 -0700178 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800179 *outX = mX;
180 *outY = mY;
181 }
182
Chris Yea52ade12020-08-27 16:49:20 -0700183 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800184
Chris Yea52ade12020-08-27 16:49:20 -0700185 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800186 mDisplayId = viewport.displayId;
187 }
188
Arthur Hung7c645402019-01-25 17:45:42 +0800189 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
190 return mSpotsByDisplay;
191 }
192
Michael Wrightd02c5b62014-02-10 15:10:22 -0800193private:
Chris Yea52ade12020-08-27 16:49:20 -0700194 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800195 *outMinX = mMinX;
196 *outMinY = mMinY;
197 *outMaxX = mMaxX;
198 *outMaxY = mMaxY;
199 return mHaveBounds;
200 }
201
Chris Yea52ade12020-08-27 16:49:20 -0700202 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800203 mX += deltaX;
204 if (mX < mMinX) mX = mMinX;
205 if (mX > mMaxX) mX = mMaxX;
206 mY += deltaY;
207 if (mY < mMinY) mY = mMinY;
208 if (mY > mMaxY) mY = mMaxY;
209 }
210
Chris Yea52ade12020-08-27 16:49:20 -0700211 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800212
Chris Yea52ade12020-08-27 16:49:20 -0700213 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800214
Chris Yea52ade12020-08-27 16:49:20 -0700215 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800216
Chris Yea52ade12020-08-27 16:49:20 -0700217 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
218 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800219 std::vector<int32_t> newSpots;
220 // Add spots for fingers that are down.
221 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
222 uint32_t id = idBits.clearFirstMarkedBit();
223 newSpots.push_back(id);
224 }
225
226 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800227 }
228
Prabir Pradhan197e0862022-07-01 14:28:00 +0000229 void clearSpots() override { mSpotsByDisplay.clear(); }
Arthur Hung7c645402019-01-25 17:45:42 +0800230
231 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800232};
233
234
235// --- FakeInputReaderPolicy ---
236
237class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700238 std::mutex mLock;
239 std::condition_variable mDevicesChangedCondition;
240
Michael Wrightd02c5b62014-02-10 15:10:22 -0800241 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000242 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700243 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
244 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100245 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700246 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800247
248protected:
Chris Yea52ade12020-08-27 16:49:20 -0700249 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800250
251public:
252 FakeInputReaderPolicy() {
253 }
254
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700255 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800256 waitForInputDevices([](bool devicesChanged) {
257 if (!devicesChanged) {
258 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
259 }
260 });
261 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700262
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800263 void assertInputDevicesNotChanged() {
264 waitForInputDevices([](bool devicesChanged) {
265 if (devicesChanged) {
266 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
267 }
268 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700269 }
270
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700271 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100272 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100273 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700274 }
275
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700276 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
277 return mConfig.getDisplayViewportByUniqueId(uniqueId);
278 }
279 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
280 return mConfig.getDisplayViewportByType(type);
281 }
282
283 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
284 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700285 }
286
Prabir Pradhan5632d622021-09-06 07:57:20 -0700287 void addDisplayViewport(DisplayViewport viewport) {
288 mViewports.push_back(std::move(viewport));
289 mConfig.setDisplayViewports(mViewports);
290 }
291
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700292 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000293 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700294 std::optional<uint8_t> physicalPort, ViewportType type) {
295 const bool isRotated =
296 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
297 DisplayViewport v;
298 v.displayId = displayId;
299 v.orientation = orientation;
300 v.logicalLeft = 0;
301 v.logicalTop = 0;
302 v.logicalRight = isRotated ? height : width;
303 v.logicalBottom = isRotated ? width : height;
304 v.physicalLeft = 0;
305 v.physicalTop = 0;
306 v.physicalRight = isRotated ? height : width;
307 v.physicalBottom = isRotated ? width : height;
308 v.deviceWidth = isRotated ? height : width;
309 v.deviceHeight = isRotated ? width : height;
310 v.isActive = isActive;
311 v.uniqueId = uniqueId;
312 v.physicalPort = physicalPort;
313 v.type = type;
314
315 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800316 }
317
Arthur Hung6cd19a42019-08-30 19:04:12 +0800318 bool updateViewport(const DisplayViewport& viewport) {
319 size_t count = mViewports.size();
320 for (size_t i = 0; i < count; i++) {
321 const DisplayViewport& currentViewport = mViewports[i];
322 if (currentViewport.displayId == viewport.displayId) {
323 mViewports[i] = viewport;
324 mConfig.setDisplayViewports(mViewports);
325 return true;
326 }
327 }
328 // no viewport found.
329 return false;
330 }
331
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100332 void addExcludedDeviceName(const std::string& deviceName) {
333 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800334 }
335
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700336 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
337 mConfig.portAssociations.insert({inputPort, displayPort});
338 }
339
Christine Franks1ba71cc2021-04-07 14:37:42 -0700340 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
341 const std::string& displayUniqueId) {
342 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
343 }
344
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000345 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700346
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000347 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700348
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000349 void setPointerController(std::shared_ptr<FakePointerController> controller) {
350 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351 }
352
353 const InputReaderConfiguration* getReaderConfiguration() const {
354 return &mConfig;
355 }
356
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800357 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800358 return mInputDevices;
359 }
360
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100361 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700362 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700363 return transform;
364 }
365
366 void setTouchAffineTransformation(const TouchAffineTransformation t) {
367 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800368 }
369
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000370 PointerCaptureRequest setPointerCapture(bool enabled) {
371 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
372 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800373 }
374
Arthur Hung7c645402019-01-25 17:45:42 +0800375 void setShowTouches(bool enabled) {
376 mConfig.showTouches = enabled;
377 }
378
Garfield Tan888a6a42020-01-09 11:39:16 -0800379 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
380 mConfig.defaultPointerDisplayId = pointerDisplayId;
381 }
382
HQ Liue6983c72022-04-19 22:14:56 +0000383 void setPointerGestureEnabled(bool enabled) { mConfig.pointerGesturesEnabled = enabled; }
384
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800385 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
386
HQ Liue6983c72022-04-19 22:14:56 +0000387 float getPointerGestureZoomSpeedRatio() { return mConfig.pointerGestureZoomSpeedRatio; }
388
Prabir Pradhanf99d6e72022-04-21 15:28:35 +0000389 void setVelocityControlParams(const VelocityControlParameters& params) {
390 mConfig.pointerVelocityControlParameters = params;
391 mConfig.wheelVelocityControlParameters = params;
392 }
393
Michael Wrightd02c5b62014-02-10 15:10:22 -0800394private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000395 uint32_t mNextPointerCaptureSequenceNumber = 0;
396
Chris Yea52ade12020-08-27 16:49:20 -0700397 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800398 *outConfig = mConfig;
399 }
400
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000401 std::shared_ptr<PointerControllerInterface> obtainPointerController(
402 int32_t /*deviceId*/) override {
403 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800404 }
405
Chris Yea52ade12020-08-27 16:49:20 -0700406 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700407 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800408 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700409 mInputDevicesChanged = true;
410 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800411 }
412
Chris Yea52ade12020-08-27 16:49:20 -0700413 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
414 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700415 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800416 }
417
Chris Yea52ade12020-08-27 16:49:20 -0700418 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800419
420 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
421 std::unique_lock<std::mutex> lock(mLock);
422 base::ScopedLockAssertion assumeLocked(mLock);
423
424 const bool devicesChanged =
425 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
426 return mInputDevicesChanged;
427 });
428 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
429 mInputDevicesChanged = false;
430 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800431};
432
Michael Wrightd02c5b62014-02-10 15:10:22 -0800433// --- FakeEventHub ---
434
435class FakeEventHub : public EventHubInterface {
436 struct KeyInfo {
437 int32_t keyCode;
438 uint32_t flags;
439 };
440
Chris Yef59a2f42020-10-16 12:55:26 -0700441 struct SensorInfo {
442 InputDeviceSensorType sensorType;
443 int32_t sensorDataIndex;
444 };
445
Michael Wrightd02c5b62014-02-10 15:10:22 -0800446 struct Device {
447 InputDeviceIdentifier identifier;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700448 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800449 PropertyMap configuration;
450 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
451 KeyedVector<int, bool> relativeAxes;
452 KeyedVector<int32_t, int32_t> keyCodeStates;
453 KeyedVector<int32_t, int32_t> scanCodeStates;
454 KeyedVector<int32_t, int32_t> switchStates;
455 KeyedVector<int32_t, int32_t> absoluteAxisValue;
456 KeyedVector<int32_t, KeyInfo> keysByScanCode;
457 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
458 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100459 // fake mapping which would normally come from keyCharacterMap
460 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700461 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
462 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800463 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700464 bool enabled;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000465 InputDeviceCountryCode countryCode;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700466
467 status_t enable() {
468 enabled = true;
469 return OK;
470 }
471
472 status_t disable() {
473 enabled = false;
474 return OK;
475 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800476
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700477 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800478 };
479
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700480 std::mutex mLock;
481 std::condition_variable mEventsCondition;
482
Michael Wrightd02c5b62014-02-10 15:10:22 -0800483 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100484 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000485 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600486 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000487 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800488 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
489 // Simulates a device light brightness, from light id to light brightness.
490 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
491 // Simulates a device light intensities, from light id to light intensities map.
492 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
493 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800494
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700495public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800496 virtual ~FakeEventHub() {
497 for (size_t i = 0; i < mDevices.size(); i++) {
498 delete mDevices.valueAt(i);
499 }
500 }
501
Michael Wrightd02c5b62014-02-10 15:10:22 -0800502 FakeEventHub() { }
503
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700504 void addDevice(int32_t deviceId, const std::string& name,
505 ftl::Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800506 Device* device = new Device(classes);
507 device->identifier.name = name;
508 mDevices.add(deviceId, device);
509
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000510 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800511 }
512
513 void removeDevice(int32_t deviceId) {
514 delete mDevices.valueFor(deviceId);
515 mDevices.removeItem(deviceId);
516
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000517 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800518 }
519
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000520 bool isDeviceEnabled(int32_t deviceId) const override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700521 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700522 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700523 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
524 return false;
525 }
526 return device->enabled;
527 }
528
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000529 status_t enableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700530 status_t result;
531 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700532 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700533 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
534 return BAD_VALUE;
535 }
536 if (device->enabled) {
537 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
538 return OK;
539 }
540 result = device->enable();
541 return result;
542 }
543
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000544 status_t disableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700545 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700546 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700547 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
548 return BAD_VALUE;
549 }
550 if (!device->enabled) {
551 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
552 return OK;
553 }
554 return device->disable();
555 }
556
Michael Wrightd02c5b62014-02-10 15:10:22 -0800557 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000558 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800559 }
560
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700561 void addConfigurationProperty(int32_t deviceId, const char* key, const char* value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800562 Device* device = getDevice(deviceId);
563 device->configuration.addProperty(key, value);
564 }
565
566 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
567 Device* device = getDevice(deviceId);
568 device->configuration.addAll(configuration);
569 }
570
571 void addAbsoluteAxis(int32_t deviceId, int axis,
572 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
573 Device* device = getDevice(deviceId);
574
575 RawAbsoluteAxisInfo info;
576 info.valid = true;
577 info.minValue = minValue;
578 info.maxValue = maxValue;
579 info.flat = flat;
580 info.fuzz = fuzz;
581 info.resolution = resolution;
582 device->absoluteAxes.add(axis, info);
583 }
584
585 void addRelativeAxis(int32_t deviceId, int32_t axis) {
586 Device* device = getDevice(deviceId);
587 device->relativeAxes.add(axis, true);
588 }
589
590 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
591 Device* device = getDevice(deviceId);
592 device->keyCodeStates.replaceValueFor(keyCode, state);
593 }
594
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000595 void setCountryCode(int32_t deviceId, InputDeviceCountryCode countryCode) {
596 Device* device = getDevice(deviceId);
597 device->countryCode = countryCode;
598 }
599
Michael Wrightd02c5b62014-02-10 15:10:22 -0800600 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
601 Device* device = getDevice(deviceId);
602 device->scanCodeStates.replaceValueFor(scanCode, state);
603 }
604
605 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
606 Device* device = getDevice(deviceId);
607 device->switchStates.replaceValueFor(switchCode, state);
608 }
609
610 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
611 Device* device = getDevice(deviceId);
612 device->absoluteAxisValue.replaceValueFor(axis, value);
613 }
614
615 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
616 int32_t keyCode, uint32_t flags) {
617 Device* device = getDevice(deviceId);
618 KeyInfo info;
619 info.keyCode = keyCode;
620 info.flags = flags;
621 if (scanCode) {
622 device->keysByScanCode.add(scanCode, info);
623 }
624 if (usageCode) {
625 device->keysByUsageCode.add(usageCode, info);
626 }
627 }
628
Philip Junker4af3b3d2021-12-14 10:36:55 +0100629 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
630 Device* device = getDevice(deviceId);
631 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
632 }
633
Michael Wrightd02c5b62014-02-10 15:10:22 -0800634 void addLed(int32_t deviceId, int32_t led, bool initialState) {
635 Device* device = getDevice(deviceId);
636 device->leds.add(led, initialState);
637 }
638
Chris Yef59a2f42020-10-16 12:55:26 -0700639 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
640 int32_t sensorDataIndex) {
641 Device* device = getDevice(deviceId);
642 SensorInfo info;
643 info.sensorType = sensorType;
644 info.sensorDataIndex = sensorDataIndex;
645 device->sensorsByAbsCode.emplace(absCode, info);
646 }
647
648 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
649 Device* device = getDevice(deviceId);
650 typename BitArray<MSC_MAX>::Buffer buffer;
651 buffer[mscEvent / 32] = 1 << mscEvent % 32;
652 device->mscBitmask.loadFromBuffer(buffer);
653 }
654
Chris Ye3fdbfef2021-01-06 18:45:18 -0800655 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
656 mRawLightInfos.emplace(rawId, std::move(info));
657 }
658
659 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
660 mLightBrightness.emplace(rawId, brightness);
661 }
662
663 void fakeLightIntensities(int32_t rawId,
664 const std::unordered_map<LightColor, int32_t> intensities) {
665 mLightIntensities.emplace(rawId, std::move(intensities));
666 }
667
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668 bool getLedState(int32_t deviceId, int32_t led) {
669 Device* device = getDevice(deviceId);
670 return device->leds.valueFor(led);
671 }
672
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100673 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800674 return mExcludedDevices;
675 }
676
677 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
678 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800679 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800680 }
681
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000682 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
683 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700684 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800685 RawEvent event;
686 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000687 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800688 event.deviceId = deviceId;
689 event.type = type;
690 event.code = code;
691 event.value = value;
692 mEvents.push_back(event);
693
694 if (type == EV_ABS) {
695 setAbsoluteAxisValue(deviceId, code, value);
696 }
697 }
698
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600699 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
700 std::vector<TouchVideoFrame>> videoFrames) {
701 mVideoFrames = std::move(videoFrames);
702 }
703
Michael Wrightd02c5b62014-02-10 15:10:22 -0800704 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700705 std::unique_lock<std::mutex> lock(mLock);
706 base::ScopedLockAssertion assumeLocked(mLock);
707 const bool queueIsEmpty =
708 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
709 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
710 if (!queueIsEmpty) {
711 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
712 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800713 }
714
715private:
716 Device* getDevice(int32_t deviceId) const {
717 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100718 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800719 }
720
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700721 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800722 Device* device = getDevice(deviceId);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700723 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800724 }
725
Chris Yea52ade12020-08-27 16:49:20 -0700726 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800727 Device* device = getDevice(deviceId);
728 return device ? device->identifier : InputDeviceIdentifier();
729 }
730
Chris Yea52ade12020-08-27 16:49:20 -0700731 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800732
Chris Yea52ade12020-08-27 16:49:20 -0700733 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800734 Device* device = getDevice(deviceId);
735 if (device) {
736 *outConfiguration = device->configuration;
737 }
738 }
739
Chris Yea52ade12020-08-27 16:49:20 -0700740 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
741 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800742 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800743 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800744 ssize_t index = device->absoluteAxes.indexOfKey(axis);
745 if (index >= 0) {
746 *outAxisInfo = device->absoluteAxes.valueAt(index);
747 return OK;
748 }
749 }
750 outAxisInfo->clear();
751 return -1;
752 }
753
Chris Yea52ade12020-08-27 16:49:20 -0700754 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800755 Device* device = getDevice(deviceId);
756 if (device) {
757 return device->relativeAxes.indexOfKey(axis) >= 0;
758 }
759 return false;
760 }
761
Chris Yea52ade12020-08-27 16:49:20 -0700762 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800763
Chris Yef59a2f42020-10-16 12:55:26 -0700764 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
765 Device* device = getDevice(deviceId);
766 if (device) {
767 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
768 }
769 return false;
770 }
771
Chris Yea52ade12020-08-27 16:49:20 -0700772 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
773 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800774 Device* device = getDevice(deviceId);
775 if (device) {
776 const KeyInfo* key = getKey(device, scanCode, usageCode);
777 if (key) {
778 if (outKeycode) {
779 *outKeycode = key->keyCode;
780 }
781 if (outFlags) {
782 *outFlags = key->flags;
783 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700784 if (outMetaState) {
785 *outMetaState = metaState;
786 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800787 return OK;
788 }
789 }
790 return NAME_NOT_FOUND;
791 }
792
793 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
794 if (usageCode) {
795 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
796 if (index >= 0) {
797 return &device->keysByUsageCode.valueAt(index);
798 }
799 }
800 if (scanCode) {
801 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
802 if (index >= 0) {
803 return &device->keysByScanCode.valueAt(index);
804 }
805 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700806 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800807 }
808
Chris Yea52ade12020-08-27 16:49:20 -0700809 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000811 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
812 int32_t deviceId, int32_t absCode) const override {
Chris Yef59a2f42020-10-16 12:55:26 -0700813 Device* device = getDevice(deviceId);
814 if (!device) {
815 return Errorf("Sensor device not found.");
816 }
817 auto it = device->sensorsByAbsCode.find(absCode);
818 if (it == device->sensorsByAbsCode.end()) {
819 return Errorf("Sensor map not found.");
820 }
821 const SensorInfo& info = it->second;
822 return std::make_pair(info.sensorType, info.sensorDataIndex);
823 }
824
Chris Yea52ade12020-08-27 16:49:20 -0700825 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800826 mExcludedDevices = devices;
827 }
828
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700829 std::vector<RawEvent> getEvents(int) override {
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000830 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800831
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700832 std::vector<RawEvent> buffer;
833 std::swap(buffer, mEvents);
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000834
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700835 mEventsCondition.notify_all();
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700836 return buffer;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800837 }
838
Chris Yea52ade12020-08-27 16:49:20 -0700839 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600840 auto it = mVideoFrames.find(deviceId);
841 if (it != mVideoFrames.end()) {
842 std::vector<TouchVideoFrame> frames = std::move(it->second);
843 mVideoFrames.erase(deviceId);
844 return frames;
845 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800846 return {};
847 }
848
Chris Yea52ade12020-08-27 16:49:20 -0700849 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800850 Device* device = getDevice(deviceId);
851 if (device) {
852 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
853 if (index >= 0) {
854 return device->scanCodeStates.valueAt(index);
855 }
856 }
857 return AKEY_STATE_UNKNOWN;
858 }
859
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000860 InputDeviceCountryCode getCountryCode(int32_t deviceId) const override {
861 Device* device = getDevice(deviceId);
862 if (device) {
863 return device->countryCode;
864 }
865 return InputDeviceCountryCode::INVALID;
866 }
867
Chris Yea52ade12020-08-27 16:49:20 -0700868 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800869 Device* device = getDevice(deviceId);
870 if (device) {
871 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
872 if (index >= 0) {
873 return device->keyCodeStates.valueAt(index);
874 }
875 }
876 return AKEY_STATE_UNKNOWN;
877 }
878
Chris Yea52ade12020-08-27 16:49:20 -0700879 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800880 Device* device = getDevice(deviceId);
881 if (device) {
882 ssize_t index = device->switchStates.indexOfKey(sw);
883 if (index >= 0) {
884 return device->switchStates.valueAt(index);
885 }
886 }
887 return AKEY_STATE_UNKNOWN;
888 }
889
Chris Yea52ade12020-08-27 16:49:20 -0700890 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
891 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800892 Device* device = getDevice(deviceId);
893 if (device) {
894 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
895 if (index >= 0) {
896 *outValue = device->absoluteAxisValue.valueAt(index);
897 return OK;
898 }
899 }
900 *outValue = 0;
901 return -1;
902 }
903
Philip Junker4af3b3d2021-12-14 10:36:55 +0100904 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
905 Device* device = getDevice(deviceId);
906 if (!device) {
907 return AKEYCODE_UNKNOWN;
908 }
909 auto it = device->keyCodeMapping.find(locationKeyCode);
910 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
911 }
912
Chris Yea52ade12020-08-27 16:49:20 -0700913 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700914 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700915 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800916 bool result = false;
917 Device* device = getDevice(deviceId);
918 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700919 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700920 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800921 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
922 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
923 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800924 }
925 }
926 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
927 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
928 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929 }
930 }
931 }
932 }
933 return result;
934 }
935
Chris Yea52ade12020-08-27 16:49:20 -0700936 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800937 Device* device = getDevice(deviceId);
938 if (device) {
939 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
940 return index >= 0;
941 }
942 return false;
943 }
944
Arthur Hungcb40a002021-08-03 14:31:01 +0000945 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
946 Device* device = getDevice(deviceId);
947 if (!device) {
948 return false;
949 }
950 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
951 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
952 return true;
953 }
954 }
955 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
956 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
957 return true;
958 }
959 }
960 return false;
961 }
962
Chris Yea52ade12020-08-27 16:49:20 -0700963 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800964 Device* device = getDevice(deviceId);
965 return device && device->leds.indexOfKey(led) >= 0;
966 }
967
Chris Yea52ade12020-08-27 16:49:20 -0700968 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800969 Device* device = getDevice(deviceId);
970 if (device) {
971 ssize_t index = device->leds.indexOfKey(led);
972 if (index >= 0) {
973 device->leds.replaceValueAt(led, on);
974 } else {
975 ADD_FAILURE()
976 << "Attempted to set the state of an LED that the EventHub declared "
977 "was not present. led=" << led;
978 }
979 }
980 }
981
Chris Yea52ade12020-08-27 16:49:20 -0700982 void getVirtualKeyDefinitions(
983 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800984 outVirtualKeys.clear();
985
986 Device* device = getDevice(deviceId);
987 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800988 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800989 }
990 }
991
Chris Yea52ade12020-08-27 16:49:20 -0700992 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700993 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800994 }
995
Chris Yea52ade12020-08-27 16:49:20 -0700996 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800997 return false;
998 }
999
Chris Yea52ade12020-08-27 16:49:20 -07001000 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001001
Chris Yea52ade12020-08-27 16:49:20 -07001002 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001003
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001004 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override { return mVibrators; };
Chris Ye87143712020-11-10 05:05:58 +00001005
Chris Yee2b1e5c2021-03-10 22:45:12 -08001006 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
1007 return BATTERY_CAPACITY;
1008 }
Kim Low03ea0352020-11-06 12:45:07 -08001009
Chris Yee2b1e5c2021-03-10 22:45:12 -08001010 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
1011 return BATTERY_STATUS;
1012 }
1013
Andy Chenf9f1a022022-08-29 20:07:10 -04001014 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override {
1015 return {DEFAULT_BATTERY};
1016 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001017
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001018 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
1019 int32_t batteryId) const override {
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001020 if (batteryId != DEFAULT_BATTERY) return {};
1021 static const auto BATTERY_INFO = RawBatteryInfo{.id = DEFAULT_BATTERY,
1022 .name = "default battery",
1023 .flags = InputBatteryClass::CAPACITY,
1024 .path = BATTERY_DEVPATH};
1025 return BATTERY_INFO;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001026 }
Kim Low03ea0352020-11-06 12:45:07 -08001027
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001028 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001029 std::vector<int32_t> ids;
1030 for (const auto& [rawId, info] : mRawLightInfos) {
1031 ids.push_back(rawId);
1032 }
1033 return ids;
1034 }
1035
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001036 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001037 auto it = mRawLightInfos.find(lightId);
1038 if (it == mRawLightInfos.end()) {
1039 return std::nullopt;
1040 }
1041 return it->second;
1042 }
1043
1044 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1045 mLightBrightness.emplace(lightId, brightness);
1046 }
1047
1048 void setLightIntensities(int32_t deviceId, int32_t lightId,
1049 std::unordered_map<LightColor, int32_t> intensities) override {
1050 mLightIntensities.emplace(lightId, intensities);
1051 };
1052
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001053 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001054 auto lightIt = mLightBrightness.find(lightId);
1055 if (lightIt == mLightBrightness.end()) {
1056 return std::nullopt;
1057 }
1058 return lightIt->second;
1059 }
1060
1061 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001062 int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001063 auto lightIt = mLightIntensities.find(lightId);
1064 if (lightIt == mLightIntensities.end()) {
1065 return std::nullopt;
1066 }
1067 return lightIt->second;
1068 };
1069
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001070 void dump(std::string&) const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001071
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001072 void monitor() const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001073
Chris Yea52ade12020-08-27 16:49:20 -07001074 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001075
Chris Yea52ade12020-08-27 16:49:20 -07001076 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001077};
1078
Michael Wrightd02c5b62014-02-10 15:10:22 -08001079// --- FakeInputMapper ---
1080
1081class FakeInputMapper : public InputMapper {
1082 uint32_t mSources;
1083 int32_t mKeyboardType;
1084 int32_t mMetaState;
1085 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1086 KeyedVector<int32_t, int32_t> mScanCodeStates;
1087 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001088 // fake mapping which would normally come from keyCharacterMap
1089 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001090 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001091
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001092 std::mutex mLock;
1093 std::condition_variable mStateChangedCondition;
1094 bool mConfigureWasCalled GUARDED_BY(mLock);
1095 bool mResetWasCalled GUARDED_BY(mLock);
1096 bool mProcessWasCalled GUARDED_BY(mLock);
1097 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001098
Arthur Hungc23540e2018-11-29 20:42:11 +08001099 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001100public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001101 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1102 : InputMapper(deviceContext),
1103 mSources(sources),
1104 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001105 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001106 mConfigureWasCalled(false),
1107 mResetWasCalled(false),
1108 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001109
Chris Yea52ade12020-08-27 16:49:20 -07001110 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001111
1112 void setKeyboardType(int32_t keyboardType) {
1113 mKeyboardType = keyboardType;
1114 }
1115
1116 void setMetaState(int32_t metaState) {
1117 mMetaState = metaState;
1118 }
1119
1120 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001121 std::unique_lock<std::mutex> lock(mLock);
1122 base::ScopedLockAssertion assumeLocked(mLock);
1123 const bool configureCalled =
1124 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1125 return mConfigureWasCalled;
1126 });
1127 if (!configureCalled) {
1128 FAIL() << "Expected configure() to have been called.";
1129 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001130 mConfigureWasCalled = false;
1131 }
1132
1133 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001134 std::unique_lock<std::mutex> lock(mLock);
1135 base::ScopedLockAssertion assumeLocked(mLock);
1136 const bool resetCalled =
1137 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1138 return mResetWasCalled;
1139 });
1140 if (!resetCalled) {
1141 FAIL() << "Expected reset() to have been called.";
1142 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001143 mResetWasCalled = false;
1144 }
1145
Yi Kong9b14ac62018-07-17 13:48:38 -07001146 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001147 std::unique_lock<std::mutex> lock(mLock);
1148 base::ScopedLockAssertion assumeLocked(mLock);
1149 const bool processCalled =
1150 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1151 return mProcessWasCalled;
1152 });
1153 if (!processCalled) {
1154 FAIL() << "Expected process() to have been called.";
1155 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001156 if (outLastEvent) {
1157 *outLastEvent = mLastEvent;
1158 }
1159 mProcessWasCalled = false;
1160 }
1161
1162 void setKeyCodeState(int32_t keyCode, int32_t state) {
1163 mKeyCodeStates.replaceValueFor(keyCode, state);
1164 }
1165
1166 void setScanCodeState(int32_t scanCode, int32_t state) {
1167 mScanCodeStates.replaceValueFor(scanCode, state);
1168 }
1169
1170 void setSwitchState(int32_t switchCode, int32_t state) {
1171 mSwitchStates.replaceValueFor(switchCode, state);
1172 }
1173
1174 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001175 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001176 }
1177
Philip Junker4af3b3d2021-12-14 10:36:55 +01001178 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1179 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1180 }
1181
Michael Wrightd02c5b62014-02-10 15:10:22 -08001182private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001183 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001184
Chris Yea52ade12020-08-27 16:49:20 -07001185 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001186 InputMapper::populateDeviceInfo(deviceInfo);
1187
1188 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1189 deviceInfo->setKeyboardType(mKeyboardType);
1190 }
1191 }
1192
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001193 std::list<NotifyArgs> configure(nsecs_t, const InputReaderConfiguration* config,
1194 uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001195 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001196 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001197
1198 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001199 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001200 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1201 mViewport = config->getDisplayViewportByPort(*displayPort);
1202 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001203
1204 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001205 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001206 }
1207
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001208 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001209 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001210 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001211 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001212 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001213 }
1214
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001215 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001216 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001217 mLastEvent = *rawEvent;
1218 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001219 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001220 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001221 }
1222
Chris Yea52ade12020-08-27 16:49:20 -07001223 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001224 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1225 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1226 }
1227
Philip Junker4af3b3d2021-12-14 10:36:55 +01001228 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1229 auto it = mKeyCodeMapping.find(locationKeyCode);
1230 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1231 }
1232
Chris Yea52ade12020-08-27 16:49:20 -07001233 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001234 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1235 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1236 }
1237
Chris Yea52ade12020-08-27 16:49:20 -07001238 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001239 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1240 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1241 }
1242
Chris Yea52ade12020-08-27 16:49:20 -07001243 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001244 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -07001245 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001246 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001247 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1248 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1249 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001250 }
1251 }
1252 }
Chris Yea52ade12020-08-27 16:49:20 -07001253 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001254 return result;
1255 }
1256
1257 virtual int32_t getMetaState() {
1258 return mMetaState;
1259 }
1260
1261 virtual void fadePointer() {
1262 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001263
1264 virtual std::optional<int32_t> getAssociatedDisplay() {
1265 if (mViewport) {
1266 return std::make_optional(mViewport->displayId);
1267 }
1268 return std::nullopt;
1269 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001270};
1271
1272
1273// --- InstrumentedInputReader ---
1274
1275class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001276 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001277
1278public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001279 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1280 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001281 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001282 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001283
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001284 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001285
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001286 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001287
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001288 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001289 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001290 InputDeviceIdentifier identifier;
1291 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001292 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001293 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001294 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001295 }
1296
Prabir Pradhan28efc192019-11-05 01:10:04 +00001297 // Make the protected loopOnce method accessible to tests.
1298 using InputReader::loopOnce;
1299
Michael Wrightd02c5b62014-02-10 15:10:22 -08001300protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001301 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1302 const InputDeviceIdentifier& identifier)
1303 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001304 if (!mNextDevices.empty()) {
1305 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1306 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001307 return device;
1308 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001309 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001310 }
1311
arthurhungdcef2dc2020-08-11 14:47:50 +08001312 // --- FakeInputReaderContext ---
1313 class FakeInputReaderContext : public ContextImpl {
1314 int32_t mGlobalMetaState;
1315 bool mUpdateGlobalMetaStateWasCalled;
1316 int32_t mGeneration;
1317
1318 public:
1319 FakeInputReaderContext(InputReader* reader)
1320 : ContextImpl(reader),
1321 mGlobalMetaState(0),
1322 mUpdateGlobalMetaStateWasCalled(false),
1323 mGeneration(1) {}
1324
1325 virtual ~FakeInputReaderContext() {}
1326
1327 void assertUpdateGlobalMetaStateWasCalled() {
1328 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1329 << "Expected updateGlobalMetaState() to have been called.";
1330 mUpdateGlobalMetaStateWasCalled = false;
1331 }
1332
1333 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1334
1335 uint32_t getGeneration() { return mGeneration; }
1336
1337 void updateGlobalMetaState() override {
1338 mUpdateGlobalMetaStateWasCalled = true;
1339 ContextImpl::updateGlobalMetaState();
1340 }
1341
1342 int32_t getGlobalMetaState() override {
1343 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1344 }
1345
1346 int32_t bumpGeneration() override {
1347 mGeneration = ContextImpl::bumpGeneration();
1348 return mGeneration;
1349 }
1350 } mFakeContext;
1351
Michael Wrightd02c5b62014-02-10 15:10:22 -08001352 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001353
1354public:
1355 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001356};
1357
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001358// --- InputReaderPolicyTest ---
1359class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001360protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001361 sp<FakeInputReaderPolicy> mFakePolicy;
1362
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001363 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -07001364 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001365};
1366
1367/**
1368 * Check that empty set of viewports is an acceptable configuration.
1369 * Also try to get internal viewport two different ways - by type and by uniqueId.
1370 *
1371 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1372 * Such configuration is not currently allowed.
1373 */
1374TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001375 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001376
1377 // We didn't add any viewports yet, so there shouldn't be any.
1378 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001379 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001380 ASSERT_FALSE(internalViewport);
1381
1382 // Add an internal viewport, then clear it
1383 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001384 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001385 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001386
1387 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001388 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001389 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001390 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001391
1392 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001393 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001394 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001395 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001396
1397 mFakePolicy->clearViewports();
1398 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001399 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001400 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001401 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001402 ASSERT_FALSE(internalViewport);
1403}
1404
1405TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1406 const std::string internalUniqueId = "local:0";
1407 const std::string externalUniqueId = "local:1";
1408 const std::string virtualUniqueId1 = "virtual:2";
1409 const std::string virtualUniqueId2 = "virtual:3";
1410 constexpr int32_t virtualDisplayId1 = 2;
1411 constexpr int32_t virtualDisplayId2 = 3;
1412
1413 // Add an internal viewport
1414 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001415 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1416 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001417 // Add an external viewport
1418 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001419 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1420 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001421 // Add an virtual viewport
1422 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001423 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1424 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001425 // Add another virtual viewport
1426 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001427 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1428 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001429
1430 // Check matching by type for internal
1431 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001432 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001433 ASSERT_TRUE(internalViewport);
1434 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1435
1436 // Check matching by type for external
1437 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001438 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001439 ASSERT_TRUE(externalViewport);
1440 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1441
1442 // Check matching by uniqueId for virtual viewport #1
1443 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001444 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001445 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001446 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001447 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1448 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1449
1450 // Check matching by uniqueId for virtual viewport #2
1451 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001452 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001453 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001454 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001455 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1456 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1457}
1458
1459
1460/**
1461 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1462 * that lookup works by checking display id.
1463 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1464 */
1465TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1466 const std::string uniqueId1 = "uniqueId1";
1467 const std::string uniqueId2 = "uniqueId2";
1468 constexpr int32_t displayId1 = 2;
1469 constexpr int32_t displayId2 = 3;
1470
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001471 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1472 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001473 for (const ViewportType& type : types) {
1474 mFakePolicy->clearViewports();
1475 // Add a viewport
1476 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001477 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1478 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001479 // Add another viewport
1480 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001481 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1482 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001483
1484 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001485 std::optional<DisplayViewport> viewport1 =
1486 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001487 ASSERT_TRUE(viewport1);
1488 ASSERT_EQ(displayId1, viewport1->displayId);
1489 ASSERT_EQ(type, viewport1->type);
1490
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001491 std::optional<DisplayViewport> viewport2 =
1492 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001493 ASSERT_TRUE(viewport2);
1494 ASSERT_EQ(displayId2, viewport2->displayId);
1495 ASSERT_EQ(type, viewport2->type);
1496
1497 // When there are multiple viewports of the same kind, and uniqueId is not specified
1498 // in the call to getDisplayViewport, then that situation is not supported.
1499 // The viewports can be stored in any order, so we cannot rely on the order, since that
1500 // is just implementation detail.
1501 // However, we can check that it still returns *a* viewport, we just cannot assert
1502 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001503 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001504 ASSERT_TRUE(someViewport);
1505 }
1506}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001507
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001508/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001509 * When we have multiple internal displays make sure we always return the default display when
1510 * querying by type.
1511 */
1512TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1513 const std::string uniqueId1 = "uniqueId1";
1514 const std::string uniqueId2 = "uniqueId2";
1515 constexpr int32_t nonDefaultDisplayId = 2;
1516 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1517 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1518
1519 // Add the default display first and ensure it gets returned.
1520 mFakePolicy->clearViewports();
1521 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001522 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001523 ViewportType::INTERNAL);
1524 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001525 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001526 ViewportType::INTERNAL);
1527
1528 std::optional<DisplayViewport> viewport =
1529 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1530 ASSERT_TRUE(viewport);
1531 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1532 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1533
1534 // Add the default display second to make sure order doesn't matter.
1535 mFakePolicy->clearViewports();
1536 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001537 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001538 ViewportType::INTERNAL);
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
1543 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1544 ASSERT_TRUE(viewport);
1545 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1546 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1547}
1548
1549/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001550 * Check getDisplayViewportByPort
1551 */
1552TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001553 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001554 const std::string uniqueId1 = "uniqueId1";
1555 const std::string uniqueId2 = "uniqueId2";
1556 constexpr int32_t displayId1 = 1;
1557 constexpr int32_t displayId2 = 2;
1558 const uint8_t hdmi1 = 0;
1559 const uint8_t hdmi2 = 1;
1560 const uint8_t hdmi3 = 2;
1561
1562 mFakePolicy->clearViewports();
1563 // Add a viewport that's associated with some display port that's not of interest.
1564 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001565 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1566 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001567 // Add another viewport, connected to HDMI1 port
1568 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001569 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1570 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001571
1572 // Check that correct display viewport was returned by comparing the display ports.
1573 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1574 ASSERT_TRUE(hdmi1Viewport);
1575 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1576 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1577
1578 // Check that we can still get the same viewport using the uniqueId
1579 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1580 ASSERT_TRUE(hdmi1Viewport);
1581 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1582 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1583 ASSERT_EQ(type, hdmi1Viewport->type);
1584
1585 // Check that we cannot find a port with "HDMI2", because we never added one
1586 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1587 ASSERT_FALSE(hdmi2Viewport);
1588}
1589
Michael Wrightd02c5b62014-02-10 15:10:22 -08001590// --- InputReaderTest ---
1591
1592class InputReaderTest : public testing::Test {
1593protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001594 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001595 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001596 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001597 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001598
Chris Yea52ade12020-08-27 16:49:20 -07001599 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001600 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001601 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001602 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001603
Prabir Pradhan28efc192019-11-05 01:10:04 +00001604 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001605 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001606 }
1607
Chris Yea52ade12020-08-27 16:49:20 -07001608 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001609 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001610 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001611 }
1612
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001613 void addDevice(int32_t eventHubId, const std::string& name,
1614 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001615 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001616
1617 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001618 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001619 }
1620 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001621 mReader->loopOnce();
1622 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001623 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1624 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001625 }
1626
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001627 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001628 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001629 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001630 }
1631
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001632 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001633 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001634 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001635 }
1636
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001637 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001638 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001639 ftl::Flags<InputDeviceClass> classes,
1640 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001641 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001642 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1643 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001644 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001645 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001646 return mapper;
1647 }
1648};
1649
Chris Ye98d3f532020-10-01 21:48:59 -07001650TEST_F(InputReaderTest, PolicyGetInputDevices) {
1651 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001652 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001653 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001654
1655 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001656 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001657 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001658 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001659 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001660 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1661 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001662 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001663}
1664
Chris Yee7310032020-09-22 15:36:28 -07001665TEST_F(InputReaderTest, GetMergedInputDevices) {
1666 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1667 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1668 // Add two subdevices to device
1669 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1670 // Must add at least one mapper or the device will be ignored!
1671 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1672 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1673
1674 // Push same device instance for next device to be added, so they'll have same identifier.
1675 mReader->pushNextDevice(device);
1676 mReader->pushNextDevice(device);
1677 ASSERT_NO_FATAL_FAILURE(
1678 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1679 ASSERT_NO_FATAL_FAILURE(
1680 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1681
1682 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001683 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001684}
1685
Chris Yee14523a2020-12-19 13:46:00 -08001686TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1687 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1688 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1689 // Add two subdevices to device
1690 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1691 // Must add at least one mapper or the device will be ignored!
1692 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1693 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1694
1695 // Push same device instance for next device to be added, so they'll have same identifier.
1696 mReader->pushNextDevice(device);
1697 mReader->pushNextDevice(device);
1698 // Sensor device is initially disabled
1699 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1700 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1701 nullptr));
1702 // Device is disabled because the only sub device is a sensor device and disabled initially.
1703 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1704 ASSERT_FALSE(device->isEnabled());
1705 ASSERT_NO_FATAL_FAILURE(
1706 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1707 // The merged device is enabled if any sub device is enabled
1708 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1709 ASSERT_TRUE(device->isEnabled());
1710}
1711
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001712TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001713 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001714 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001715 constexpr int32_t eventHubId = 1;
1716 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001717 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001718 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001719 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001720 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001721
Yi Kong9b14ac62018-07-17 13:48:38 -07001722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001723
1724 NotifyDeviceResetArgs resetArgs;
1725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001726 ASSERT_EQ(deviceId, resetArgs.deviceId);
1727
1728 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001729 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001730 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001731
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001733 ASSERT_EQ(deviceId, resetArgs.deviceId);
1734 ASSERT_EQ(device->isEnabled(), false);
1735
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001736 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001737 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001740 ASSERT_EQ(device->isEnabled(), false);
1741
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001742 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001743 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001745 ASSERT_EQ(deviceId, resetArgs.deviceId);
1746 ASSERT_EQ(device->isEnabled(), true);
1747}
1748
Michael Wrightd02c5b62014-02-10 15:10:22 -08001749TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001750 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001751 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001752 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001753 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001754 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001755 AINPUT_SOURCE_KEYBOARD, nullptr);
1756 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001757
1758 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1759 AINPUT_SOURCE_ANY, AKEYCODE_A))
1760 << "Should return unknown when the device id is >= 0 but unknown.";
1761
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001762 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1763 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1764 << "Should return unknown when the device id is valid but the sources are not "
1765 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001766
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001767 ASSERT_EQ(AKEY_STATE_DOWN,
1768 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1769 AKEYCODE_A))
1770 << "Should return value provided by mapper when device id is valid and the device "
1771 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001772
1773 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1774 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1775 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1776
1777 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1778 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1779 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1780}
1781
Philip Junker4af3b3d2021-12-14 10:36:55 +01001782TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1783 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1784 constexpr int32_t eventHubId = 1;
1785 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1786 InputDeviceClass::KEYBOARD,
1787 AINPUT_SOURCE_KEYBOARD, nullptr);
1788 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1789
1790 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1791 << "Should return unknown when the device with the specified id is not found.";
1792
1793 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1794 << "Should return correct mapping when device id is valid and mapping exists.";
1795
1796 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1797 << "Should return the location key code when device id is valid and there's no "
1798 "mapping.";
1799}
1800
1801TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1802 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1803 constexpr int32_t eventHubId = 1;
1804 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1805 InputDeviceClass::JOYSTICK,
1806 AINPUT_SOURCE_GAMEPAD, nullptr);
1807 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1808
1809 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1810 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1811}
1812
Michael Wrightd02c5b62014-02-10 15:10:22 -08001813TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001814 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001815 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001816 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001817 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001818 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001819 AINPUT_SOURCE_KEYBOARD, nullptr);
1820 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001821
1822 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1823 AINPUT_SOURCE_ANY, KEY_A))
1824 << "Should return unknown when the device id is >= 0 but unknown.";
1825
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001826 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1827 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1828 << "Should return unknown when the device id is valid but the sources are not "
1829 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001830
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001831 ASSERT_EQ(AKEY_STATE_DOWN,
1832 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1833 KEY_A))
1834 << "Should return value provided by mapper when device id is valid and the device "
1835 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001836
1837 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1838 AINPUT_SOURCE_TRACKBALL, KEY_A))
1839 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1840
1841 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1842 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1843 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1844}
1845
1846TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001847 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001848 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001849 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001850 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001851 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001852 AINPUT_SOURCE_KEYBOARD, nullptr);
1853 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001854
1855 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1856 AINPUT_SOURCE_ANY, SW_LID))
1857 << "Should return unknown when the device id is >= 0 but unknown.";
1858
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001859 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1860 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1861 << "Should return unknown when the device id is valid but the sources are not "
1862 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001863
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001864 ASSERT_EQ(AKEY_STATE_DOWN,
1865 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1866 SW_LID))
1867 << "Should return value provided by mapper when device id is valid and the device "
1868 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001869
1870 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1871 AINPUT_SOURCE_TRACKBALL, SW_LID))
1872 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1873
1874 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1875 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1876 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1877}
1878
1879TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001880 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001881 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001882 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001883 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001884 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001885 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001886
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001887 mapper.addSupportedKeyCode(AKEYCODE_A);
1888 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001889
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001890 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001891 uint8_t flags[4] = { 0, 0, 0, 1 };
1892
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001893 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08001894 << "Should return false when device id is >= 0 but unknown.";
1895 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1896
1897 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001898 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001899 << "Should return false when device id is valid but the sources are not supported by "
1900 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001901 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1902
1903 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001904 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001905 keyCodes, flags))
1906 << "Should return value provided by mapper when device id is valid and the device "
1907 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001908 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1909
1910 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001911 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1912 << "Should return false when the device id is < 0 but the sources are not supported by "
1913 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001914 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1915
1916 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001917 ASSERT_TRUE(
1918 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1919 << "Should return value provided by mapper when device id is < 0 and one of the "
1920 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001921 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1922}
1923
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001924TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001925 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001926 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001927
1928 NotifyConfigurationChangedArgs args;
1929
1930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1931 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1932}
1933
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001934TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001935 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001936 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001937 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001938 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001939 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001940 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001941 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001942 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001943
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001944 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001945 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001946 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1947
1948 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001949 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001950 ASSERT_EQ(when, event.when);
1951 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001952 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001953 ASSERT_EQ(EV_KEY, event.type);
1954 ASSERT_EQ(KEY_A, event.code);
1955 ASSERT_EQ(1, event.value);
1956}
1957
Garfield Tan1c7bc862020-01-28 13:24:04 -08001958TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001959 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001960 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001961 constexpr int32_t eventHubId = 1;
1962 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001963 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001964 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001965 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001966 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001967
1968 NotifyDeviceResetArgs resetArgs;
1969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001970 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001971
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001972 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001973 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001975 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001976 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001977
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001978 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001979 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001981 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001982 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001983
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001984 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001985 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001987 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001988 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001989}
1990
Garfield Tan1c7bc862020-01-28 13:24:04 -08001991TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1992 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001993 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001994 constexpr int32_t eventHubId = 1;
1995 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1996 // Must add at least one mapper or the device will be ignored!
1997 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001998 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001999 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
2000
2001 NotifyDeviceResetArgs resetArgs;
2002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2003 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
2004}
2005
Arthur Hungc23540e2018-11-29 20:42:11 +08002006TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002007 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002008 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002009 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08002010 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002011 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2012 FakeInputMapper& mapper =
2013 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002014 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08002015
2016 const uint8_t hdmi1 = 1;
2017
2018 // Associated touch screen with second display.
2019 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2020
2021 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00002022 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08002023 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002024 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002025 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002026 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002027 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002028 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002029 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002030 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002031
2032 // Add the device, and make sure all of the callbacks are triggered.
2033 // The device is added after the input port associations are processed since
2034 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002035 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002038 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002039
Arthur Hung2c9a3342019-07-23 14:18:59 +08002040 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002041 ASSERT_EQ(deviceId, device->getId());
2042 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2043 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002044
2045 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002046 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002047 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002048 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002049}
2050
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002051TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2052 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002053 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002054 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2055 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2056 // Must add at least one mapper or the device will be ignored!
2057 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2058 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2059 mReader->pushNextDevice(device);
2060 mReader->pushNextDevice(device);
2061 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2062 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2063
2064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2065
2066 NotifyDeviceResetArgs resetArgs;
2067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2068 ASSERT_EQ(deviceId, resetArgs.deviceId);
2069 ASSERT_TRUE(device->isEnabled());
2070 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2071 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2072
2073 disableDevice(deviceId);
2074 mReader->loopOnce();
2075
2076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2077 ASSERT_EQ(deviceId, resetArgs.deviceId);
2078 ASSERT_FALSE(device->isEnabled());
2079 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2080 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2081
2082 enableDevice(deviceId);
2083 mReader->loopOnce();
2084
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
2092TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2093 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002094 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002095 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2096 // Add two subdevices to device
2097 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2098 FakeInputMapper& mapperDevice1 =
2099 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2100 FakeInputMapper& mapperDevice2 =
2101 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2102 mReader->pushNextDevice(device);
2103 mReader->pushNextDevice(device);
2104 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2105 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2106
2107 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2108 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2109
2110 ASSERT_EQ(AKEY_STATE_DOWN,
2111 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2112 ASSERT_EQ(AKEY_STATE_DOWN,
2113 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2114 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2115 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2116}
2117
Prabir Pradhan7e186182020-11-10 13:56:45 -08002118TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2119 NotifyPointerCaptureChangedArgs args;
2120
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002121 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002122 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2123 mReader->loopOnce();
2124 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002125 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2126 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002127
2128 mFakePolicy->setPointerCapture(false);
2129 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2130 mReader->loopOnce();
2131 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002132 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002133
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002134 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002135 // does not change.
2136 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2137 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002138 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002139}
2140
Chris Ye87143712020-11-10 05:05:58 +00002141class FakeVibratorInputMapper : public FakeInputMapper {
2142public:
2143 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2144 : FakeInputMapper(deviceContext, sources) {}
2145
2146 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2147};
2148
2149TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2150 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002151 ftl::Flags<InputDeviceClass> deviceClass =
2152 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002153 constexpr int32_t eventHubId = 1;
2154 const char* DEVICE_LOCATION = "BLUETOOTH";
2155 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2156 FakeVibratorInputMapper& mapper =
2157 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2158 mReader->pushNextDevice(device);
2159
2160 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2161 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2162
2163 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2164 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2165}
2166
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002167// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002168
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002169class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002170public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002171 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002172
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002173 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002174
Andy Chenf9f1a022022-08-29 20:07:10 -04002175 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
2176
Chris Yee2b1e5c2021-03-10 22:45:12 -08002177 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2178
2179 void dump(std::string& dump) override {}
2180
2181 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2182 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002183 }
2184
Chris Yee2b1e5c2021-03-10 22:45:12 -08002185 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2186 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002187 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002188
2189 bool setLightColor(int32_t lightId, int32_t color) override {
2190 getDeviceContext().setLightBrightness(lightId, color >> 24);
2191 return true;
2192 }
2193
2194 std::optional<int32_t> getLightColor(int32_t lightId) override {
2195 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2196 if (!result.has_value()) {
2197 return std::nullopt;
2198 }
2199 return result.value() << 24;
2200 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002201
2202 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2203
2204 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2205
2206private:
2207 InputDeviceContext& mDeviceContext;
2208 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2209 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04002210 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002211};
2212
Chris Yee2b1e5c2021-03-10 22:45:12 -08002213TEST_F(InputReaderTest, BatteryGetCapacity) {
2214 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002215 ftl::Flags<InputDeviceClass> deviceClass =
2216 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002217 constexpr int32_t eventHubId = 1;
2218 const char* DEVICE_LOCATION = "BLUETOOTH";
2219 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002220 FakePeripheralController& controller =
2221 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002222 mReader->pushNextDevice(device);
2223
2224 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2225
2226 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2227 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2228}
2229
2230TEST_F(InputReaderTest, BatteryGetStatus) {
2231 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002232 ftl::Flags<InputDeviceClass> deviceClass =
2233 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002234 constexpr int32_t eventHubId = 1;
2235 const char* DEVICE_LOCATION = "BLUETOOTH";
2236 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002237 FakePeripheralController& controller =
2238 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002239 mReader->pushNextDevice(device);
2240
2241 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2242
2243 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2244 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2245}
2246
Prabir Pradhane287ecd2022-09-07 21:18:05 +00002247TEST_F(InputReaderTest, BatteryGetDevicePath) {
2248 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2249 ftl::Flags<InputDeviceClass> deviceClass =
2250 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2251 constexpr int32_t eventHubId = 1;
2252 const char* DEVICE_LOCATION = "BLUETOOTH";
2253 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2254 device->addController<FakePeripheralController>(eventHubId);
2255 mReader->pushNextDevice(device);
2256
2257 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2258
2259 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), BATTERY_DEVPATH);
2260}
2261
Chris Ye3fdbfef2021-01-06 18:45:18 -08002262TEST_F(InputReaderTest, LightGetColor) {
2263 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002264 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002265 constexpr int32_t eventHubId = 1;
2266 const char* DEVICE_LOCATION = "BLUETOOTH";
2267 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002268 FakePeripheralController& controller =
2269 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002270 mReader->pushNextDevice(device);
2271 RawLightInfo info = {.id = 1,
2272 .name = "Mono",
2273 .maxBrightness = 255,
2274 .flags = InputLightClass::BRIGHTNESS,
2275 .path = ""};
2276 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2277 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2278
2279 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002280
Chris Yee2b1e5c2021-03-10 22:45:12 -08002281 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2282 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002283 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2284 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2285}
2286
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002287// --- InputReaderIntegrationTest ---
2288
2289// These tests create and interact with the InputReader only through its interface.
2290// The InputReader is started during SetUp(), which starts its processing in its own
2291// thread. The tests use linux uinput to emulate input devices.
2292// NOTE: Interacting with the physical device while these tests are running may cause
2293// the tests to fail.
2294class InputReaderIntegrationTest : public testing::Test {
2295protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002296 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002297 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002298 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002299
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002300 std::shared_ptr<FakePointerController> mFakePointerController;
2301
Chris Yea52ade12020-08-27 16:49:20 -07002302 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002303#if !defined(__ANDROID__)
2304 GTEST_SKIP();
2305#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002306 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002307 mFakePointerController = std::make_shared<FakePointerController>();
2308 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002309 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2310 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002311
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002312 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2313 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002314 ASSERT_EQ(mReader->start(), OK);
2315
2316 // Since this test is run on a real device, all the input devices connected
2317 // to the test device will show up in mReader. We wait for those input devices to
2318 // show up before beginning the tests.
2319 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2320 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2321 }
2322
Chris Yea52ade12020-08-27 16:49:20 -07002323 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002324#if !defined(__ANDROID__)
2325 return;
2326#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002327 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002328 mReader.reset();
2329 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002330 mFakePolicy.clear();
2331 }
2332};
2333
2334TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2335 // An invalid input device that is only used for this test.
2336 class InvalidUinputDevice : public UinputDevice {
2337 public:
2338 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2339
2340 private:
2341 void configureDevice(int fd, uinput_user_dev* device) override {}
2342 };
2343
2344 const size_t numDevices = mFakePolicy->getInputDevices().size();
2345
2346 // UinputDevice does not set any event or key bits, so InputReader should not
2347 // consider it as a valid device.
2348 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2349 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2350 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2351 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2352
2353 invalidDevice.reset();
2354 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2355 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2356 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2357}
2358
2359TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2360 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2361
2362 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2363 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2364 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2365 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2366
2367 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002368 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002369 const auto& it =
2370 std::find_if(inputDevices.begin(), inputDevices.end(),
2371 [&keyboard](const InputDeviceInfo& info) {
2372 return info.getIdentifier().name == keyboard->getName();
2373 });
2374
2375 ASSERT_NE(it, inputDevices.end());
2376 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2377 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2378 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002379
2380 keyboard.reset();
2381 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2382 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2383 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2384}
2385
2386TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2387 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2388 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2389
2390 NotifyConfigurationChangedArgs configChangedArgs;
2391 ASSERT_NO_FATAL_FAILURE(
2392 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002393 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002394 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2395
2396 NotifyKeyArgs keyArgs;
2397 keyboard->pressAndReleaseHomeKey();
2398 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2399 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002400 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002401 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002402 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002403 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002404 prevTimestamp = keyArgs.eventTime;
2405
2406 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2407 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002408 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002409 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002410 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002411}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002412
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002413/**
2414 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2415 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2416 * are passed to the listener.
2417 */
2418static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2419TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2420 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2421 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2422 NotifyKeyArgs keyArgs;
2423
2424 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2425 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2426 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2427 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2428
2429 controller->pressAndReleaseKey(BTN_GEAR_UP);
2430 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2431 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2432 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2433}
2434
Arthur Hungaab25622020-01-16 11:22:11 +08002435// --- TouchProcessTest ---
2436class TouchIntegrationTest : public InputReaderIntegrationTest {
2437protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002438 const std::string UNIQUE_ID = "local:0";
2439
Chris Yea52ade12020-08-27 16:49:20 -07002440 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002441#if !defined(__ANDROID__)
2442 GTEST_SKIP();
2443#endif
Arthur Hungaab25622020-01-16 11:22:11 +08002444 InputReaderIntegrationTest::SetUp();
2445 // At least add an internal display.
2446 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2447 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002448 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002449
2450 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2451 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2452 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2453 }
2454
2455 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2456 int32_t orientation, const std::string& uniqueId,
2457 std::optional<uint8_t> physicalPort,
2458 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002459 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2460 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002461 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2462 }
2463
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002464 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2465 NotifyMotionArgs args;
2466 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2467 EXPECT_EQ(action, args.action);
2468 ASSERT_EQ(points.size(), args.pointerCount);
2469 for (size_t i = 0; i < args.pointerCount; i++) {
2470 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2471 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2472 }
2473 }
2474
Arthur Hungaab25622020-01-16 11:22:11 +08002475 std::unique_ptr<UinputTouchScreen> mDevice;
2476};
2477
2478TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2479 NotifyMotionArgs args;
2480 const Point centerPoint = mDevice->getCenterPoint();
2481
2482 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002483 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002484 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002485 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002486 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2487 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2488
2489 // ACTION_MOVE
2490 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002491 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002492 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2493 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2494
2495 // ACTION_UP
2496 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002497 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002498 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2499 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2500}
2501
2502TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2503 NotifyMotionArgs args;
2504 const Point centerPoint = mDevice->getCenterPoint();
2505
2506 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002507 mDevice->sendSlot(FIRST_SLOT);
2508 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002509 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002510 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002511 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2512 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2513
2514 // ACTION_POINTER_DOWN (Second slot)
2515 const Point secondPoint = centerPoint + Point(100, 100);
2516 mDevice->sendSlot(SECOND_SLOT);
2517 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002518 mDevice->sendDown(secondPoint);
2519 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002520 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002521 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002522
2523 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002524 mDevice->sendMove(secondPoint + Point(1, 1));
2525 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002526 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2527 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2528
2529 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002530 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002531 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002532 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002533 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002534
2535 // ACTION_UP
2536 mDevice->sendSlot(FIRST_SLOT);
2537 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002538 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002539 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2540 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2541}
2542
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002543/**
2544 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2545 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2546 * data?
2547 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2548 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2549 * for Pointer 0 only is generated after.
2550 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2551 * events, we will not miss any information.
2552 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2553 * event generated afterwards that contains the newest movement of pointer 0.
2554 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2555 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2556 * losing information about non-palm pointers.
2557 */
2558TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2559 NotifyMotionArgs args;
2560 const Point centerPoint = mDevice->getCenterPoint();
2561
2562 // ACTION_DOWN
2563 mDevice->sendSlot(FIRST_SLOT);
2564 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2565 mDevice->sendDown(centerPoint);
2566 mDevice->sendSync();
2567 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2568
2569 // ACTION_POINTER_DOWN (Second slot)
2570 const Point secondPoint = centerPoint + Point(100, 100);
2571 mDevice->sendSlot(SECOND_SLOT);
2572 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2573 mDevice->sendDown(secondPoint);
2574 mDevice->sendSync();
2575 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2576
2577 // ACTION_MOVE (First slot)
2578 mDevice->sendSlot(FIRST_SLOT);
2579 mDevice->sendMove(centerPoint + Point(5, 5));
2580 // ACTION_POINTER_UP (Second slot)
2581 mDevice->sendSlot(SECOND_SLOT);
2582 mDevice->sendPointerUp();
2583 // Send a single sync for the above 2 pointer updates
2584 mDevice->sendSync();
2585
2586 // First, we should get POINTER_UP for the second pointer
2587 assertReceivedMotion(ACTION_POINTER_1_UP,
2588 {/*first pointer */ centerPoint + Point(5, 5),
2589 /*second pointer*/ secondPoint});
2590
2591 // Next, the MOVE event for the first pointer
2592 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2593}
2594
2595/**
2596 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2597 * move, and then it will go up, all in the same frame.
2598 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2599 * gets sent to the listener.
2600 */
2601TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2602 NotifyMotionArgs args;
2603 const Point centerPoint = mDevice->getCenterPoint();
2604
2605 // ACTION_DOWN
2606 mDevice->sendSlot(FIRST_SLOT);
2607 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2608 mDevice->sendDown(centerPoint);
2609 mDevice->sendSync();
2610 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2611
2612 // ACTION_POINTER_DOWN (Second slot)
2613 const Point secondPoint = centerPoint + Point(100, 100);
2614 mDevice->sendSlot(SECOND_SLOT);
2615 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2616 mDevice->sendDown(secondPoint);
2617 mDevice->sendSync();
2618 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2619
2620 // ACTION_MOVE (First slot)
2621 mDevice->sendSlot(FIRST_SLOT);
2622 mDevice->sendMove(centerPoint + Point(5, 5));
2623 // ACTION_POINTER_UP (Second slot)
2624 mDevice->sendSlot(SECOND_SLOT);
2625 mDevice->sendMove(secondPoint + Point(6, 6));
2626 mDevice->sendPointerUp();
2627 // Send a single sync for the above 2 pointer updates
2628 mDevice->sendSync();
2629
2630 // First, we should get POINTER_UP for the second pointer
2631 // The movement of the second pointer during the liftoff frame is ignored.
2632 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2633 assertReceivedMotion(ACTION_POINTER_1_UP,
2634 {/*first pointer */ centerPoint + Point(5, 5),
2635 /*second pointer*/ secondPoint});
2636
2637 // Next, the MOVE event for the first pointer
2638 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2639}
2640
Arthur Hungaab25622020-01-16 11:22:11 +08002641TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2642 NotifyMotionArgs args;
2643 const Point centerPoint = mDevice->getCenterPoint();
2644
2645 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002646 mDevice->sendSlot(FIRST_SLOT);
2647 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002648 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002649 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002650 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2651 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2652
arthurhungcc7f9802020-04-30 17:55:40 +08002653 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002654 const Point secondPoint = centerPoint + Point(100, 100);
2655 mDevice->sendSlot(SECOND_SLOT);
2656 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2657 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002658 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002659 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002660 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002661
arthurhungcc7f9802020-04-30 17:55:40 +08002662 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002663 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002664 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002665 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2666 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2667
arthurhungcc7f9802020-04-30 17:55:40 +08002668 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2669 // a palm event.
2670 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002671 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002672 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002673 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002674 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002675 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002676
arthurhungcc7f9802020-04-30 17:55:40 +08002677 // Send up to second slot, expect first slot send moving.
2678 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002679 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002680 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2681 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002682
arthurhungcc7f9802020-04-30 17:55:40 +08002683 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002684 mDevice->sendSlot(FIRST_SLOT);
2685 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002686 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002687
arthurhungcc7f9802020-04-30 17:55:40 +08002688 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2689 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002690}
2691
Michael Wrightd02c5b62014-02-10 15:10:22 -08002692// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002693class InputDeviceTest : public testing::Test {
2694protected:
2695 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002696 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002697 static const int32_t DEVICE_ID;
2698 static const int32_t DEVICE_GENERATION;
2699 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002700 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002701 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002702
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002703 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002704 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002705 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002706 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002707 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002708
Chris Yea52ade12020-08-27 16:49:20 -07002709 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002710 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002711 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002712 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002713 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002714 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002715 InputDeviceIdentifier identifier;
2716 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002717 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002718 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002719 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002720 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002721 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002722 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002723 }
2724
Chris Yea52ade12020-08-27 16:49:20 -07002725 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002726 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002727 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002728 }
2729};
2730
2731const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002732const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002733const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002734const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2735const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002736const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002737 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002738const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002739
2740TEST_F(InputDeviceTest, ImmutableProperties) {
2741 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002742 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002743 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002744}
2745
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002746TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
2747 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
2748
2749 // Configuration
2750 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2751 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002752 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002753
2754 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
2755}
2756
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002757TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2758 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002759}
2760
Michael Wrightd02c5b62014-02-10 15:10:22 -08002761TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2762 // Configuration.
2763 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002764 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002765
2766 // Reset.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002767 unused += mDevice->reset(ARBITRARY_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002768
2769 NotifyDeviceResetArgs resetArgs;
2770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2771 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2772 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2773
2774 // Metadata.
2775 ASSERT_TRUE(mDevice->isIgnored());
2776 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2777
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002778 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002779 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002780 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002781 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2782 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2783
2784 // State queries.
2785 ASSERT_EQ(0, mDevice->getMetaState());
2786
2787 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2788 << "Ignored device should return unknown key code state.";
2789 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2790 << "Ignored device should return unknown scan code state.";
2791 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2792 << "Ignored device should return unknown switch state.";
2793
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002794 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002795 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002796 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002797 << "Ignored device should never mark any key codes.";
2798 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2799 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2800}
2801
2802TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2803 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002804 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08002805
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002806 FakeInputMapper& mapper1 =
2807 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002808 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2809 mapper1.setMetaState(AMETA_ALT_ON);
2810 mapper1.addSupportedKeyCode(AKEYCODE_A);
2811 mapper1.addSupportedKeyCode(AKEYCODE_B);
2812 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2813 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2814 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2815 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2816 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002817
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002818 FakeInputMapper& mapper2 =
2819 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002820 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002821
2822 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002823 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002824
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002825 std::string propertyValue;
2826 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002827 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002828 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002829
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002830 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2831 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002832
2833 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002834 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002835 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2836 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002837
2838 NotifyDeviceResetArgs resetArgs;
2839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2840 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2841 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2842
2843 // Metadata.
2844 ASSERT_FALSE(mDevice->isIgnored());
2845 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2846
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002847 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002848 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002849 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002850 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2851 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2852
2853 // State queries.
2854 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2855 << "Should query mappers and combine meta states.";
2856
2857 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2858 << "Should return unknown key code state when source not supported.";
2859 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2860 << "Should return unknown scan code state when source not supported.";
2861 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2862 << "Should return unknown switch state when source not supported.";
2863
2864 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2865 << "Should query mapper when source is supported.";
2866 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2867 << "Should query mapper when source is supported.";
2868 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2869 << "Should query mapper when source is supported.";
2870
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002871 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002872 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002873 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002874 << "Should do nothing when source is unsupported.";
2875 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2876 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2877 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2878 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2879
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002880 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002881 << "Should query mapper when source is supported.";
2882 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2883 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2884 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2885 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2886
2887 // Event handling.
2888 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002889 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002890 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002891
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002892 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2893 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002894}
2895
Arthur Hung2c9a3342019-07-23 14:18:59 +08002896// A single input device is associated with a specific display. Check that:
2897// 1. Device is disabled if the viewport corresponding to the associated display is not found
2898// 2. Device is disabled when setEnabled API is called
2899TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002900 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002901
2902 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002903 std::list<NotifyArgs> unused =
2904 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002905
2906 // Device should be enabled by default.
2907 ASSERT_TRUE(mDevice->isEnabled());
2908
2909 // Prepare associated info.
2910 constexpr uint8_t hdmi = 1;
2911 const std::string UNIQUE_ID = "local:1";
2912
2913 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002914 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2915 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002916 // Device should be disabled because it is associated with a specific display via
2917 // input port <-> display port association, but the corresponding display is not found
2918 ASSERT_FALSE(mDevice->isEnabled());
2919
2920 // Prepare displays.
2921 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002922 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2923 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002924 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2925 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002926 ASSERT_TRUE(mDevice->isEnabled());
2927
2928 // Device should be disabled after set disable.
2929 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002930 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2931 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002932 ASSERT_FALSE(mDevice->isEnabled());
2933
2934 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002935 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2936 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002937 ASSERT_FALSE(mDevice->isEnabled());
2938}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002939
Christine Franks1ba71cc2021-04-07 14:37:42 -07002940TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2941 // Device should be enabled by default.
2942 mFakePolicy->clearViewports();
2943 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002944 std::list<NotifyArgs> unused =
2945 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002946 ASSERT_TRUE(mDevice->isEnabled());
2947
2948 // Device should be disabled because it is associated with a specific display, but the
2949 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002950 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002951 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2952 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002953 ASSERT_FALSE(mDevice->isEnabled());
2954
2955 // Device should be enabled when a display is found.
2956 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2957 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2958 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002959 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2960 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002961 ASSERT_TRUE(mDevice->isEnabled());
2962
2963 // Device should be disabled after set disable.
2964 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002965 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2966 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002967 ASSERT_FALSE(mDevice->isEnabled());
2968
2969 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002970 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2971 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002972 ASSERT_FALSE(mDevice->isEnabled());
2973}
2974
Christine Franks2a2293c2022-01-18 11:51:16 -08002975TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2976 mFakePolicy->clearViewports();
2977 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002978 std::list<NotifyArgs> unused =
2979 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks2a2293c2022-01-18 11:51:16 -08002980
Christine Franks2a2293c2022-01-18 11:51:16 -08002981 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2982 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2983 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2984 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002985 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2986 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002987 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2988}
2989
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002990/**
2991 * This test reproduces a crash caused by a dangling reference that remains after device is added
2992 * and removed. The reference is accessed in InputDevice::dump(..);
2993 */
2994TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2995 constexpr int32_t TEST_EVENTHUB_ID = 10;
2996 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2997
2998 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
2999 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
3000 device.removeEventHubDevice(TEST_EVENTHUB_ID);
3001 std::string dumpStr, eventHubDevStr;
3002 device.dump(dumpStr, eventHubDevStr);
3003}
3004
Michael Wrightd02c5b62014-02-10 15:10:22 -08003005// --- InputMapperTest ---
3006
3007class InputMapperTest : public testing::Test {
3008protected:
3009 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003010 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003011 static const int32_t DEVICE_ID;
3012 static const int32_t DEVICE_GENERATION;
3013 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003014 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003015 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003016
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003017 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003018 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003019 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08003020 std::unique_ptr<InstrumentedInputReader> mReader;
3021 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003022
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003023 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003024 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003025 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003026 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08003027 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003028 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08003029 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003030 // Consume the device reset notification generated when adding a new device.
3031 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003032 }
3033
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003034 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003035 SetUp(DEVICE_CLASSES);
3036 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003037
Chris Yea52ade12020-08-27 16:49:20 -07003038 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003039 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003040 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003041 }
3042
3043 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003044 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003045 }
3046
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003047 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00003048 if (!changes ||
3049 (changes &
3050 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
3051 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003052 mReader->requestRefreshConfiguration(changes);
3053 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08003054 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003055 std::list<NotifyArgs> out =
3056 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003057 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003058 for (const NotifyArgs& args : out) {
3059 mFakeListener->notify(args);
3060 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003061 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003062 return out;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003063 }
3064
arthurhungdcef2dc2020-08-11 14:47:50 +08003065 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
3066 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003067 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003068 InputDeviceIdentifier identifier;
3069 identifier.name = name;
3070 identifier.location = location;
3071 std::shared_ptr<InputDevice> device =
3072 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
3073 identifier);
3074 mReader->pushNextDevice(device);
3075 mFakeEventHub->addDevice(eventHubId, name, classes);
3076 mReader->loopOnce();
3077 return device;
3078 }
3079
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003080 template <class T, typename... Args>
3081 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003082 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003083 configureDevice(0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003084 std::list<NotifyArgs> resetArgList = mDevice->reset(ARBITRARY_TIME);
3085 resetArgList += mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003086 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003087 for (const NotifyArgs& loopArgs : resetArgList) {
3088 mFakeListener->notify(loopArgs);
3089 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003090 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003091 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092 }
3093
3094 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003095 int32_t orientation, const std::string& uniqueId,
3096 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003097 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3098 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003099 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3100 }
3101
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003102 void clearViewports() {
3103 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003104 }
3105
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003106 std::list<NotifyArgs> process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type,
3107 int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003108 RawEvent event;
3109 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003110 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003111 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003112 event.type = type;
3113 event.code = code;
3114 event.value = value;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003115 std::list<NotifyArgs> processArgList = mapper.process(&event);
3116 for (const NotifyArgs& args : processArgList) {
3117 mFakeListener->notify(args);
3118 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003119 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003120 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003121 return processArgList;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003122 }
3123
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00003124 void resetMapper(InputMapper& mapper, nsecs_t when) {
3125 const auto resetArgs = mapper.reset(when);
3126 for (const auto args : resetArgs) {
3127 mFakeListener->notify(args);
3128 }
3129 // Loop the reader to flush the input listener queue.
3130 mReader->loopOnce();
3131 }
3132
Michael Wrightd02c5b62014-02-10 15:10:22 -08003133 static void assertMotionRange(const InputDeviceInfo& info,
3134 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3135 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003136 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003137 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3138 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3139 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3140 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3141 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3142 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3143 }
3144
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003145 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3146 float size, float touchMajor, float touchMinor, float toolMajor,
3147 float toolMinor, float orientation, float distance,
3148 float scaledAxisEpsilon = 1.f) {
3149 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3150 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003151 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3152 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003153 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3154 scaledAxisEpsilon);
3155 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3156 scaledAxisEpsilon);
3157 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3158 scaledAxisEpsilon);
3159 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3160 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003161 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3162 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3163 }
3164
Michael Wright17db18e2020-06-26 20:51:44 +01003165 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003166 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003167 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003168 ASSERT_NEAR(x, actualX, 1);
3169 ASSERT_NEAR(y, actualY, 1);
3170 }
3171};
3172
3173const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003174const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003175const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003176const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3177const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003178const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3179 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003180const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003181
3182// --- SwitchInputMapperTest ---
3183
3184class SwitchInputMapperTest : public InputMapperTest {
3185protected:
3186};
3187
3188TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003189 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003190
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003191 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003192}
3193
3194TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003195 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003196
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003197 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003198 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003199
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003200 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003201 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003202}
3203
3204TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003205 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003206 std::list<NotifyArgs> out;
3207 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3208 ASSERT_TRUE(out.empty());
3209 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3210 ASSERT_TRUE(out.empty());
3211 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3212 ASSERT_TRUE(out.empty());
3213 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003214
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003215 ASSERT_EQ(1u, out.size());
3216 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003217 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003218 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3219 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003220 args.switchMask);
3221 ASSERT_EQ(uint32_t(0), args.policyFlags);
3222}
3223
Chris Ye87143712020-11-10 05:05:58 +00003224// --- VibratorInputMapperTest ---
3225class VibratorInputMapperTest : public InputMapperTest {
3226protected:
3227 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3228};
3229
3230TEST_F(VibratorInputMapperTest, GetSources) {
3231 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3232
3233 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3234}
3235
3236TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3237 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3238
3239 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3240}
3241
3242TEST_F(VibratorInputMapperTest, Vibrate) {
3243 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003244 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003245 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3246
3247 VibrationElement pattern(2);
3248 VibrationSequence sequence(2);
3249 pattern.duration = std::chrono::milliseconds(200);
3250 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3251 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3252 sequence.addElement(pattern);
3253 pattern.duration = std::chrono::milliseconds(500);
3254 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3255 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3256 sequence.addElement(pattern);
3257
3258 std::vector<int64_t> timings = {0, 1};
3259 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3260
3261 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003262 // Start vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003263 std::list<NotifyArgs> out = mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003264 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003265 // Verify vibrator state listener was notified.
3266 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003267 ASSERT_EQ(1u, out.size());
3268 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3269 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
3270 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08003271 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003272 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08003273 ASSERT_FALSE(mapper.isVibrating());
3274 // Verify vibrator state listener was notified.
3275 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003276 ASSERT_EQ(1u, out.size());
3277 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3278 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
3279 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003280}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003281
Chris Yef59a2f42020-10-16 12:55:26 -07003282// --- SensorInputMapperTest ---
3283
3284class SensorInputMapperTest : public InputMapperTest {
3285protected:
3286 static const int32_t ACCEL_RAW_MIN;
3287 static const int32_t ACCEL_RAW_MAX;
3288 static const int32_t ACCEL_RAW_FUZZ;
3289 static const int32_t ACCEL_RAW_FLAT;
3290 static const int32_t ACCEL_RAW_RESOLUTION;
3291
3292 static const int32_t GYRO_RAW_MIN;
3293 static const int32_t GYRO_RAW_MAX;
3294 static const int32_t GYRO_RAW_FUZZ;
3295 static const int32_t GYRO_RAW_FLAT;
3296 static const int32_t GYRO_RAW_RESOLUTION;
3297
3298 static const float GRAVITY_MS2_UNIT;
3299 static const float DEGREE_RADIAN_UNIT;
3300
3301 void prepareAccelAxes();
3302 void prepareGyroAxes();
3303 void setAccelProperties();
3304 void setGyroProperties();
3305 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3306};
3307
3308const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3309const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3310const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3311const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3312const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3313
3314const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3315const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3316const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3317const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3318const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3319
3320const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3321const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3322
3323void SensorInputMapperTest::prepareAccelAxes() {
3324 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3325 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3326 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3327 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3328 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3329 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3330}
3331
3332void SensorInputMapperTest::prepareGyroAxes() {
3333 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3334 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3335 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3336 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3337 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3338 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3339}
3340
3341void SensorInputMapperTest::setAccelProperties() {
3342 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3343 /* sensorDataIndex */ 0);
3344 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3345 /* sensorDataIndex */ 1);
3346 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3347 /* sensorDataIndex */ 2);
3348 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3349 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3350 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3351 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3352 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3353}
3354
3355void SensorInputMapperTest::setGyroProperties() {
3356 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3357 /* sensorDataIndex */ 0);
3358 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3359 /* sensorDataIndex */ 1);
3360 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3361 /* sensorDataIndex */ 2);
3362 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3363 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3364 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3365 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3366 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3367}
3368
3369TEST_F(SensorInputMapperTest, GetSources) {
3370 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3371
3372 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3373}
3374
3375TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3376 setAccelProperties();
3377 prepareAccelAxes();
3378 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3379
3380 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3381 std::chrono::microseconds(10000),
3382 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003383 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003384 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3385 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3386 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3387 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3388 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003389
3390 NotifySensorArgs args;
3391 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3392 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3393 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3394
3395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3396 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3397 ASSERT_EQ(args.deviceId, DEVICE_ID);
3398 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3399 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3400 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3401 ASSERT_EQ(args.values, values);
3402 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3403}
3404
3405TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3406 setGyroProperties();
3407 prepareGyroAxes();
3408 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3409
3410 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3411 std::chrono::microseconds(10000),
3412 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003413 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003414 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3415 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3416 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3417 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3418 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003419
3420 NotifySensorArgs args;
3421 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3422 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3423 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3424
3425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3426 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3427 ASSERT_EQ(args.deviceId, DEVICE_ID);
3428 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3429 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3430 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3431 ASSERT_EQ(args.values, values);
3432 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3433}
3434
Michael Wrightd02c5b62014-02-10 15:10:22 -08003435// --- KeyboardInputMapperTest ---
3436
3437class KeyboardInputMapperTest : public InputMapperTest {
3438protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003439 const std::string UNIQUE_ID = "local:0";
3440
3441 void prepareDisplay(int32_t orientation);
3442
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003443 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003444 int32_t originalKeyCode, int32_t rotatedKeyCode,
3445 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003446};
3447
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003448/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3449 * orientation.
3450 */
3451void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003452 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3453 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003454}
3455
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003456void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003457 int32_t originalScanCode, int32_t originalKeyCode,
3458 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003459 NotifyKeyArgs args;
3460
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003461 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3463 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3464 ASSERT_EQ(originalScanCode, args.scanCode);
3465 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003466 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003467
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003468 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3470 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3471 ASSERT_EQ(originalScanCode, args.scanCode);
3472 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003473 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003474}
3475
Michael Wrightd02c5b62014-02-10 15:10:22 -08003476TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003477 KeyboardInputMapper& mapper =
3478 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3479 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003480
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003481 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003482}
3483
3484TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3485 const int32_t USAGE_A = 0x070004;
3486 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003487 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3488 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003489 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3490 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3491 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003492
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003493 KeyboardInputMapper& mapper =
3494 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3495 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003496 // Initial metastate is AMETA_NONE.
3497 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003498
3499 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003500 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003501 NotifyKeyArgs args;
3502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3503 ASSERT_EQ(DEVICE_ID, args.deviceId);
3504 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3505 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3506 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3507 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3508 ASSERT_EQ(KEY_HOME, args.scanCode);
3509 ASSERT_EQ(AMETA_NONE, args.metaState);
3510 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3511 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3512 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3513
3514 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003515 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3517 ASSERT_EQ(DEVICE_ID, args.deviceId);
3518 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3519 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3520 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3521 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3522 ASSERT_EQ(KEY_HOME, args.scanCode);
3523 ASSERT_EQ(AMETA_NONE, args.metaState);
3524 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3525 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3526 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3527
3528 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003529 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3530 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3532 ASSERT_EQ(DEVICE_ID, args.deviceId);
3533 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3534 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3535 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3536 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3537 ASSERT_EQ(0, args.scanCode);
3538 ASSERT_EQ(AMETA_NONE, args.metaState);
3539 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3540 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3541 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3542
3543 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003544 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3545 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3547 ASSERT_EQ(DEVICE_ID, args.deviceId);
3548 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3549 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3550 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3551 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3552 ASSERT_EQ(0, args.scanCode);
3553 ASSERT_EQ(AMETA_NONE, args.metaState);
3554 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3555 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3556 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3557
3558 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003559 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3560 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3562 ASSERT_EQ(DEVICE_ID, args.deviceId);
3563 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3564 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3565 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3566 ASSERT_EQ(0, args.keyCode);
3567 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3568 ASSERT_EQ(AMETA_NONE, args.metaState);
3569 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3570 ASSERT_EQ(0U, args.policyFlags);
3571 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3572
3573 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003574 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3575 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3577 ASSERT_EQ(DEVICE_ID, args.deviceId);
3578 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3579 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3580 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3581 ASSERT_EQ(0, args.keyCode);
3582 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3583 ASSERT_EQ(AMETA_NONE, args.metaState);
3584 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3585 ASSERT_EQ(0U, args.policyFlags);
3586 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3587}
3588
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003589/**
3590 * Ensure that the readTime is set to the time when the EV_KEY is received.
3591 */
3592TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3593 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3594
3595 KeyboardInputMapper& mapper =
3596 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3597 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3598 NotifyKeyArgs args;
3599
3600 // Key down
3601 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3603 ASSERT_EQ(12, args.readTime);
3604
3605 // Key up
3606 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3608 ASSERT_EQ(15, args.readTime);
3609}
3610
Michael Wrightd02c5b62014-02-10 15:10:22 -08003611TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003612 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3613 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003614 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3615 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3616 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003617
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003618 KeyboardInputMapper& mapper =
3619 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3620 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003621
Arthur Hung95f68612022-04-07 14:08:22 +08003622 // Initial metastate is AMETA_NONE.
3623 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003624
3625 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003626 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003627 NotifyKeyArgs args;
3628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3629 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003630 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003631 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003632
3633 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003634 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3636 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003637 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003638
3639 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003640 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3642 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003643 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003644
3645 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003646 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3648 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003649 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003650 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003651}
3652
3653TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003654 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3655 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3656 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3657 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003658
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003659 KeyboardInputMapper& mapper =
3660 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3661 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003662
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003663 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003664 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3665 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3666 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3667 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3668 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3669 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3670 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3671 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3672}
3673
3674TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003675 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3676 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3677 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3678 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003679
Michael Wrightd02c5b62014-02-10 15:10:22 -08003680 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003681 KeyboardInputMapper& mapper =
3682 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3683 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003684
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003685 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003686 ASSERT_NO_FATAL_FAILURE(
3687 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3688 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3689 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3690 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3691 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3692 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3693 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003694
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003695 clearViewports();
3696 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003697 ASSERT_NO_FATAL_FAILURE(
3698 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3699 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3700 AKEYCODE_DPAD_UP, DISPLAY_ID));
3701 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3702 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3703 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3704 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003705
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003706 clearViewports();
3707 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003708 ASSERT_NO_FATAL_FAILURE(
3709 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3710 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3711 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3712 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3713 AKEYCODE_DPAD_UP, DISPLAY_ID));
3714 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3715 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003716
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003717 clearViewports();
3718 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003719 ASSERT_NO_FATAL_FAILURE(
3720 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3721 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3722 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3723 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3724 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3725 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3726 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003727
3728 // Special case: if orientation changes while key is down, we still emit the same keycode
3729 // in the key up as we did in the key down.
3730 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003731 clearViewports();
3732 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003733 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3735 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3736 ASSERT_EQ(KEY_UP, args.scanCode);
3737 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3738
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003739 clearViewports();
3740 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003741 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3743 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3744 ASSERT_EQ(KEY_UP, args.scanCode);
3745 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3746}
3747
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003748TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3749 // If the keyboard is not orientation aware,
3750 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003751 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003752
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003753 KeyboardInputMapper& mapper =
3754 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3755 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003756 NotifyKeyArgs args;
3757
3758 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003759 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003761 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3763 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3764
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003765 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003766 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003768 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3770 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3771}
3772
3773TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3774 // If the keyboard is orientation aware,
3775 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003776 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003777
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003778 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003779 KeyboardInputMapper& mapper =
3780 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3781 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003782 NotifyKeyArgs args;
3783
3784 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3785 // ^--- already checked by the previous test
3786
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003787 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003788 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003789 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003791 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3793 ASSERT_EQ(DISPLAY_ID, args.displayId);
3794
3795 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003796 clearViewports();
3797 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003798 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3803 ASSERT_EQ(newDisplayId, args.displayId);
3804}
3805
Michael Wrightd02c5b62014-02-10 15:10:22 -08003806TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003807 KeyboardInputMapper& mapper =
3808 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3809 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003810
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003811 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003812 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003813
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003814 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003815 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003816}
3817
Philip Junker4af3b3d2021-12-14 10:36:55 +01003818TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3819 KeyboardInputMapper& mapper =
3820 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3821 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3822
3823 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3824 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3825 << "If a mapping is available, the result is equal to the mapping";
3826
3827 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3828 << "If no mapping is available, the result is the key location";
3829}
3830
Michael Wrightd02c5b62014-02-10 15:10:22 -08003831TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003832 KeyboardInputMapper& mapper =
3833 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3834 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003835
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003836 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003837 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003838
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003839 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003840 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003841}
3842
3843TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003844 KeyboardInputMapper& mapper =
3845 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3846 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003847
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003848 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003849
Michael Wrightd02c5b62014-02-10 15:10:22 -08003850 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003851 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003852 ASSERT_TRUE(flags[0]);
3853 ASSERT_FALSE(flags[1]);
3854}
3855
3856TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003857 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3858 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3859 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3860 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3861 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3862 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003863
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003864 KeyboardInputMapper& mapper =
3865 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3866 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003867 // Initial metastate is AMETA_NONE.
3868 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003869
3870 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003871 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3872 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3873 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003874
3875 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003876 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3877 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003878 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3879 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3880 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003881 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003882
3883 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003884 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3885 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003886 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3887 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3888 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003889 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003890
3891 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003892 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3893 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003894 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3895 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3896 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003897 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003898
3899 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003900 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3901 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003902 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3903 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3904 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003905 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003906
3907 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003908 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3909 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003910 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3911 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3912 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003913 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003914
3915 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003916 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3917 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003918 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3919 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3920 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003921 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003922}
3923
Chris Yea52ade12020-08-27 16:49:20 -07003924TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3925 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3926 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3927 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3928 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3929
3930 KeyboardInputMapper& mapper =
3931 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3932 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3933
Chris Yea52ade12020-08-27 16:49:20 -07003934 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003935 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003936 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3937 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3938 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3939 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3940
3941 NotifyKeyArgs args;
3942 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003943 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3945 ASSERT_EQ(AMETA_NONE, args.metaState);
3946 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3947 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3948 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3949
3950 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003951 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3953 ASSERT_EQ(AMETA_NONE, args.metaState);
3954 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3955 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3956 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3957}
3958
Arthur Hung2c9a3342019-07-23 14:18:59 +08003959TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3960 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003961 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3962 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3963 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3964 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003965
3966 // keyboard 2.
3967 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003968 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003969 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003970 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003971 std::shared_ptr<InputDevice> device2 =
3972 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003973 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003974
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003975 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3976 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3977 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3978 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003979
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003980 KeyboardInputMapper& mapper =
3981 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3982 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003983
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003984 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003985 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003986 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003987 std::list<NotifyArgs> unused =
3988 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3989 0 /*changes*/);
3990 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003991
3992 // Prepared displays and associated info.
3993 constexpr uint8_t hdmi1 = 0;
3994 constexpr uint8_t hdmi2 = 1;
3995 const std::string SECONDARY_UNIQUE_ID = "local:1";
3996
3997 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3998 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3999
4000 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004001 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4002 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004003 ASSERT_FALSE(device2->isEnabled());
4004
4005 // Prepare second display.
4006 constexpr int32_t newDisplayId = 2;
4007 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004008 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004009 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004010 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004011 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004012 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4013 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004014
4015 // Device should be enabled after the associated display is found.
4016 ASSERT_TRUE(mDevice->isEnabled());
4017 ASSERT_TRUE(device2->isEnabled());
4018
4019 // Test pad key events
4020 ASSERT_NO_FATAL_FAILURE(
4021 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
4022 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4023 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4024 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4025 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4026 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4027 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4028
4029 ASSERT_NO_FATAL_FAILURE(
4030 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
4031 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4032 AKEYCODE_DPAD_RIGHT, newDisplayId));
4033 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4034 AKEYCODE_DPAD_DOWN, newDisplayId));
4035 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4036 AKEYCODE_DPAD_LEFT, newDisplayId));
4037}
Michael Wrightd02c5b62014-02-10 15:10:22 -08004038
arthurhungc903df12020-08-11 15:08:42 +08004039TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
4040 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4041 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4042 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4043 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4044 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4045 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4046
4047 KeyboardInputMapper& mapper =
4048 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4049 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004050 // Initial metastate is AMETA_NONE.
4051 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004052
4053 // Initialization should have turned all of the lights off.
4054 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4055 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4056 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4057
4058 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004059 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4060 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004061 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4062 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4063
4064 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004065 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4066 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004067 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4068 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
4069
4070 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004071 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4072 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004073 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4074 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
4075
4076 mFakeEventHub->removeDevice(EVENTHUB_ID);
4077 mReader->loopOnce();
4078
4079 // keyboard 2 should default toggle keys.
4080 const std::string USB2 = "USB2";
4081 const std::string DEVICE_NAME2 = "KEYBOARD2";
4082 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4083 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4084 std::shared_ptr<InputDevice> device2 =
4085 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004086 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08004087 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4088 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4089 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4090 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4091 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4092 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4093
arthurhung6fe95782020-10-05 22:41:16 +08004094 KeyboardInputMapper& mapper2 =
4095 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4096 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004097 std::list<NotifyArgs> unused =
4098 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4099 0 /*changes*/);
4100 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08004101
4102 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4103 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4104 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004105 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4106 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004107}
4108
Arthur Hungcb40a002021-08-03 14:31:01 +00004109TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4110 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4111 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4112 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4113
4114 // Suppose we have two mappers. (DPAD + KEYBOARD)
4115 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4116 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4117 KeyboardInputMapper& mapper =
4118 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4119 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004120 // Initial metastate is AMETA_NONE.
4121 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004122
4123 mReader->toggleCapsLockState(DEVICE_ID);
4124 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4125}
4126
Arthur Hungfb3cc112022-04-13 07:39:50 +00004127TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4128 // keyboard 1.
4129 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4130 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4131 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4132 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4133 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4134 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4135
4136 KeyboardInputMapper& mapper1 =
4137 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4138 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4139
4140 // keyboard 2.
4141 const std::string USB2 = "USB2";
4142 const std::string DEVICE_NAME2 = "KEYBOARD2";
4143 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4144 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4145 std::shared_ptr<InputDevice> device2 =
4146 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4147 ftl::Flags<InputDeviceClass>(0));
4148 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4149 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4150 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4151 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4152 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4153 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4154
4155 KeyboardInputMapper& mapper2 =
4156 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4157 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004158 std::list<NotifyArgs> unused =
4159 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4160 0 /*changes*/);
4161 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004162
Arthur Hung95f68612022-04-07 14:08:22 +08004163 // Initial metastate is AMETA_NONE.
4164 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4165 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4166
4167 // Toggle num lock on and off.
4168 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4169 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004170 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4171 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4172 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4173
4174 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4175 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4176 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4177 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4178 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4179
4180 // Toggle caps lock on and off.
4181 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4182 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4183 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4184 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4185 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4186
4187 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4188 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4189 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4190 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4191 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4192
4193 // Toggle scroll lock on and off.
4194 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4195 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4196 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4197 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4198 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4199
4200 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4201 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4202 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4203 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4204 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4205}
4206
Arthur Hung2141d542022-08-23 07:45:21 +00004207TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4208 const int32_t USAGE_A = 0x070004;
4209 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4210 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4211
4212 KeyboardInputMapper& mapper =
4213 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4214 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4215 // Key down by scan code.
4216 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4217 NotifyKeyArgs args;
4218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4219 ASSERT_EQ(DEVICE_ID, args.deviceId);
4220 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4221 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4222 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4223 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4224 ASSERT_EQ(KEY_HOME, args.scanCode);
4225 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4226
4227 // Disable device, it should synthesize cancellation events for down events.
4228 mFakePolicy->addDisabledDevice(DEVICE_ID);
4229 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
4230
4231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4232 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4233 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4234 ASSERT_EQ(KEY_HOME, args.scanCode);
4235 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4236}
4237
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004238// --- KeyboardInputMapperTest_ExternalDevice ---
4239
4240class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4241protected:
Chris Yea52ade12020-08-27 16:49:20 -07004242 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004243};
4244
4245TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004246 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4247 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004248
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004249 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4250 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4251 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4252 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004253
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004254 KeyboardInputMapper& mapper =
4255 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4256 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004257
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004258 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004259 NotifyKeyArgs args;
4260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4261 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4262
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004263 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4265 ASSERT_EQ(uint32_t(0), args.policyFlags);
4266
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004267 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4269 ASSERT_EQ(uint32_t(0), args.policyFlags);
4270
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004271 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4273 ASSERT_EQ(uint32_t(0), args.policyFlags);
4274
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004275 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4277 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4278
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004279 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4281 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4282}
4283
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004284TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004285 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004286
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004287 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4288 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4289 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004290
Powei Fengd041c5d2019-05-03 17:11:33 -07004291 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004292 KeyboardInputMapper& mapper =
4293 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4294 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004295
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004296 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004297 NotifyKeyArgs args;
4298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4299 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4300
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004301 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4303 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4304
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004305 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4307 ASSERT_EQ(uint32_t(0), args.policyFlags);
4308
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004309 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4311 ASSERT_EQ(uint32_t(0), args.policyFlags);
4312
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004313 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4315 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4316
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004317 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4319 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4320}
4321
Michael Wrightd02c5b62014-02-10 15:10:22 -08004322// --- CursorInputMapperTest ---
4323
4324class CursorInputMapperTest : public InputMapperTest {
4325protected:
4326 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4327
Michael Wright17db18e2020-06-26 20:51:44 +01004328 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004329
Chris Yea52ade12020-08-27 16:49:20 -07004330 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004331 InputMapperTest::SetUp();
4332
Michael Wright17db18e2020-06-26 20:51:44 +01004333 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004334 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004335 }
4336
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004337 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4338 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004339
4340 void prepareDisplay(int32_t orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004341 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4342 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4343 }
4344
4345 void prepareSecondaryDisplay() {
4346 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4347 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
4348 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004349 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004350
4351 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4352 float pressure) {
4353 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4354 0.0f, 0.0f, 0.0f, EPSILON));
4355 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004356};
4357
4358const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4359
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004360void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4361 int32_t originalY, int32_t rotatedX,
4362 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004363 NotifyMotionArgs args;
4364
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004365 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4366 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4367 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4369 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004370 ASSERT_NO_FATAL_FAILURE(
4371 assertCursorPointerCoords(args.pointerCoords[0],
4372 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4373 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004374}
4375
4376TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004377 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004378 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004380 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004381}
4382
4383TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004384 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004385 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004386
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004387 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004388}
4389
4390TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004391 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004392 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004393
4394 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004395 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004396
4397 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004398 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4399 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004400 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4401 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4402
4403 // When the bounds are set, then there should be a valid motion range.
4404 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4405
4406 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004407 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004408
4409 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4410 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4411 1, 800 - 1, 0.0f, 0.0f));
4412 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4413 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4414 2, 480 - 1, 0.0f, 0.0f));
4415 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4416 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4417 0.0f, 1.0f, 0.0f, 0.0f));
4418}
4419
4420TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004421 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004422 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004423
4424 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004425 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004426
4427 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4428 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4429 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4430 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4431 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4432 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4433 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4434 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4435 0.0f, 1.0f, 0.0f, 0.0f));
4436}
4437
4438TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004439 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004440 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004441
arthurhungdcef2dc2020-08-11 14:47:50 +08004442 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004443
4444 NotifyMotionArgs args;
4445
4446 // Button press.
4447 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004448 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4451 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4452 ASSERT_EQ(DEVICE_ID, args.deviceId);
4453 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4454 ASSERT_EQ(uint32_t(0), args.policyFlags);
4455 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4456 ASSERT_EQ(0, args.flags);
4457 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4458 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4459 ASSERT_EQ(0, args.edgeFlags);
4460 ASSERT_EQ(uint32_t(1), args.pointerCount);
4461 ASSERT_EQ(0, args.pointerProperties[0].id);
4462 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004463 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4465 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4466 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4467
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4469 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4470 ASSERT_EQ(DEVICE_ID, args.deviceId);
4471 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4472 ASSERT_EQ(uint32_t(0), args.policyFlags);
4473 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4474 ASSERT_EQ(0, args.flags);
4475 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4476 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4477 ASSERT_EQ(0, args.edgeFlags);
4478 ASSERT_EQ(uint32_t(1), args.pointerCount);
4479 ASSERT_EQ(0, args.pointerProperties[0].id);
4480 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004481 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004482 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4483 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4484 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4485
Michael Wrightd02c5b62014-02-10 15:10:22 -08004486 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004487 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4488 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4490 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4491 ASSERT_EQ(DEVICE_ID, args.deviceId);
4492 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4493 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004494 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4495 ASSERT_EQ(0, args.flags);
4496 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4497 ASSERT_EQ(0, args.buttonState);
4498 ASSERT_EQ(0, args.edgeFlags);
4499 ASSERT_EQ(uint32_t(1), args.pointerCount);
4500 ASSERT_EQ(0, args.pointerProperties[0].id);
4501 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004502 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004503 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4504 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4505 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4506
4507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4508 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4509 ASSERT_EQ(DEVICE_ID, args.deviceId);
4510 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4511 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004512 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4513 ASSERT_EQ(0, args.flags);
4514 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4515 ASSERT_EQ(0, args.buttonState);
4516 ASSERT_EQ(0, args.edgeFlags);
4517 ASSERT_EQ(uint32_t(1), args.pointerCount);
4518 ASSERT_EQ(0, args.pointerProperties[0].id);
4519 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004520 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004521 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4522 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4523 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4524}
4525
4526TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004527 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004528 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004529
4530 NotifyMotionArgs args;
4531
4532 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004533 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4534 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4536 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004537 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4538 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4539 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004540
4541 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004542 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4543 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4545 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004546 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4547 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004548}
4549
4550TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004551 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004552 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004553
4554 NotifyMotionArgs args;
4555
4556 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004557 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4558 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4560 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004561 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004562
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4564 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004565 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004566
Michael Wrightd02c5b62014-02-10 15:10:22 -08004567 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004568 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4569 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004571 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004572 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004573
4574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004575 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004576 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004577}
4578
4579TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004580 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004581 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004582
4583 NotifyMotionArgs args;
4584
4585 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004586 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4587 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4588 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4589 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4591 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004592 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4593 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4594 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004595
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4597 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004598 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4599 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4600 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004601
Michael Wrightd02c5b62014-02-10 15:10:22 -08004602 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004603 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4604 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4605 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4607 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004608 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4609 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4610 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004611
4612 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004613 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004616 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004617 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004618
4619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004620 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004621 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004622}
4623
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004624TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004625 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004626 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004627 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4628 // need to be rotated.
4629 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004630 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004631
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004632 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004633 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4634 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4635 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4636 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4637 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4638 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4639 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4640 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4641}
4642
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004643TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004644 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004645 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004646 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4647 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004648 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004649
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004650 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004651 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004652 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4653 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4654 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4655 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4656 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4657 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4658 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4659 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4660
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004661 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004662 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004663 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4664 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4665 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4666 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4667 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4668 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4669 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4670 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004671
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004672 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004673 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004674 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4675 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4676 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4677 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4678 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4679 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4680 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4681 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4682
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004683 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004684 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004685 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4686 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4687 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4688 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4689 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4690 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4691 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4692 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004693}
4694
4695TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004696 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004697 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004698
4699 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4700 mFakePointerController->setPosition(100, 200);
4701 mFakePointerController->setButtonState(0);
4702
4703 NotifyMotionArgs motionArgs;
4704 NotifyKeyArgs keyArgs;
4705
4706 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004707 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4708 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4710 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4711 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4712 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004713 ASSERT_NO_FATAL_FAILURE(
4714 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004715
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4717 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4718 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4719 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004720 ASSERT_NO_FATAL_FAILURE(
4721 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004722
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004723 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4724 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004726 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004727 ASSERT_EQ(0, motionArgs.buttonState);
4728 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004729 ASSERT_NO_FATAL_FAILURE(
4730 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004731
4732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004733 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004734 ASSERT_EQ(0, motionArgs.buttonState);
4735 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004736 ASSERT_NO_FATAL_FAILURE(
4737 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004738
4739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004740 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004741 ASSERT_EQ(0, motionArgs.buttonState);
4742 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004743 ASSERT_NO_FATAL_FAILURE(
4744 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004745
4746 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004747 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4748 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4749 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4751 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4752 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4753 motionArgs.buttonState);
4754 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4755 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004756 ASSERT_NO_FATAL_FAILURE(
4757 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004758
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4760 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4761 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4762 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4763 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004764 ASSERT_NO_FATAL_FAILURE(
4765 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004766
4767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4768 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4769 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4770 motionArgs.buttonState);
4771 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4772 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004773 ASSERT_NO_FATAL_FAILURE(
4774 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004775
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004776 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4777 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004779 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004780 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4781 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004782 ASSERT_NO_FATAL_FAILURE(
4783 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004784
4785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004786 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004787 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4788 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004789 ASSERT_NO_FATAL_FAILURE(
4790 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004791
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004792 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4793 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004795 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4796 ASSERT_EQ(0, motionArgs.buttonState);
4797 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004798 ASSERT_NO_FATAL_FAILURE(
4799 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004800 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004802
4803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004804 ASSERT_EQ(0, motionArgs.buttonState);
4805 ASSERT_EQ(0, mFakePointerController->getButtonState());
4806 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004807 ASSERT_NO_FATAL_FAILURE(
4808 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004809
Michael Wrightd02c5b62014-02-10 15:10:22 -08004810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4811 ASSERT_EQ(0, motionArgs.buttonState);
4812 ASSERT_EQ(0, mFakePointerController->getButtonState());
4813 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004814 ASSERT_NO_FATAL_FAILURE(
4815 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004816
4817 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004818 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4819 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4821 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4822 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004823
Michael Wrightd02c5b62014-02-10 15:10:22 -08004824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004825 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004826 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4827 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, 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));
4832 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4833 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4834 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, 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
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004838 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4839 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004841 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004842 ASSERT_EQ(0, motionArgs.buttonState);
4843 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004844 ASSERT_NO_FATAL_FAILURE(
4845 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004846
4847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004848 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004849 ASSERT_EQ(0, motionArgs.buttonState);
4850 ASSERT_EQ(0, mFakePointerController->getButtonState());
4851
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004852 ASSERT_NO_FATAL_FAILURE(
4853 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4855 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4856 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4857
4858 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004859 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4862 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4863 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004864
Michael Wrightd02c5b62014-02-10 15:10:22 -08004865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004866 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004867 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4868 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004869 ASSERT_NO_FATAL_FAILURE(
4870 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004871
4872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4873 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4874 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4875 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004876 ASSERT_NO_FATAL_FAILURE(
4877 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004878
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004879 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4880 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004882 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004883 ASSERT_EQ(0, motionArgs.buttonState);
4884 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004885 ASSERT_NO_FATAL_FAILURE(
4886 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004887
4888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4889 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4890 ASSERT_EQ(0, motionArgs.buttonState);
4891 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004892 ASSERT_NO_FATAL_FAILURE(
4893 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004894
Michael Wrightd02c5b62014-02-10 15:10:22 -08004895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4896 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4897 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4898
4899 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004900 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4901 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4903 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4904 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004905
Michael Wrightd02c5b62014-02-10 15:10:22 -08004906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004907 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004908 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4909 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004910 ASSERT_NO_FATAL_FAILURE(
4911 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004912
4913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4914 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4915 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4916 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004917 ASSERT_NO_FATAL_FAILURE(
4918 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004919
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004920 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4921 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004923 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004924 ASSERT_EQ(0, motionArgs.buttonState);
4925 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004926 ASSERT_NO_FATAL_FAILURE(
4927 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004928
4929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4930 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4931 ASSERT_EQ(0, motionArgs.buttonState);
4932 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004933 ASSERT_NO_FATAL_FAILURE(
4934 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004935
Michael Wrightd02c5b62014-02-10 15:10:22 -08004936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4937 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4938 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4939
4940 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004941 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4942 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4944 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4945 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004946
Michael Wrightd02c5b62014-02-10 15:10:22 -08004947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004948 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004949 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4950 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004951 ASSERT_NO_FATAL_FAILURE(
4952 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004953
4954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4955 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4956 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4957 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004958 ASSERT_NO_FATAL_FAILURE(
4959 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004960
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004961 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4962 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004964 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004965 ASSERT_EQ(0, motionArgs.buttonState);
4966 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004967 ASSERT_NO_FATAL_FAILURE(
4968 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004969
4970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4971 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4972 ASSERT_EQ(0, motionArgs.buttonState);
4973 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004974 ASSERT_NO_FATAL_FAILURE(
4975 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004976
Michael Wrightd02c5b62014-02-10 15:10:22 -08004977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4978 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4979 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4980}
4981
4982TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004983 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004984 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004985
4986 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4987 mFakePointerController->setPosition(100, 200);
4988 mFakePointerController->setButtonState(0);
4989
4990 NotifyMotionArgs args;
4991
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004992 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4994 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004996 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4997 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4998 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4999 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 +01005000 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005001}
5002
5003TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005004 addConfigurationProperty("cursor.mode", "pointer");
5005 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005006 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005007
5008 NotifyDeviceResetArgs resetArgs;
5009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5010 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5011 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5012
5013 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5014 mFakePointerController->setPosition(100, 200);
5015 mFakePointerController->setButtonState(0);
5016
5017 NotifyMotionArgs args;
5018
5019 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005020 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5021 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5022 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5024 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5025 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5026 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5027 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 +01005028 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005029
5030 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005031 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5032 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5034 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5035 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5036 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5037 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5039 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5040 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
5041 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5042 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5043
5044 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005045 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5046 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5048 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5049 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
5050 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5051 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5053 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5054 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
5055 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5056 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5057
5058 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005059 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
5060 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
5061 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5063 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5064 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5065 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5066 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 +01005067 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005068
5069 // Disable pointer capture and check that the device generation got bumped
5070 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08005071 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005072 mFakePolicy->setPointerCapture(false);
5073 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08005074 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005075
5076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005077 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5078
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005079 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5080 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5081 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5083 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005084 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5085 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5086 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 +01005087 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005088}
5089
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00005090/**
5091 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
5092 * pointer acceleration or speed processing should not be applied.
5093 */
5094TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
5095 addConfigurationProperty("cursor.mode", "pointer");
5096 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
5097 100.f /*high threshold*/, 10.f /*acceleration*/);
5098 mFakePolicy->setVelocityControlParams(testParams);
5099 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5100
5101 NotifyDeviceResetArgs resetArgs;
5102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5103 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5104 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5105
5106 NotifyMotionArgs args;
5107
5108 // Move and verify scale is applied.
5109 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5110 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5111 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5113 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5114 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5115 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5116 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5117 ASSERT_GT(relX, 10);
5118 ASSERT_GT(relY, 20);
5119
5120 // Enable Pointer Capture
5121 mFakePolicy->setPointerCapture(true);
5122 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5123 NotifyPointerCaptureChangedArgs captureArgs;
5124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5125 ASSERT_TRUE(captureArgs.request.enable);
5126
5127 // Move and verify scale is not applied.
5128 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5129 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5130 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5132 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5134 ASSERT_EQ(10, args.pointerCoords[0].getX());
5135 ASSERT_EQ(20, args.pointerCoords[0].getY());
5136}
5137
Prabir Pradhan208360b2022-06-24 18:37:04 +00005138TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5139 addConfigurationProperty("cursor.mode", "pointer");
5140 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5141
5142 NotifyDeviceResetArgs resetArgs;
5143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5144 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5145 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5146
5147 // Ensure the display is rotated.
5148 prepareDisplay(DISPLAY_ORIENTATION_90);
5149
5150 NotifyMotionArgs args;
5151
5152 // Verify that the coordinates are rotated.
5153 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5154 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5155 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5157 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5158 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5159 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5160 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5161
5162 // Enable Pointer Capture.
5163 mFakePolicy->setPointerCapture(true);
5164 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5165 NotifyPointerCaptureChangedArgs captureArgs;
5166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5167 ASSERT_TRUE(captureArgs.request.enable);
5168
5169 // Move and verify rotation is not applied.
5170 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5171 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5172 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5174 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5175 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5176 ASSERT_EQ(10, args.pointerCoords[0].getX());
5177 ASSERT_EQ(20, args.pointerCoords[0].getY());
5178}
5179
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005180TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005181 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005182
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005183 // Set up the default display.
5184 prepareDisplay(DISPLAY_ORIENTATION_90);
5185
5186 // Set up the secondary display as the display on which the pointer should be shown.
5187 // The InputDevice is not associated with any display.
5188 prepareSecondaryDisplay();
5189 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08005190 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5191
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005192 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005193 mFakePointerController->setPosition(100, 200);
5194 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005195
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005196 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5198 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5199 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005201 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5202 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5203 WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01005204 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005205}
5206
5207TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
5208 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5209
5210 // Set up the default display.
5211 prepareDisplay(DISPLAY_ORIENTATION_90);
5212
5213 // Set up the secondary display as the display on which the pointer should be shown,
5214 // and associate the InputDevice with the secondary display.
5215 prepareSecondaryDisplay();
5216 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5217 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5218 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5219
5220 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5221 mFakePointerController->setPosition(100, 200);
5222 mFakePointerController->setButtonState(0);
5223
5224 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5225 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5226 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005228 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5229 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5230 WithCoords(110.0f, 220.0f))));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005231 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
5232}
5233
5234TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
5235 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5236
5237 // Set up the default display as the display on which the pointer should be shown.
5238 prepareDisplay(DISPLAY_ORIENTATION_90);
5239 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5240
5241 // Associate the InputDevice with the secondary display.
5242 prepareSecondaryDisplay();
5243 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5244 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5245
5246 // The mapper should not generate any events because it is associated with a display that is
5247 // different from the pointer display.
5248 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5249 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5250 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005252}
5253
Michael Wrightd02c5b62014-02-10 15:10:22 -08005254// --- TouchInputMapperTest ---
5255
5256class TouchInputMapperTest : public InputMapperTest {
5257protected:
5258 static const int32_t RAW_X_MIN;
5259 static const int32_t RAW_X_MAX;
5260 static const int32_t RAW_Y_MIN;
5261 static const int32_t RAW_Y_MAX;
5262 static const int32_t RAW_TOUCH_MIN;
5263 static const int32_t RAW_TOUCH_MAX;
5264 static const int32_t RAW_TOOL_MIN;
5265 static const int32_t RAW_TOOL_MAX;
5266 static const int32_t RAW_PRESSURE_MIN;
5267 static const int32_t RAW_PRESSURE_MAX;
5268 static const int32_t RAW_ORIENTATION_MIN;
5269 static const int32_t RAW_ORIENTATION_MAX;
5270 static const int32_t RAW_DISTANCE_MIN;
5271 static const int32_t RAW_DISTANCE_MAX;
5272 static const int32_t RAW_TILT_MIN;
5273 static const int32_t RAW_TILT_MAX;
5274 static const int32_t RAW_ID_MIN;
5275 static const int32_t RAW_ID_MAX;
5276 static const int32_t RAW_SLOT_MIN;
5277 static const int32_t RAW_SLOT_MAX;
5278 static const float X_PRECISION;
5279 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005280 static const float X_PRECISION_VIRTUAL;
5281 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005282
5283 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005284 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005285
5286 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5287
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005288 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005289 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005290
Michael Wrightd02c5b62014-02-10 15:10:22 -08005291 enum Axes {
5292 POSITION = 1 << 0,
5293 TOUCH = 1 << 1,
5294 TOOL = 1 << 2,
5295 PRESSURE = 1 << 3,
5296 ORIENTATION = 1 << 4,
5297 MINOR = 1 << 5,
5298 ID = 1 << 6,
5299 DISTANCE = 1 << 7,
5300 TILT = 1 << 8,
5301 SLOT = 1 << 9,
5302 TOOL_TYPE = 1 << 10,
5303 };
5304
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005305 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5306 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005307 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005308 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005309 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005310 int32_t toRawX(float displayX);
5311 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005312 int32_t toRotatedRawX(float displayX);
5313 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005314 float toCookedX(float rawX, float rawY);
5315 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005316 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005317 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005318 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005319 float toDisplayY(int32_t rawY, int32_t displayHeight);
5320
Michael Wrightd02c5b62014-02-10 15:10:22 -08005321};
5322
5323const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5324const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5325const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5326const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5327const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5328const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5329const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5330const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005331const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5332const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005333const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5334const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5335const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5336const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5337const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5338const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5339const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5340const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5341const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5342const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5343const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5344const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005345const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5346 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5347const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5348 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005349const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5350 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005351
5352const float TouchInputMapperTest::GEOMETRIC_SCALE =
5353 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5354 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5355
5356const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5357 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5358 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5359};
5360
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005361void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005362 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5363 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005364}
5365
5366void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5367 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5368 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005369}
5370
Santos Cordonfa5cf462017-04-05 10:37:00 -07005371void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005372 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5373 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5374 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005375}
5376
Michael Wrightd02c5b62014-02-10 15:10:22 -08005377void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005378 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5379 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5380 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5381 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005382}
5383
Jason Gerecke489fda82012-09-07 17:19:40 -07005384void TouchInputMapperTest::prepareLocationCalibration() {
5385 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5386}
5387
Michael Wrightd02c5b62014-02-10 15:10:22 -08005388int32_t TouchInputMapperTest::toRawX(float displayX) {
5389 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5390}
5391
5392int32_t TouchInputMapperTest::toRawY(float displayY) {
5393 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5394}
5395
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005396int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5397 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5398}
5399
5400int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5401 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5402}
5403
Jason Gerecke489fda82012-09-07 17:19:40 -07005404float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5405 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5406 return rawX;
5407}
5408
5409float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5410 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5411 return rawY;
5412}
5413
Michael Wrightd02c5b62014-02-10 15:10:22 -08005414float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005415 return toDisplayX(rawX, DISPLAY_WIDTH);
5416}
5417
5418float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5419 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005420}
5421
5422float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005423 return toDisplayY(rawY, DISPLAY_HEIGHT);
5424}
5425
5426float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5427 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005428}
5429
5430
5431// --- SingleTouchInputMapperTest ---
5432
5433class SingleTouchInputMapperTest : public TouchInputMapperTest {
5434protected:
5435 void prepareButtons();
5436 void prepareAxes(int axes);
5437
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005438 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5439 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5440 void processUp(SingleTouchInputMapper& mappery);
5441 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5442 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5443 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5444 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5445 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5446 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005447};
5448
5449void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005450 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005451}
5452
5453void SingleTouchInputMapperTest::prepareAxes(int axes) {
5454 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005455 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5456 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005457 }
5458 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005459 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5460 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005461 }
5462 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005463 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5464 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005465 }
5466 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005467 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5468 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005469 }
5470 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005471 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5472 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005473 }
5474}
5475
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005476void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005477 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5478 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5479 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005480}
5481
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005482void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005483 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5484 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005485}
5486
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005487void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005488 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005489}
5490
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005491void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005492 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005493}
5494
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005495void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5496 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005497 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005498}
5499
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005500void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005501 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005502}
5503
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005504void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5505 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005506 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5507 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005508}
5509
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005510void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5511 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005512 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005513}
5514
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005515void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005516 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005517}
5518
Michael Wrightd02c5b62014-02-10 15:10:22 -08005519TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005520 prepareButtons();
5521 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005522 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005523
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005524 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005525}
5526
Michael Wrightd02c5b62014-02-10 15:10:22 -08005527TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005528 prepareButtons();
5529 prepareAxes(POSITION);
5530 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005531 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005532
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005533 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005534}
5535
5536TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005537 addConfigurationProperty("touch.deviceType", "touchScreen");
5538 prepareDisplay(DISPLAY_ORIENTATION_0);
5539 prepareButtons();
5540 prepareAxes(POSITION);
5541 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005542 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005543
5544 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005545 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005546
5547 // Virtual key is down.
5548 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5549 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5550 processDown(mapper, x, y);
5551 processSync(mapper);
5552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5553
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005554 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005555
5556 // Virtual key is up.
5557 processUp(mapper);
5558 processSync(mapper);
5559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5560
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005561 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005562}
5563
5564TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005565 addConfigurationProperty("touch.deviceType", "touchScreen");
5566 prepareDisplay(DISPLAY_ORIENTATION_0);
5567 prepareButtons();
5568 prepareAxes(POSITION);
5569 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005570 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005571
5572 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005573 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005574
5575 // Virtual key is down.
5576 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5577 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5578 processDown(mapper, x, y);
5579 processSync(mapper);
5580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5581
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005582 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005583
5584 // Virtual key is up.
5585 processUp(mapper);
5586 processSync(mapper);
5587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5588
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005589 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005590}
5591
5592TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005593 addConfigurationProperty("touch.deviceType", "touchScreen");
5594 prepareDisplay(DISPLAY_ORIENTATION_0);
5595 prepareButtons();
5596 prepareAxes(POSITION);
5597 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005598 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005599
Michael Wrightd02c5b62014-02-10 15:10:22 -08005600 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005601 ASSERT_TRUE(
5602 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005603 ASSERT_TRUE(flags[0]);
5604 ASSERT_FALSE(flags[1]);
5605}
5606
5607TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005608 addConfigurationProperty("touch.deviceType", "touchScreen");
5609 prepareDisplay(DISPLAY_ORIENTATION_0);
5610 prepareButtons();
5611 prepareAxes(POSITION);
5612 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005613 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005614
arthurhungdcef2dc2020-08-11 14:47:50 +08005615 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005616
5617 NotifyKeyArgs args;
5618
5619 // Press virtual key.
5620 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5621 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5622 processDown(mapper, x, y);
5623 processSync(mapper);
5624
5625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5626 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5627 ASSERT_EQ(DEVICE_ID, args.deviceId);
5628 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5629 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5630 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5631 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5632 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5633 ASSERT_EQ(KEY_HOME, args.scanCode);
5634 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5635 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5636
5637 // Release virtual key.
5638 processUp(mapper);
5639 processSync(mapper);
5640
5641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5642 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5643 ASSERT_EQ(DEVICE_ID, args.deviceId);
5644 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5645 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5646 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5647 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5648 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5649 ASSERT_EQ(KEY_HOME, args.scanCode);
5650 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5651 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5652
5653 // Should not have sent any motions.
5654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5655}
5656
5657TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005658 addConfigurationProperty("touch.deviceType", "touchScreen");
5659 prepareDisplay(DISPLAY_ORIENTATION_0);
5660 prepareButtons();
5661 prepareAxes(POSITION);
5662 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005663 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005664
arthurhungdcef2dc2020-08-11 14:47:50 +08005665 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005666
5667 NotifyKeyArgs keyArgs;
5668
5669 // Press virtual key.
5670 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5671 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5672 processDown(mapper, x, y);
5673 processSync(mapper);
5674
5675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5676 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5677 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5678 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5679 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5680 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5681 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5682 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5683 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5684 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5685 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5686
5687 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5688 // into the display area.
5689 y -= 100;
5690 processMove(mapper, x, y);
5691 processSync(mapper);
5692
5693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5694 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5695 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5696 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5697 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5698 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5699 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5700 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5701 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5702 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5703 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5704 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5705
5706 NotifyMotionArgs motionArgs;
5707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5708 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5709 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5710 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5711 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5712 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5713 ASSERT_EQ(0, motionArgs.flags);
5714 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5715 ASSERT_EQ(0, motionArgs.buttonState);
5716 ASSERT_EQ(0, motionArgs.edgeFlags);
5717 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5718 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5719 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5720 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5721 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5722 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5723 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5724 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5725
5726 // Keep moving out of bounds. Should generate a pointer move.
5727 y -= 50;
5728 processMove(mapper, x, y);
5729 processSync(mapper);
5730
5731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5732 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5733 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5734 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5735 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5736 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5737 ASSERT_EQ(0, motionArgs.flags);
5738 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5739 ASSERT_EQ(0, motionArgs.buttonState);
5740 ASSERT_EQ(0, motionArgs.edgeFlags);
5741 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5742 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5743 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5744 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5745 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5746 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5747 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5748 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5749
5750 // Release out of bounds. Should generate a pointer up.
5751 processUp(mapper);
5752 processSync(mapper);
5753
5754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5755 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5756 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5757 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5758 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5759 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5760 ASSERT_EQ(0, motionArgs.flags);
5761 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5762 ASSERT_EQ(0, motionArgs.buttonState);
5763 ASSERT_EQ(0, motionArgs.edgeFlags);
5764 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5765 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5766 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5768 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5769 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5770 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5771 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5772
5773 // Should not have sent any more keys or motions.
5774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5776}
5777
5778TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005779 addConfigurationProperty("touch.deviceType", "touchScreen");
5780 prepareDisplay(DISPLAY_ORIENTATION_0);
5781 prepareButtons();
5782 prepareAxes(POSITION);
5783 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005784 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005785
arthurhungdcef2dc2020-08-11 14:47:50 +08005786 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005787
5788 NotifyMotionArgs motionArgs;
5789
5790 // Initially go down out of bounds.
5791 int32_t x = -10;
5792 int32_t y = -10;
5793 processDown(mapper, x, y);
5794 processSync(mapper);
5795
5796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5797
5798 // Move into the display area. Should generate a pointer down.
5799 x = 50;
5800 y = 75;
5801 processMove(mapper, x, y);
5802 processSync(mapper);
5803
5804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5805 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5806 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5807 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5808 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5809 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5810 ASSERT_EQ(0, motionArgs.flags);
5811 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5812 ASSERT_EQ(0, motionArgs.buttonState);
5813 ASSERT_EQ(0, motionArgs.edgeFlags);
5814 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5815 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5816 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5817 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5818 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5819 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5820 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5821 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5822
5823 // Release. Should generate a pointer up.
5824 processUp(mapper);
5825 processSync(mapper);
5826
5827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5828 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5829 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5830 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5831 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5832 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5833 ASSERT_EQ(0, motionArgs.flags);
5834 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5835 ASSERT_EQ(0, motionArgs.buttonState);
5836 ASSERT_EQ(0, motionArgs.edgeFlags);
5837 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5838 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5839 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5840 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5841 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5842 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5843 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5844 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5845
5846 // Should not have sent any more keys or motions.
5847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5849}
5850
Santos Cordonfa5cf462017-04-05 10:37:00 -07005851TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005852 addConfigurationProperty("touch.deviceType", "touchScreen");
5853 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5854
5855 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5856 prepareButtons();
5857 prepareAxes(POSITION);
5858 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005859 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005860
arthurhungdcef2dc2020-08-11 14:47:50 +08005861 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005862
5863 NotifyMotionArgs motionArgs;
5864
5865 // Down.
5866 int32_t x = 100;
5867 int32_t y = 125;
5868 processDown(mapper, x, y);
5869 processSync(mapper);
5870
5871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5872 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5873 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5874 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5875 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5876 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5877 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5878 ASSERT_EQ(0, motionArgs.flags);
5879 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5880 ASSERT_EQ(0, motionArgs.buttonState);
5881 ASSERT_EQ(0, motionArgs.edgeFlags);
5882 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5883 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5884 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5886 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5887 1, 0, 0, 0, 0, 0, 0, 0));
5888 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5889 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5890 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5891
5892 // Move.
5893 x += 50;
5894 y += 75;
5895 processMove(mapper, x, y);
5896 processSync(mapper);
5897
5898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5899 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5900 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5901 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5902 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5903 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5904 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5905 ASSERT_EQ(0, motionArgs.flags);
5906 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5907 ASSERT_EQ(0, motionArgs.buttonState);
5908 ASSERT_EQ(0, motionArgs.edgeFlags);
5909 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5910 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5911 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5912 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5913 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5914 1, 0, 0, 0, 0, 0, 0, 0));
5915 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5916 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5917 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5918
5919 // Up.
5920 processUp(mapper);
5921 processSync(mapper);
5922
5923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5924 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5925 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5926 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5927 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5928 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5929 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5930 ASSERT_EQ(0, motionArgs.flags);
5931 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5932 ASSERT_EQ(0, motionArgs.buttonState);
5933 ASSERT_EQ(0, motionArgs.edgeFlags);
5934 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5935 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5936 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5937 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5938 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5939 1, 0, 0, 0, 0, 0, 0, 0));
5940 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5941 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5942 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5943
5944 // Should not have sent any more keys or motions.
5945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5947}
5948
Michael Wrightd02c5b62014-02-10 15:10:22 -08005949TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005950 addConfigurationProperty("touch.deviceType", "touchScreen");
5951 prepareDisplay(DISPLAY_ORIENTATION_0);
5952 prepareButtons();
5953 prepareAxes(POSITION);
5954 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005955 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005956
arthurhungdcef2dc2020-08-11 14:47:50 +08005957 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005958
5959 NotifyMotionArgs motionArgs;
5960
5961 // Down.
5962 int32_t x = 100;
5963 int32_t y = 125;
5964 processDown(mapper, x, y);
5965 processSync(mapper);
5966
5967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5968 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5969 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5970 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5971 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5972 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5973 ASSERT_EQ(0, motionArgs.flags);
5974 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5975 ASSERT_EQ(0, motionArgs.buttonState);
5976 ASSERT_EQ(0, motionArgs.edgeFlags);
5977 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5978 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5979 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5980 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5981 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5982 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5983 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5984 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5985
5986 // Move.
5987 x += 50;
5988 y += 75;
5989 processMove(mapper, x, y);
5990 processSync(mapper);
5991
5992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5993 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5994 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5995 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5996 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5997 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5998 ASSERT_EQ(0, motionArgs.flags);
5999 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6000 ASSERT_EQ(0, motionArgs.buttonState);
6001 ASSERT_EQ(0, motionArgs.edgeFlags);
6002 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6003 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6004 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6005 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6006 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6007 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6008 ASSERT_NEAR(Y_PRECISION, 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(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6019 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6020 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6021 ASSERT_EQ(0, motionArgs.flags);
6022 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6023 ASSERT_EQ(0, motionArgs.buttonState);
6024 ASSERT_EQ(0, motionArgs.edgeFlags);
6025 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6026 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6027 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6028 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6029 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6030 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6031 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6032 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6033
6034 // Should not have sent any more keys or motions.
6035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6037}
6038
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006039TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006040 addConfigurationProperty("touch.deviceType", "touchScreen");
6041 prepareButtons();
6042 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006043 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
6044 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006045 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006046
6047 NotifyMotionArgs args;
6048
6049 // Rotation 90.
6050 prepareDisplay(DISPLAY_ORIENTATION_90);
6051 processDown(mapper, toRawX(50), toRawY(75));
6052 processSync(mapper);
6053
6054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6055 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6056 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6057
6058 processUp(mapper);
6059 processSync(mapper);
6060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6061}
6062
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006063TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006064 addConfigurationProperty("touch.deviceType", "touchScreen");
6065 prepareButtons();
6066 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006067 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6068 // orientation-aware are affected by display rotation.
6069 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006070 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006071
6072 NotifyMotionArgs args;
6073
6074 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006075 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006076 prepareDisplay(DISPLAY_ORIENTATION_0);
6077 processDown(mapper, toRawX(50), toRawY(75));
6078 processSync(mapper);
6079
6080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6081 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6082 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6083
6084 processUp(mapper);
6085 processSync(mapper);
6086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6087
6088 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006089 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006090 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006091 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006092 processSync(mapper);
6093
6094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6095 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6096 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6097
6098 processUp(mapper);
6099 processSync(mapper);
6100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6101
6102 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006103 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006104 prepareDisplay(DISPLAY_ORIENTATION_180);
6105 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6106 processSync(mapper);
6107
6108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6109 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6110 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6111
6112 processUp(mapper);
6113 processSync(mapper);
6114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6115
6116 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006117 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006118 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006119 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006120 processSync(mapper);
6121
6122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6123 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6124 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6125
6126 processUp(mapper);
6127 processSync(mapper);
6128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6129}
6130
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006131TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6132 addConfigurationProperty("touch.deviceType", "touchScreen");
6133 prepareButtons();
6134 prepareAxes(POSITION);
6135 addConfigurationProperty("touch.orientationAware", "1");
6136 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6137 clearViewports();
6138 prepareDisplay(DISPLAY_ORIENTATION_0);
6139 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6140 NotifyMotionArgs args;
6141
6142 // Orientation 0.
6143 processDown(mapper, toRawX(50), toRawY(75));
6144 processSync(mapper);
6145
6146 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6147 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6148 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6149
6150 processUp(mapper);
6151 processSync(mapper);
6152 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6153}
6154
6155TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6156 addConfigurationProperty("touch.deviceType", "touchScreen");
6157 prepareButtons();
6158 prepareAxes(POSITION);
6159 addConfigurationProperty("touch.orientationAware", "1");
6160 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6161 clearViewports();
6162 prepareDisplay(DISPLAY_ORIENTATION_0);
6163 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6164 NotifyMotionArgs args;
6165
6166 // Orientation 90.
6167 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6168 processSync(mapper);
6169
6170 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6171 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6172 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6173
6174 processUp(mapper);
6175 processSync(mapper);
6176 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6177}
6178
6179TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6180 addConfigurationProperty("touch.deviceType", "touchScreen");
6181 prepareButtons();
6182 prepareAxes(POSITION);
6183 addConfigurationProperty("touch.orientationAware", "1");
6184 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6185 clearViewports();
6186 prepareDisplay(DISPLAY_ORIENTATION_0);
6187 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6188 NotifyMotionArgs args;
6189
6190 // Orientation 180.
6191 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6192 processSync(mapper);
6193
6194 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6195 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6196 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6197
6198 processUp(mapper);
6199 processSync(mapper);
6200 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6201}
6202
6203TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6204 addConfigurationProperty("touch.deviceType", "touchScreen");
6205 prepareButtons();
6206 prepareAxes(POSITION);
6207 addConfigurationProperty("touch.orientationAware", "1");
6208 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6209 clearViewports();
6210 prepareDisplay(DISPLAY_ORIENTATION_0);
6211 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6212 NotifyMotionArgs args;
6213
6214 // Orientation 270.
6215 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6216 processSync(mapper);
6217
6218 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6219 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6220 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6221
6222 processUp(mapper);
6223 processSync(mapper);
6224 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6225}
6226
6227TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6228 addConfigurationProperty("touch.deviceType", "touchScreen");
6229 prepareButtons();
6230 prepareAxes(POSITION);
6231 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6232 // orientation-aware are affected by display rotation.
6233 addConfigurationProperty("touch.orientationAware", "0");
6234 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6235 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6236
6237 NotifyMotionArgs args;
6238
6239 // Orientation 90, Rotation 0.
6240 clearViewports();
6241 prepareDisplay(DISPLAY_ORIENTATION_0);
6242 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6243 processSync(mapper);
6244
6245 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6246 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6247 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6248
6249 processUp(mapper);
6250 processSync(mapper);
6251 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6252
6253 // Orientation 90, Rotation 90.
6254 clearViewports();
6255 prepareDisplay(DISPLAY_ORIENTATION_90);
6256 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6257 processSync(mapper);
6258
6259 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6260 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6261 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6262
6263 processUp(mapper);
6264 processSync(mapper);
6265 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6266
6267 // Orientation 90, Rotation 180.
6268 clearViewports();
6269 prepareDisplay(DISPLAY_ORIENTATION_180);
6270 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6271 processSync(mapper);
6272
6273 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6274 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6275 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6276
6277 processUp(mapper);
6278 processSync(mapper);
6279 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6280
6281 // Orientation 90, Rotation 270.
6282 clearViewports();
6283 prepareDisplay(DISPLAY_ORIENTATION_270);
6284 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6285 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6286 processSync(mapper);
6287
6288 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6289 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6290 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6291
6292 processUp(mapper);
6293 processSync(mapper);
6294 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6295}
6296
Michael Wrightd02c5b62014-02-10 15:10:22 -08006297TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006298 addConfigurationProperty("touch.deviceType", "touchScreen");
6299 prepareDisplay(DISPLAY_ORIENTATION_0);
6300 prepareButtons();
6301 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006302 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006303
6304 // These calculations are based on the input device calibration documentation.
6305 int32_t rawX = 100;
6306 int32_t rawY = 200;
6307 int32_t rawPressure = 10;
6308 int32_t rawToolMajor = 12;
6309 int32_t rawDistance = 2;
6310 int32_t rawTiltX = 30;
6311 int32_t rawTiltY = 110;
6312
6313 float x = toDisplayX(rawX);
6314 float y = toDisplayY(rawY);
6315 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6316 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6317 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6318 float distance = float(rawDistance);
6319
6320 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6321 float tiltScale = M_PI / 180;
6322 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6323 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6324 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6325 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6326
6327 processDown(mapper, rawX, rawY);
6328 processPressure(mapper, rawPressure);
6329 processToolMajor(mapper, rawToolMajor);
6330 processDistance(mapper, rawDistance);
6331 processTilt(mapper, rawTiltX, rawTiltY);
6332 processSync(mapper);
6333
6334 NotifyMotionArgs args;
6335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6337 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6338 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6339}
6340
Jason Gerecke489fda82012-09-07 17:19:40 -07006341TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006342 addConfigurationProperty("touch.deviceType", "touchScreen");
6343 prepareDisplay(DISPLAY_ORIENTATION_0);
6344 prepareLocationCalibration();
6345 prepareButtons();
6346 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006347 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006348
6349 int32_t rawX = 100;
6350 int32_t rawY = 200;
6351
6352 float x = toDisplayX(toCookedX(rawX, rawY));
6353 float y = toDisplayY(toCookedY(rawX, rawY));
6354
6355 processDown(mapper, rawX, rawY);
6356 processSync(mapper);
6357
6358 NotifyMotionArgs args;
6359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6360 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6361 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6362}
6363
Michael Wrightd02c5b62014-02-10 15:10:22 -08006364TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006365 addConfigurationProperty("touch.deviceType", "touchScreen");
6366 prepareDisplay(DISPLAY_ORIENTATION_0);
6367 prepareButtons();
6368 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006369 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006370
6371 NotifyMotionArgs motionArgs;
6372 NotifyKeyArgs keyArgs;
6373
6374 processDown(mapper, 100, 200);
6375 processSync(mapper);
6376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6377 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6378 ASSERT_EQ(0, motionArgs.buttonState);
6379
6380 // press BTN_LEFT, release BTN_LEFT
6381 processKey(mapper, BTN_LEFT, 1);
6382 processSync(mapper);
6383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6384 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6385 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6386
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6388 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6389 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6390
Michael Wrightd02c5b62014-02-10 15:10:22 -08006391 processKey(mapper, BTN_LEFT, 0);
6392 processSync(mapper);
6393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006394 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006395 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006396
6397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006398 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006399 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006400
6401 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6402 processKey(mapper, BTN_RIGHT, 1);
6403 processKey(mapper, BTN_MIDDLE, 1);
6404 processSync(mapper);
6405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6406 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6407 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6408 motionArgs.buttonState);
6409
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6411 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6412 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6413
6414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6415 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6416 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6417 motionArgs.buttonState);
6418
Michael Wrightd02c5b62014-02-10 15:10:22 -08006419 processKey(mapper, BTN_RIGHT, 0);
6420 processSync(mapper);
6421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006422 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006423 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006424
6425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006426 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006427 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006428
6429 processKey(mapper, BTN_MIDDLE, 0);
6430 processSync(mapper);
6431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006432 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006433 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006434
6435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006436 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006437 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006438
6439 // press BTN_BACK, release BTN_BACK
6440 processKey(mapper, BTN_BACK, 1);
6441 processSync(mapper);
6442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6443 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6444 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006445
Michael Wrightd02c5b62014-02-10 15:10:22 -08006446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006447 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006448 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6449
6450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6451 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6452 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006453
6454 processKey(mapper, BTN_BACK, 0);
6455 processSync(mapper);
6456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006457 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006458 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006459
6460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006461 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006462 ASSERT_EQ(0, motionArgs.buttonState);
6463
Michael Wrightd02c5b62014-02-10 15:10:22 -08006464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6465 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6466 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6467
6468 // press BTN_SIDE, release BTN_SIDE
6469 processKey(mapper, BTN_SIDE, 1);
6470 processSync(mapper);
6471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6472 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6473 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006474
Michael Wrightd02c5b62014-02-10 15:10:22 -08006475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006476 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006477 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6478
6479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6480 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6481 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006482
6483 processKey(mapper, BTN_SIDE, 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);
6492
Michael Wrightd02c5b62014-02-10 15:10:22 -08006493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6494 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6495 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6496
6497 // press BTN_FORWARD, release BTN_FORWARD
6498 processKey(mapper, BTN_FORWARD, 1);
6499 processSync(mapper);
6500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6501 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6502 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006503
Michael Wrightd02c5b62014-02-10 15:10:22 -08006504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006505 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006506 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6507
6508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6509 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6510 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006511
6512 processKey(mapper, BTN_FORWARD, 0);
6513 processSync(mapper);
6514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006515 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006516 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006517
6518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006519 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006520 ASSERT_EQ(0, motionArgs.buttonState);
6521
Michael Wrightd02c5b62014-02-10 15:10:22 -08006522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6523 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6524 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6525
6526 // press BTN_EXTRA, release BTN_EXTRA
6527 processKey(mapper, BTN_EXTRA, 1);
6528 processSync(mapper);
6529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6530 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6531 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006532
Michael Wrightd02c5b62014-02-10 15:10:22 -08006533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006534 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006535 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6536
6537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6538 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6539 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006540
6541 processKey(mapper, BTN_EXTRA, 0);
6542 processSync(mapper);
6543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006544 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006545 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006546
6547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006548 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006549 ASSERT_EQ(0, motionArgs.buttonState);
6550
Michael Wrightd02c5b62014-02-10 15:10:22 -08006551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6552 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6553 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6554
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6556
Michael Wrightd02c5b62014-02-10 15:10:22 -08006557 // press BTN_STYLUS, release BTN_STYLUS
6558 processKey(mapper, BTN_STYLUS, 1);
6559 processSync(mapper);
6560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6561 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006562 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6563
6564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6565 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6566 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006567
6568 processKey(mapper, BTN_STYLUS, 0);
6569 processSync(mapper);
6570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006571 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006572 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006573
6574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006575 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006576 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006577
6578 // press BTN_STYLUS2, release BTN_STYLUS2
6579 processKey(mapper, BTN_STYLUS2, 1);
6580 processSync(mapper);
6581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6582 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006583 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6584
6585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6586 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6587 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006588
6589 processKey(mapper, BTN_STYLUS2, 0);
6590 processSync(mapper);
6591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006592 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006593 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006594
6595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006596 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006597 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006598
6599 // release touch
6600 processUp(mapper);
6601 processSync(mapper);
6602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6603 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6604 ASSERT_EQ(0, motionArgs.buttonState);
6605}
6606
6607TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006608 addConfigurationProperty("touch.deviceType", "touchScreen");
6609 prepareDisplay(DISPLAY_ORIENTATION_0);
6610 prepareButtons();
6611 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006612 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006613
6614 NotifyMotionArgs motionArgs;
6615
6616 // default tool type is finger
6617 processDown(mapper, 100, 200);
6618 processSync(mapper);
6619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6620 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6621 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6622
6623 // eraser
6624 processKey(mapper, BTN_TOOL_RUBBER, 1);
6625 processSync(mapper);
6626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6627 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6628 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6629
6630 // stylus
6631 processKey(mapper, BTN_TOOL_RUBBER, 0);
6632 processKey(mapper, BTN_TOOL_PEN, 1);
6633 processSync(mapper);
6634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6635 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6636 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6637
6638 // brush
6639 processKey(mapper, BTN_TOOL_PEN, 0);
6640 processKey(mapper, BTN_TOOL_BRUSH, 1);
6641 processSync(mapper);
6642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6643 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6644 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6645
6646 // pencil
6647 processKey(mapper, BTN_TOOL_BRUSH, 0);
6648 processKey(mapper, BTN_TOOL_PENCIL, 1);
6649 processSync(mapper);
6650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6651 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6652 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6653
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006654 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006655 processKey(mapper, BTN_TOOL_PENCIL, 0);
6656 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6657 processSync(mapper);
6658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6659 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6660 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6661
6662 // mouse
6663 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6664 processKey(mapper, BTN_TOOL_MOUSE, 1);
6665 processSync(mapper);
6666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6667 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6668 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6669
6670 // lens
6671 processKey(mapper, BTN_TOOL_MOUSE, 0);
6672 processKey(mapper, BTN_TOOL_LENS, 1);
6673 processSync(mapper);
6674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6675 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6676 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6677
6678 // double-tap
6679 processKey(mapper, BTN_TOOL_LENS, 0);
6680 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6681 processSync(mapper);
6682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6683 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6684 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6685
6686 // triple-tap
6687 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6688 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6689 processSync(mapper);
6690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6691 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6692 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6693
6694 // quad-tap
6695 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6696 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6697 processSync(mapper);
6698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6699 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6700 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6701
6702 // finger
6703 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6704 processKey(mapper, BTN_TOOL_FINGER, 1);
6705 processSync(mapper);
6706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6707 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6708 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6709
6710 // stylus trumps finger
6711 processKey(mapper, BTN_TOOL_PEN, 1);
6712 processSync(mapper);
6713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6714 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6715 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6716
6717 // eraser trumps stylus
6718 processKey(mapper, BTN_TOOL_RUBBER, 1);
6719 processSync(mapper);
6720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6721 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6722 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6723
6724 // mouse trumps eraser
6725 processKey(mapper, BTN_TOOL_MOUSE, 1);
6726 processSync(mapper);
6727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6728 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6729 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6730
6731 // back to default tool type
6732 processKey(mapper, BTN_TOOL_MOUSE, 0);
6733 processKey(mapper, BTN_TOOL_RUBBER, 0);
6734 processKey(mapper, BTN_TOOL_PEN, 0);
6735 processKey(mapper, BTN_TOOL_FINGER, 0);
6736 processSync(mapper);
6737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6738 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6739 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6740}
6741
6742TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006743 addConfigurationProperty("touch.deviceType", "touchScreen");
6744 prepareDisplay(DISPLAY_ORIENTATION_0);
6745 prepareButtons();
6746 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006747 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006748 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006749
6750 NotifyMotionArgs motionArgs;
6751
6752 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6753 processKey(mapper, BTN_TOOL_FINGER, 1);
6754 processMove(mapper, 100, 200);
6755 processSync(mapper);
6756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6757 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6758 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6759 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6760
6761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6762 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6763 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6764 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6765
6766 // move a little
6767 processMove(mapper, 150, 250);
6768 processSync(mapper);
6769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6770 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6771 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6772 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6773
6774 // down when BTN_TOUCH is pressed, pressure defaults to 1
6775 processKey(mapper, BTN_TOUCH, 1);
6776 processSync(mapper);
6777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6778 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6780 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6781
6782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6783 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6785 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6786
6787 // up when BTN_TOUCH is released, hover restored
6788 processKey(mapper, BTN_TOUCH, 0);
6789 processSync(mapper);
6790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6791 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6792 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6793 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6794
6795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6796 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6797 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6798 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6799
6800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6801 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6803 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6804
6805 // exit hover when pointer goes away
6806 processKey(mapper, BTN_TOOL_FINGER, 0);
6807 processSync(mapper);
6808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6809 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6810 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6811 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6812}
6813
6814TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006815 addConfigurationProperty("touch.deviceType", "touchScreen");
6816 prepareDisplay(DISPLAY_ORIENTATION_0);
6817 prepareButtons();
6818 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006819 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006820
6821 NotifyMotionArgs motionArgs;
6822
6823 // initially hovering because pressure is 0
6824 processDown(mapper, 100, 200);
6825 processPressure(mapper, 0);
6826 processSync(mapper);
6827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6828 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6829 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6830 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6831
6832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6833 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6834 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6835 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6836
6837 // move a little
6838 processMove(mapper, 150, 250);
6839 processSync(mapper);
6840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6841 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6842 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6843 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6844
6845 // down when pressure is non-zero
6846 processPressure(mapper, RAW_PRESSURE_MAX);
6847 processSync(mapper);
6848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6849 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6851 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6852
6853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6854 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6855 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6856 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6857
6858 // up when pressure becomes 0, hover restored
6859 processPressure(mapper, 0);
6860 processSync(mapper);
6861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6862 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6863 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6864 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6865
6866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6867 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6868 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6869 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6870
6871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6872 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6873 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6874 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6875
6876 // exit hover when pointer goes away
6877 processUp(mapper);
6878 processSync(mapper);
6879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6880 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6881 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6882 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6883}
6884
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006885TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6886 addConfigurationProperty("touch.deviceType", "touchScreen");
6887 prepareDisplay(DISPLAY_ORIENTATION_0);
6888 prepareButtons();
6889 prepareAxes(POSITION | PRESSURE);
6890 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6891
6892 // Touch down.
6893 processDown(mapper, 100, 200);
6894 processPressure(mapper, 1);
6895 processSync(mapper);
6896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6897 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6898
6899 // Reset the mapper. This should cancel the ongoing gesture.
6900 resetMapper(mapper, ARBITRARY_TIME);
6901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6902 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6903
6904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6905}
6906
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006907TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6908 addConfigurationProperty("touch.deviceType", "touchScreen");
6909 prepareDisplay(DISPLAY_ORIENTATION_0);
6910 prepareButtons();
6911 prepareAxes(POSITION | PRESSURE);
6912 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6913
6914 // Set the initial state for the touch pointer.
6915 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6916 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6917 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6918 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6919
6920 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006921 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6922 // does not generate any events.
6923 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006924
6925 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6926 // the recreated touch state to generate a down event.
6927 processSync(mapper);
6928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6929 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6930
6931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6932}
6933
lilinnan687e58f2022-07-19 16:00:50 +08006934TEST_F(SingleTouchInputMapperTest,
6935 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6936 addConfigurationProperty("touch.deviceType", "touchScreen");
6937 prepareDisplay(DISPLAY_ORIENTATION_0);
6938 prepareButtons();
6939 prepareAxes(POSITION);
6940 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6941 NotifyMotionArgs motionArgs;
6942
6943 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006944 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006945 processSync(mapper);
6946
6947 // We should receive a down event
6948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6949 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6950
6951 // Change display id
6952 clearViewports();
6953 prepareSecondaryDisplay(ViewportType::INTERNAL);
6954
6955 // We should receive a cancel event
6956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6957 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6958 // Then receive reset called
6959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6960}
6961
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006962TEST_F(SingleTouchInputMapperTest,
6963 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6964 addConfigurationProperty("touch.deviceType", "touchScreen");
6965 prepareDisplay(DISPLAY_ORIENTATION_0);
6966 prepareButtons();
6967 prepareAxes(POSITION);
6968 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6970 NotifyMotionArgs motionArgs;
6971
6972 // Start a new gesture.
6973 processDown(mapper, 100, 200);
6974 processSync(mapper);
6975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6976 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6977
6978 // Make the viewport inactive. This will put the device in disabled mode.
6979 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6980 viewport->isActive = false;
6981 mFakePolicy->updateViewport(*viewport);
6982 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6983
6984 // We should receive a cancel event for the ongoing gesture.
6985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6986 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6987 // Then we should be notified that the device was reset.
6988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6989
6990 // No events are generated while the viewport is inactive.
6991 processMove(mapper, 101, 201);
6992 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006993 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006994 processSync(mapper);
6995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6996
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006997 // Start a new gesture while the viewport is still inactive.
6998 processDown(mapper, 300, 400);
6999 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
7000 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
7001 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7002 processSync(mapper);
7003
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007004 // Make the viewport active again. The device should resume processing events.
7005 viewport->isActive = true;
7006 mFakePolicy->updateViewport(*viewport);
7007 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7008
7009 // The device is reset because it changes back to direct mode, without generating any events.
7010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7012
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007013 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007014 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7016 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007017
7018 // No more events.
7019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
7021}
7022
Prabir Pradhan5632d622021-09-06 07:57:20 -07007023// --- TouchDisplayProjectionTest ---
7024
7025class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
7026public:
7027 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
7028 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
7029 // rotated equivalent of the given un-rotated physical display bounds.
7030 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
7031 uint32_t inverseRotationFlags;
7032 auto width = DISPLAY_WIDTH;
7033 auto height = DISPLAY_HEIGHT;
7034 switch (orientation) {
7035 case DISPLAY_ORIENTATION_90:
7036 inverseRotationFlags = ui::Transform::ROT_270;
7037 std::swap(width, height);
7038 break;
7039 case DISPLAY_ORIENTATION_180:
7040 inverseRotationFlags = ui::Transform::ROT_180;
7041 break;
7042 case DISPLAY_ORIENTATION_270:
7043 inverseRotationFlags = ui::Transform::ROT_90;
7044 std::swap(width, height);
7045 break;
7046 case DISPLAY_ORIENTATION_0:
7047 inverseRotationFlags = ui::Transform::ROT_0;
7048 break;
7049 default:
7050 FAIL() << "Invalid orientation: " << orientation;
7051 }
7052
7053 const ui::Transform rotation(inverseRotationFlags, width, height);
7054 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
7055
7056 std::optional<DisplayViewport> internalViewport =
7057 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7058 DisplayViewport& v = *internalViewport;
7059 v.displayId = DISPLAY_ID;
7060 v.orientation = orientation;
7061
7062 v.logicalLeft = 0;
7063 v.logicalTop = 0;
7064 v.logicalRight = 100;
7065 v.logicalBottom = 100;
7066
7067 v.physicalLeft = rotatedPhysicalDisplay.left;
7068 v.physicalTop = rotatedPhysicalDisplay.top;
7069 v.physicalRight = rotatedPhysicalDisplay.right;
7070 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7071
7072 v.deviceWidth = width;
7073 v.deviceHeight = height;
7074
7075 v.isActive = true;
7076 v.uniqueId = UNIQUE_ID;
7077 v.type = ViewportType::INTERNAL;
7078 mFakePolicy->updateViewport(v);
7079 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7080 }
7081
7082 void assertReceivedMove(const Point& point) {
7083 NotifyMotionArgs motionArgs;
7084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7085 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7086 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7087 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7088 1, 0, 0, 0, 0, 0, 0, 0));
7089 }
7090};
7091
7092TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7093 addConfigurationProperty("touch.deviceType", "touchScreen");
7094 prepareDisplay(DISPLAY_ORIENTATION_0);
7095
7096 prepareButtons();
7097 prepareAxes(POSITION);
7098 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7099
7100 NotifyMotionArgs motionArgs;
7101
7102 // Configure the DisplayViewport such that the logical display maps to a subsection of
7103 // the display panel called the physical display. Here, the physical display is bounded by the
7104 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7105 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7106 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7107 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7108
7109 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7110 DISPLAY_ORIENTATION_270}) {
7111 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7112
7113 // Touches outside the physical display should be ignored, and should not generate any
7114 // events. Ensure touches at the following points that lie outside of the physical display
7115 // area do not generate any events.
7116 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7117 processDown(mapper, toRawX(point.x), toRawY(point.y));
7118 processSync(mapper);
7119 processUp(mapper);
7120 processSync(mapper);
7121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7122 << "Unexpected event generated for touch outside physical display at point: "
7123 << point.x << ", " << point.y;
7124 }
7125 }
7126}
7127
7128TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7129 addConfigurationProperty("touch.deviceType", "touchScreen");
7130 prepareDisplay(DISPLAY_ORIENTATION_0);
7131
7132 prepareButtons();
7133 prepareAxes(POSITION);
7134 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7135
7136 NotifyMotionArgs motionArgs;
7137
7138 // Configure the DisplayViewport such that the logical display maps to a subsection of
7139 // the display panel called the physical display. Here, the physical display is bounded by the
7140 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7141 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7142
7143 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7144 DISPLAY_ORIENTATION_270}) {
7145 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7146
7147 // Touches that start outside the physical display should be ignored until it enters the
7148 // physical display bounds, at which point it should generate a down event. Start a touch at
7149 // the point (5, 100), which is outside the physical display bounds.
7150 static const Point kOutsidePoint{5, 100};
7151 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7152 processSync(mapper);
7153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7154
7155 // Move the touch into the physical display area. This should generate a pointer down.
7156 processMove(mapper, toRawX(11), toRawY(21));
7157 processSync(mapper);
7158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7159 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7160 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7161 ASSERT_NO_FATAL_FAILURE(
7162 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7163
7164 // Move the touch inside the physical display area. This should generate a pointer move.
7165 processMove(mapper, toRawX(69), toRawY(159));
7166 processSync(mapper);
7167 assertReceivedMove({69, 159});
7168
7169 // Move outside the physical display area. Since the pointer is already down, this should
7170 // now continue generating events.
7171 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7172 processSync(mapper);
7173 assertReceivedMove(kOutsidePoint);
7174
7175 // Release. This should generate a pointer up.
7176 processUp(mapper);
7177 processSync(mapper);
7178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7179 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7180 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7181 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7182
7183 // Ensure no more events were generated.
7184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7186 }
7187}
7188
Michael Wrightd02c5b62014-02-10 15:10:22 -08007189// --- MultiTouchInputMapperTest ---
7190
7191class MultiTouchInputMapperTest : public TouchInputMapperTest {
7192protected:
7193 void prepareAxes(int axes);
7194
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007195 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7196 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7197 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7198 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7199 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7200 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7201 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7202 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7203 void processId(MultiTouchInputMapper& mapper, int32_t id);
7204 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7205 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7206 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
7207 void processMTSync(MultiTouchInputMapper& mapper);
7208 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007209};
7210
7211void MultiTouchInputMapperTest::prepareAxes(int axes) {
7212 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007213 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7214 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007215 }
7216 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007217 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7218 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007219 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007220 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7221 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007222 }
7223 }
7224 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007225 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7226 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007227 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007228 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007229 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007230 }
7231 }
7232 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007233 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7234 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007235 }
7236 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007237 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7238 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007239 }
7240 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007241 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7242 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007243 }
7244 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007245 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7246 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007247 }
7248 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007249 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7250 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007251 }
7252 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007253 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007254 }
7255}
7256
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007257void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7258 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007259 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7260 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007261}
7262
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007263void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7264 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007265 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007266}
7267
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007268void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7269 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007270 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007271}
7272
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007273void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007275}
7276
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007277void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007278 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007279}
7280
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007281void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7282 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007283 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007284}
7285
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007286void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007287 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007288}
7289
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007290void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007291 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007292}
7293
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007294void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007295 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007296}
7297
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007298void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007299 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007300}
7301
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007302void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007303 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007304}
7305
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007306void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7307 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007308 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007309}
7310
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007311void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007312 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007313}
7314
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007315void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007317}
7318
Michael Wrightd02c5b62014-02-10 15:10:22 -08007319TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007320 addConfigurationProperty("touch.deviceType", "touchScreen");
7321 prepareDisplay(DISPLAY_ORIENTATION_0);
7322 prepareAxes(POSITION);
7323 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007324 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007325
arthurhungdcef2dc2020-08-11 14:47:50 +08007326 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007327
7328 NotifyMotionArgs motionArgs;
7329
7330 // Two fingers down at once.
7331 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7332 processPosition(mapper, x1, y1);
7333 processMTSync(mapper);
7334 processPosition(mapper, x2, y2);
7335 processMTSync(mapper);
7336 processSync(mapper);
7337
7338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7339 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7340 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7341 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7342 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7343 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7344 ASSERT_EQ(0, motionArgs.flags);
7345 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7346 ASSERT_EQ(0, motionArgs.buttonState);
7347 ASSERT_EQ(0, motionArgs.edgeFlags);
7348 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7349 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7350 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7351 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7352 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7353 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7354 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7355 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7356
7357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7358 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7359 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7360 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7361 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007362 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007363 ASSERT_EQ(0, motionArgs.flags);
7364 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7365 ASSERT_EQ(0, motionArgs.buttonState);
7366 ASSERT_EQ(0, motionArgs.edgeFlags);
7367 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7368 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7369 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7370 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7371 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7372 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7373 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7374 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7375 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7376 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7377 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7378 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7379
7380 // Move.
7381 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7382 processPosition(mapper, x1, y1);
7383 processMTSync(mapper);
7384 processPosition(mapper, x2, y2);
7385 processMTSync(mapper);
7386 processSync(mapper);
7387
7388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7389 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7390 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7391 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7392 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7393 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7394 ASSERT_EQ(0, motionArgs.flags);
7395 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7396 ASSERT_EQ(0, motionArgs.buttonState);
7397 ASSERT_EQ(0, motionArgs.edgeFlags);
7398 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7399 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7400 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7401 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7402 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7403 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7404 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7405 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7406 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7407 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7408 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7409 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7410
7411 // First finger up.
7412 x2 += 15; y2 -= 20;
7413 processPosition(mapper, x2, y2);
7414 processMTSync(mapper);
7415 processSync(mapper);
7416
7417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7418 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7419 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7420 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7421 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007422 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007423 ASSERT_EQ(0, motionArgs.flags);
7424 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7425 ASSERT_EQ(0, motionArgs.buttonState);
7426 ASSERT_EQ(0, motionArgs.edgeFlags);
7427 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7428 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7429 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7430 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7431 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7433 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7434 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7435 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7436 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7437 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7438 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7439
7440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7441 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7442 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7443 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7444 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7445 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7446 ASSERT_EQ(0, motionArgs.flags);
7447 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7448 ASSERT_EQ(0, motionArgs.buttonState);
7449 ASSERT_EQ(0, motionArgs.edgeFlags);
7450 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7451 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7452 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7454 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7455 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7456 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7457 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7458
7459 // Move.
7460 x2 += 20; y2 -= 25;
7461 processPosition(mapper, x2, y2);
7462 processMTSync(mapper);
7463 processSync(mapper);
7464
7465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7466 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7467 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7468 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7469 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7470 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7471 ASSERT_EQ(0, motionArgs.flags);
7472 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7473 ASSERT_EQ(0, motionArgs.buttonState);
7474 ASSERT_EQ(0, motionArgs.edgeFlags);
7475 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7476 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7477 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7478 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7479 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7480 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7481 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7482 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7483
7484 // New finger down.
7485 int32_t x3 = 700, y3 = 300;
7486 processPosition(mapper, x2, y2);
7487 processMTSync(mapper);
7488 processPosition(mapper, x3, y3);
7489 processMTSync(mapper);
7490 processSync(mapper);
7491
7492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7493 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7494 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7495 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7496 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007497 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007498 ASSERT_EQ(0, motionArgs.flags);
7499 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7500 ASSERT_EQ(0, motionArgs.buttonState);
7501 ASSERT_EQ(0, motionArgs.edgeFlags);
7502 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7503 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7504 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7505 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7506 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7508 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7510 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7511 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7512 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7513 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7514
7515 // Second finger up.
7516 x3 += 30; y3 -= 20;
7517 processPosition(mapper, x3, y3);
7518 processMTSync(mapper);
7519 processSync(mapper);
7520
7521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7522 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7523 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7524 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7525 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007526 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007527 ASSERT_EQ(0, motionArgs.flags);
7528 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7529 ASSERT_EQ(0, motionArgs.buttonState);
7530 ASSERT_EQ(0, motionArgs.edgeFlags);
7531 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7532 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7533 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7534 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7535 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7536 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7537 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7538 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7539 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7540 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7541 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7542 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7543
7544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7545 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7546 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7547 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7548 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7549 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7550 ASSERT_EQ(0, motionArgs.flags);
7551 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7552 ASSERT_EQ(0, motionArgs.buttonState);
7553 ASSERT_EQ(0, motionArgs.edgeFlags);
7554 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7555 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7556 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7557 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7558 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7559 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7560 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7561 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7562
7563 // Last finger up.
7564 processMTSync(mapper);
7565 processSync(mapper);
7566
7567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7568 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7569 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7570 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7571 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7572 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7573 ASSERT_EQ(0, motionArgs.flags);
7574 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7575 ASSERT_EQ(0, motionArgs.buttonState);
7576 ASSERT_EQ(0, motionArgs.edgeFlags);
7577 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7578 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7579 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7581 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7582 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7583 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7584 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7585
7586 // Should not have sent any more keys or motions.
7587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7589}
7590
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007591TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7592 addConfigurationProperty("touch.deviceType", "touchScreen");
7593 prepareDisplay(DISPLAY_ORIENTATION_0);
7594
7595 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7596 /*fuzz*/ 0, /*resolution*/ 10);
7597 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7598 /*fuzz*/ 0, /*resolution*/ 11);
7599 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7600 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7601 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7602 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7603 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7604 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7605 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7606 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7607
7608 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7609
7610 // X and Y axes
7611 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7612 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7613 // Touch major and minor
7614 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7615 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7616 // Tool major and minor
7617 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7618 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7619}
7620
7621TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7622 addConfigurationProperty("touch.deviceType", "touchScreen");
7623 prepareDisplay(DISPLAY_ORIENTATION_0);
7624
7625 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7626 /*fuzz*/ 0, /*resolution*/ 10);
7627 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7628 /*fuzz*/ 0, /*resolution*/ 11);
7629
7630 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7631
7632 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7633
7634 // Touch major and minor
7635 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7636 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7637 // Tool major and minor
7638 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7639 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7640}
7641
Michael Wrightd02c5b62014-02-10 15:10:22 -08007642TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007643 addConfigurationProperty("touch.deviceType", "touchScreen");
7644 prepareDisplay(DISPLAY_ORIENTATION_0);
7645 prepareAxes(POSITION | ID);
7646 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007647 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007648
arthurhungdcef2dc2020-08-11 14:47:50 +08007649 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007650
7651 NotifyMotionArgs motionArgs;
7652
7653 // Two fingers down at once.
7654 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7655 processPosition(mapper, x1, y1);
7656 processId(mapper, 1);
7657 processMTSync(mapper);
7658 processPosition(mapper, x2, y2);
7659 processId(mapper, 2);
7660 processMTSync(mapper);
7661 processSync(mapper);
7662
7663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7664 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7665 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7666 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7667 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7669 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7670
7671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007672 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007673 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7674 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7675 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7676 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7677 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7679 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7680 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7681 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7682
7683 // Move.
7684 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7685 processPosition(mapper, x1, y1);
7686 processId(mapper, 1);
7687 processMTSync(mapper);
7688 processPosition(mapper, x2, y2);
7689 processId(mapper, 2);
7690 processMTSync(mapper);
7691 processSync(mapper);
7692
7693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7694 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7695 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7696 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7697 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7698 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7699 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7701 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7703 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7704
7705 // First finger up.
7706 x2 += 15; y2 -= 20;
7707 processPosition(mapper, x2, y2);
7708 processId(mapper, 2);
7709 processMTSync(mapper);
7710 processSync(mapper);
7711
7712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007713 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007714 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7715 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7716 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7717 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7718 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7719 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7720 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7721 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7722 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7723
7724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7725 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7726 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7727 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7728 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7729 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7730 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7731
7732 // Move.
7733 x2 += 20; y2 -= 25;
7734 processPosition(mapper, x2, y2);
7735 processId(mapper, 2);
7736 processMTSync(mapper);
7737 processSync(mapper);
7738
7739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7740 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7741 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7742 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7743 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7744 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7745 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7746
7747 // New finger down.
7748 int32_t x3 = 700, y3 = 300;
7749 processPosition(mapper, x2, y2);
7750 processId(mapper, 2);
7751 processMTSync(mapper);
7752 processPosition(mapper, x3, y3);
7753 processId(mapper, 3);
7754 processMTSync(mapper);
7755 processSync(mapper);
7756
7757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007758 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007759 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7760 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7761 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7762 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7763 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7764 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7765 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7767 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7768
7769 // Second finger up.
7770 x3 += 30; y3 -= 20;
7771 processPosition(mapper, x3, y3);
7772 processId(mapper, 3);
7773 processMTSync(mapper);
7774 processSync(mapper);
7775
7776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007777 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007778 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7779 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7780 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7781 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7782 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7784 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7785 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7786 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7787
7788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7789 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7790 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7791 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7792 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7793 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7794 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7795
7796 // Last finger up.
7797 processMTSync(mapper);
7798 processSync(mapper);
7799
7800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7801 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7802 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7803 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7804 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7806 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7807
7808 // Should not have sent any more keys or motions.
7809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7811}
7812
7813TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007814 addConfigurationProperty("touch.deviceType", "touchScreen");
7815 prepareDisplay(DISPLAY_ORIENTATION_0);
7816 prepareAxes(POSITION | ID | SLOT);
7817 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007818 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007819
arthurhungdcef2dc2020-08-11 14:47:50 +08007820 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007821
7822 NotifyMotionArgs motionArgs;
7823
7824 // Two fingers down at once.
7825 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7826 processPosition(mapper, x1, y1);
7827 processId(mapper, 1);
7828 processSlot(mapper, 1);
7829 processPosition(mapper, x2, y2);
7830 processId(mapper, 2);
7831 processSync(mapper);
7832
7833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7834 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7835 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7836 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7837 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7838 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7839 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7840
7841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007842 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007843 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7844 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7845 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7846 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7847 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7848 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7849 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7851 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7852
7853 // Move.
7854 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7855 processSlot(mapper, 0);
7856 processPosition(mapper, x1, y1);
7857 processSlot(mapper, 1);
7858 processPosition(mapper, x2, y2);
7859 processSync(mapper);
7860
7861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7862 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7863 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7864 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7865 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7866 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7867 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7868 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7869 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7870 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7871 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7872
7873 // First finger up.
7874 x2 += 15; y2 -= 20;
7875 processSlot(mapper, 0);
7876 processId(mapper, -1);
7877 processSlot(mapper, 1);
7878 processPosition(mapper, x2, y2);
7879 processSync(mapper);
7880
7881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007882 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007883 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7884 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7885 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7886 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7887 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7888 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7889 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7890 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7891 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7892
7893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7894 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7895 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7896 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7897 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7899 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7900
7901 // Move.
7902 x2 += 20; y2 -= 25;
7903 processPosition(mapper, x2, y2);
7904 processSync(mapper);
7905
7906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7907 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7908 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7909 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7910 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7911 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7912 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7913
7914 // New finger down.
7915 int32_t x3 = 700, y3 = 300;
7916 processPosition(mapper, x2, y2);
7917 processSlot(mapper, 0);
7918 processId(mapper, 3);
7919 processPosition(mapper, x3, y3);
7920 processSync(mapper);
7921
7922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007923 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007924 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7925 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7926 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7927 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7928 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7930 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7931 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7932 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7933
7934 // Second finger up.
7935 x3 += 30; y3 -= 20;
7936 processSlot(mapper, 1);
7937 processId(mapper, -1);
7938 processSlot(mapper, 0);
7939 processPosition(mapper, x3, y3);
7940 processSync(mapper);
7941
7942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007943 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007944 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7945 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7946 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7947 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7948 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7949 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7950 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7951 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7952 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7953
7954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7955 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7956 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7957 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7958 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7959 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7960 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7961
7962 // Last finger up.
7963 processId(mapper, -1);
7964 processSync(mapper);
7965
7966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7967 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7968 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7969 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7970 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7971 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7972 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7973
7974 // Should not have sent any more keys or motions.
7975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7977}
7978
7979TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007980 addConfigurationProperty("touch.deviceType", "touchScreen");
7981 prepareDisplay(DISPLAY_ORIENTATION_0);
7982 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007983 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007984
7985 // These calculations are based on the input device calibration documentation.
7986 int32_t rawX = 100;
7987 int32_t rawY = 200;
7988 int32_t rawTouchMajor = 7;
7989 int32_t rawTouchMinor = 6;
7990 int32_t rawToolMajor = 9;
7991 int32_t rawToolMinor = 8;
7992 int32_t rawPressure = 11;
7993 int32_t rawDistance = 0;
7994 int32_t rawOrientation = 3;
7995 int32_t id = 5;
7996
7997 float x = toDisplayX(rawX);
7998 float y = toDisplayY(rawY);
7999 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8000 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8001 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8002 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8003 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8004 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8005 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8006 float distance = float(rawDistance);
8007
8008 processPosition(mapper, rawX, rawY);
8009 processTouchMajor(mapper, rawTouchMajor);
8010 processTouchMinor(mapper, rawTouchMinor);
8011 processToolMajor(mapper, rawToolMajor);
8012 processToolMinor(mapper, rawToolMinor);
8013 processPressure(mapper, rawPressure);
8014 processOrientation(mapper, rawOrientation);
8015 processDistance(mapper, rawDistance);
8016 processId(mapper, id);
8017 processMTSync(mapper);
8018 processSync(mapper);
8019
8020 NotifyMotionArgs args;
8021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8022 ASSERT_EQ(0, args.pointerProperties[0].id);
8023 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8024 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8025 orientation, distance));
8026}
8027
8028TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008029 addConfigurationProperty("touch.deviceType", "touchScreen");
8030 prepareDisplay(DISPLAY_ORIENTATION_0);
8031 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8032 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008033 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008034
8035 // These calculations are based on the input device calibration documentation.
8036 int32_t rawX = 100;
8037 int32_t rawY = 200;
8038 int32_t rawTouchMajor = 140;
8039 int32_t rawTouchMinor = 120;
8040 int32_t rawToolMajor = 180;
8041 int32_t rawToolMinor = 160;
8042
8043 float x = toDisplayX(rawX);
8044 float y = toDisplayY(rawY);
8045 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8046 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8047 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8048 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8049 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8050
8051 processPosition(mapper, rawX, rawY);
8052 processTouchMajor(mapper, rawTouchMajor);
8053 processTouchMinor(mapper, rawTouchMinor);
8054 processToolMajor(mapper, rawToolMajor);
8055 processToolMinor(mapper, rawToolMinor);
8056 processMTSync(mapper);
8057 processSync(mapper);
8058
8059 NotifyMotionArgs args;
8060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8061 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8062 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8063}
8064
8065TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008066 addConfigurationProperty("touch.deviceType", "touchScreen");
8067 prepareDisplay(DISPLAY_ORIENTATION_0);
8068 prepareAxes(POSITION | TOUCH | TOOL);
8069 addConfigurationProperty("touch.size.calibration", "diameter");
8070 addConfigurationProperty("touch.size.scale", "10");
8071 addConfigurationProperty("touch.size.bias", "160");
8072 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008073 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008074
8075 // These calculations are based on the input device calibration documentation.
8076 // Note: We only provide a single common touch/tool value because the device is assumed
8077 // not to emit separate values for each pointer (isSummed = 1).
8078 int32_t rawX = 100;
8079 int32_t rawY = 200;
8080 int32_t rawX2 = 150;
8081 int32_t rawY2 = 250;
8082 int32_t rawTouchMajor = 5;
8083 int32_t rawToolMajor = 8;
8084
8085 float x = toDisplayX(rawX);
8086 float y = toDisplayY(rawY);
8087 float x2 = toDisplayX(rawX2);
8088 float y2 = toDisplayY(rawY2);
8089 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8090 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8091 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8092
8093 processPosition(mapper, rawX, rawY);
8094 processTouchMajor(mapper, rawTouchMajor);
8095 processToolMajor(mapper, rawToolMajor);
8096 processMTSync(mapper);
8097 processPosition(mapper, rawX2, rawY2);
8098 processTouchMajor(mapper, rawTouchMajor);
8099 processToolMajor(mapper, rawToolMajor);
8100 processMTSync(mapper);
8101 processSync(mapper);
8102
8103 NotifyMotionArgs args;
8104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8105 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8106
8107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008108 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008109 ASSERT_EQ(size_t(2), args.pointerCount);
8110 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8111 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8112 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8113 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8114}
8115
8116TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008117 addConfigurationProperty("touch.deviceType", "touchScreen");
8118 prepareDisplay(DISPLAY_ORIENTATION_0);
8119 prepareAxes(POSITION | TOUCH | TOOL);
8120 addConfigurationProperty("touch.size.calibration", "area");
8121 addConfigurationProperty("touch.size.scale", "43");
8122 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008123 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008124
8125 // These calculations are based on the input device calibration documentation.
8126 int32_t rawX = 100;
8127 int32_t rawY = 200;
8128 int32_t rawTouchMajor = 5;
8129 int32_t rawToolMajor = 8;
8130
8131 float x = toDisplayX(rawX);
8132 float y = toDisplayY(rawY);
8133 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8134 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8135 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8136
8137 processPosition(mapper, rawX, rawY);
8138 processTouchMajor(mapper, rawTouchMajor);
8139 processToolMajor(mapper, rawToolMajor);
8140 processMTSync(mapper);
8141 processSync(mapper);
8142
8143 NotifyMotionArgs args;
8144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8145 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8146 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8147}
8148
8149TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008150 addConfigurationProperty("touch.deviceType", "touchScreen");
8151 prepareDisplay(DISPLAY_ORIENTATION_0);
8152 prepareAxes(POSITION | PRESSURE);
8153 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8154 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008155 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008156
Michael Wrightaa449c92017-12-13 21:21:43 +00008157 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008158 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008159 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8160 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8161 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8162
Michael Wrightd02c5b62014-02-10 15:10:22 -08008163 // These calculations are based on the input device calibration documentation.
8164 int32_t rawX = 100;
8165 int32_t rawY = 200;
8166 int32_t rawPressure = 60;
8167
8168 float x = toDisplayX(rawX);
8169 float y = toDisplayY(rawY);
8170 float pressure = float(rawPressure) * 0.01f;
8171
8172 processPosition(mapper, rawX, rawY);
8173 processPressure(mapper, rawPressure);
8174 processMTSync(mapper);
8175 processSync(mapper);
8176
8177 NotifyMotionArgs args;
8178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8180 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8181}
8182
8183TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008184 addConfigurationProperty("touch.deviceType", "touchScreen");
8185 prepareDisplay(DISPLAY_ORIENTATION_0);
8186 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008187 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008188
8189 NotifyMotionArgs motionArgs;
8190 NotifyKeyArgs keyArgs;
8191
8192 processId(mapper, 1);
8193 processPosition(mapper, 100, 200);
8194 processSync(mapper);
8195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8196 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8197 ASSERT_EQ(0, motionArgs.buttonState);
8198
8199 // press BTN_LEFT, release BTN_LEFT
8200 processKey(mapper, BTN_LEFT, 1);
8201 processSync(mapper);
8202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8203 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8204 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8205
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8207 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8208 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8209
Michael Wrightd02c5b62014-02-10 15:10:22 -08008210 processKey(mapper, BTN_LEFT, 0);
8211 processSync(mapper);
8212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008213 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008214 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008215
8216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008217 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008218 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008219
8220 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8221 processKey(mapper, BTN_RIGHT, 1);
8222 processKey(mapper, BTN_MIDDLE, 1);
8223 processSync(mapper);
8224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8225 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8226 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8227 motionArgs.buttonState);
8228
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8230 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8231 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8232
8233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8234 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8235 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8236 motionArgs.buttonState);
8237
Michael Wrightd02c5b62014-02-10 15:10:22 -08008238 processKey(mapper, BTN_RIGHT, 0);
8239 processSync(mapper);
8240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008241 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008242 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008243
8244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008245 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008246 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008247
8248 processKey(mapper, BTN_MIDDLE, 0);
8249 processSync(mapper);
8250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008251 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008252 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008253
8254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008255 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008256 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008257
8258 // press BTN_BACK, release BTN_BACK
8259 processKey(mapper, BTN_BACK, 1);
8260 processSync(mapper);
8261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8262 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8263 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008264
Michael Wrightd02c5b62014-02-10 15:10:22 -08008265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008266 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008267 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8268
8269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8270 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8271 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008272
8273 processKey(mapper, BTN_BACK, 0);
8274 processSync(mapper);
8275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008276 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008277 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008278
8279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008280 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008281 ASSERT_EQ(0, motionArgs.buttonState);
8282
Michael Wrightd02c5b62014-02-10 15:10:22 -08008283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8284 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8285 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8286
8287 // press BTN_SIDE, release BTN_SIDE
8288 processKey(mapper, BTN_SIDE, 1);
8289 processSync(mapper);
8290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8291 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8292 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008293
Michael Wrightd02c5b62014-02-10 15:10:22 -08008294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008295 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008296 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8297
8298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8299 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8300 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008301
8302 processKey(mapper, BTN_SIDE, 0);
8303 processSync(mapper);
8304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008305 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008306 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008307
8308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008309 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008310 ASSERT_EQ(0, motionArgs.buttonState);
8311
Michael Wrightd02c5b62014-02-10 15:10:22 -08008312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8313 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8314 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8315
8316 // press BTN_FORWARD, release BTN_FORWARD
8317 processKey(mapper, BTN_FORWARD, 1);
8318 processSync(mapper);
8319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8320 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8321 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008322
Michael Wrightd02c5b62014-02-10 15:10:22 -08008323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008324 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008325 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8326
8327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8328 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8329 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008330
8331 processKey(mapper, BTN_FORWARD, 0);
8332 processSync(mapper);
8333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008334 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008335 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008336
8337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008338 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008339 ASSERT_EQ(0, motionArgs.buttonState);
8340
Michael Wrightd02c5b62014-02-10 15:10:22 -08008341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8342 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8343 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8344
8345 // press BTN_EXTRA, release BTN_EXTRA
8346 processKey(mapper, BTN_EXTRA, 1);
8347 processSync(mapper);
8348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8349 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8350 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008351
Michael Wrightd02c5b62014-02-10 15:10:22 -08008352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008353 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008354 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8355
8356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8357 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8358 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008359
8360 processKey(mapper, BTN_EXTRA, 0);
8361 processSync(mapper);
8362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008363 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008364 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008365
8366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008367 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008368 ASSERT_EQ(0, motionArgs.buttonState);
8369
Michael Wrightd02c5b62014-02-10 15:10:22 -08008370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8371 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8372 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8373
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8375
Michael Wrightd02c5b62014-02-10 15:10:22 -08008376 // press BTN_STYLUS, release BTN_STYLUS
8377 processKey(mapper, BTN_STYLUS, 1);
8378 processSync(mapper);
8379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8380 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008381 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8382
8383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8384 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8385 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008386
8387 processKey(mapper, BTN_STYLUS, 0);
8388 processSync(mapper);
8389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008390 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008391 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008392
8393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008394 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008395 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008396
8397 // press BTN_STYLUS2, release BTN_STYLUS2
8398 processKey(mapper, BTN_STYLUS2, 1);
8399 processSync(mapper);
8400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8401 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008402 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8403
8404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8405 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8406 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008407
8408 processKey(mapper, BTN_STYLUS2, 0);
8409 processSync(mapper);
8410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008411 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008412 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008413
8414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008415 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008416 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008417
8418 // release touch
8419 processId(mapper, -1);
8420 processSync(mapper);
8421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8422 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8423 ASSERT_EQ(0, motionArgs.buttonState);
8424}
8425
8426TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008427 addConfigurationProperty("touch.deviceType", "touchScreen");
8428 prepareDisplay(DISPLAY_ORIENTATION_0);
8429 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008430 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008431
8432 NotifyMotionArgs motionArgs;
8433
8434 // default tool type is finger
8435 processId(mapper, 1);
8436 processPosition(mapper, 100, 200);
8437 processSync(mapper);
8438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8439 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8440 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8441
8442 // eraser
8443 processKey(mapper, BTN_TOOL_RUBBER, 1);
8444 processSync(mapper);
8445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8446 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8447 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8448
8449 // stylus
8450 processKey(mapper, BTN_TOOL_RUBBER, 0);
8451 processKey(mapper, BTN_TOOL_PEN, 1);
8452 processSync(mapper);
8453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8454 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8455 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8456
8457 // brush
8458 processKey(mapper, BTN_TOOL_PEN, 0);
8459 processKey(mapper, BTN_TOOL_BRUSH, 1);
8460 processSync(mapper);
8461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8462 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8463 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8464
8465 // pencil
8466 processKey(mapper, BTN_TOOL_BRUSH, 0);
8467 processKey(mapper, BTN_TOOL_PENCIL, 1);
8468 processSync(mapper);
8469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8470 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8471 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8472
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008473 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008474 processKey(mapper, BTN_TOOL_PENCIL, 0);
8475 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8476 processSync(mapper);
8477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8478 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8479 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8480
8481 // mouse
8482 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8483 processKey(mapper, BTN_TOOL_MOUSE, 1);
8484 processSync(mapper);
8485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8486 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8487 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8488
8489 // lens
8490 processKey(mapper, BTN_TOOL_MOUSE, 0);
8491 processKey(mapper, BTN_TOOL_LENS, 1);
8492 processSync(mapper);
8493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8494 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8495 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8496
8497 // double-tap
8498 processKey(mapper, BTN_TOOL_LENS, 0);
8499 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8500 processSync(mapper);
8501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8502 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8503 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8504
8505 // triple-tap
8506 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8507 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8508 processSync(mapper);
8509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8510 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8511 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8512
8513 // quad-tap
8514 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8515 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8516 processSync(mapper);
8517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8518 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8519 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8520
8521 // finger
8522 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8523 processKey(mapper, BTN_TOOL_FINGER, 1);
8524 processSync(mapper);
8525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8526 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8527 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8528
8529 // stylus trumps finger
8530 processKey(mapper, BTN_TOOL_PEN, 1);
8531 processSync(mapper);
8532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8533 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8534 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8535
8536 // eraser trumps stylus
8537 processKey(mapper, BTN_TOOL_RUBBER, 1);
8538 processSync(mapper);
8539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8540 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8541 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8542
8543 // mouse trumps eraser
8544 processKey(mapper, BTN_TOOL_MOUSE, 1);
8545 processSync(mapper);
8546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8547 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8548 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8549
8550 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8551 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8552 processSync(mapper);
8553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8554 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8555 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8556
8557 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8558 processToolType(mapper, MT_TOOL_PEN);
8559 processSync(mapper);
8560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8561 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8562 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8563
8564 // back to default tool type
8565 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8566 processKey(mapper, BTN_TOOL_MOUSE, 0);
8567 processKey(mapper, BTN_TOOL_RUBBER, 0);
8568 processKey(mapper, BTN_TOOL_PEN, 0);
8569 processKey(mapper, BTN_TOOL_FINGER, 0);
8570 processSync(mapper);
8571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8572 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8573 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8574}
8575
8576TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008577 addConfigurationProperty("touch.deviceType", "touchScreen");
8578 prepareDisplay(DISPLAY_ORIENTATION_0);
8579 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008580 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008581 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008582
8583 NotifyMotionArgs motionArgs;
8584
8585 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8586 processId(mapper, 1);
8587 processPosition(mapper, 100, 200);
8588 processSync(mapper);
8589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8590 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8592 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8593
8594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8595 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8597 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8598
8599 // move a little
8600 processPosition(mapper, 150, 250);
8601 processSync(mapper);
8602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8603 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8604 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8605 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8606
8607 // down when BTN_TOUCH is pressed, pressure defaults to 1
8608 processKey(mapper, BTN_TOUCH, 1);
8609 processSync(mapper);
8610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8611 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8613 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8614
8615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8616 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8617 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8618 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8619
8620 // up when BTN_TOUCH is released, hover restored
8621 processKey(mapper, BTN_TOUCH, 0);
8622 processSync(mapper);
8623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8624 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8625 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8626 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8627
8628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8629 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8631 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8632
8633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8634 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8635 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8636 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8637
8638 // exit hover when pointer goes away
8639 processId(mapper, -1);
8640 processSync(mapper);
8641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8642 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8643 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8644 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8645}
8646
8647TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008648 addConfigurationProperty("touch.deviceType", "touchScreen");
8649 prepareDisplay(DISPLAY_ORIENTATION_0);
8650 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008651 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008652
8653 NotifyMotionArgs motionArgs;
8654
8655 // initially hovering because pressure is 0
8656 processId(mapper, 1);
8657 processPosition(mapper, 100, 200);
8658 processPressure(mapper, 0);
8659 processSync(mapper);
8660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8661 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8662 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8663 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8664
8665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8666 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8667 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8668 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8669
8670 // move a little
8671 processPosition(mapper, 150, 250);
8672 processSync(mapper);
8673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8674 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8676 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8677
8678 // down when pressure becomes non-zero
8679 processPressure(mapper, RAW_PRESSURE_MAX);
8680 processSync(mapper);
8681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8682 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8683 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8684 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8685
8686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8687 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8688 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8689 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8690
8691 // up when pressure becomes 0, hover restored
8692 processPressure(mapper, 0);
8693 processSync(mapper);
8694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8695 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8697 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8698
8699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8700 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8701 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8702 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8703
8704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8705 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8706 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8707 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8708
8709 // exit hover when pointer goes away
8710 processId(mapper, -1);
8711 processSync(mapper);
8712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8713 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8714 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8715 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8716}
8717
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008718/**
8719 * Set the input device port <--> display port associations, and check that the
8720 * events are routed to the display that matches the display port.
8721 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8722 */
8723TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008724 const std::string usb2 = "USB2";
8725 const uint8_t hdmi1 = 0;
8726 const uint8_t hdmi2 = 1;
8727 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008728 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008729
8730 addConfigurationProperty("touch.deviceType", "touchScreen");
8731 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008732 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008733
8734 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8735 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8736
8737 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8738 // for this input device is specified, and the matching viewport is not present,
8739 // the input device should be disabled (at the mapper level).
8740
8741 // Add viewport for display 2 on hdmi2
8742 prepareSecondaryDisplay(type, hdmi2);
8743 // Send a touch event
8744 processPosition(mapper, 100, 100);
8745 processSync(mapper);
8746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8747
8748 // Add viewport for display 1 on hdmi1
8749 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8750 // Send a touch event again
8751 processPosition(mapper, 100, 100);
8752 processSync(mapper);
8753
8754 NotifyMotionArgs args;
8755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8756 ASSERT_EQ(DISPLAY_ID, args.displayId);
8757}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008758
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008759TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8760 addConfigurationProperty("touch.deviceType", "touchScreen");
8761 prepareAxes(POSITION);
8762 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8763
8764 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8765
8766 prepareDisplay(DISPLAY_ORIENTATION_0);
8767 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8768
8769 // Send a touch event
8770 processPosition(mapper, 100, 100);
8771 processSync(mapper);
8772
8773 NotifyMotionArgs args;
8774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8775 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8776}
8777
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008778TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008779 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008780 std::shared_ptr<FakePointerController> fakePointerController =
8781 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008782 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008783 fakePointerController->setPosition(100, 200);
8784 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008785 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008786
Garfield Tan888a6a42020-01-09 11:39:16 -08008787 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008788 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008789
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008790 prepareDisplay(DISPLAY_ORIENTATION_0);
8791 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008792 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008793
8794 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008795 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008796
8797 NotifyMotionArgs motionArgs;
8798 processPosition(mapper, 100, 100);
8799 processSync(mapper);
8800
8801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8802 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8803 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8804}
8805
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008806/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008807 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8808 */
8809TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8810 addConfigurationProperty("touch.deviceType", "touchScreen");
8811 prepareAxes(POSITION);
8812 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8813
8814 prepareDisplay(DISPLAY_ORIENTATION_0);
8815 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8816 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8817 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8818 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8819
8820 NotifyMotionArgs args;
8821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8822 ASSERT_EQ(26, args.readTime);
8823
8824 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8825 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8826 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8827
8828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8829 ASSERT_EQ(33, args.readTime);
8830}
8831
8832/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008833 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8834 * events should not be delivered to the listener.
8835 */
8836TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8837 addConfigurationProperty("touch.deviceType", "touchScreen");
8838 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8839 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8840 ViewportType::INTERNAL);
8841 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8842 prepareAxes(POSITION);
8843 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8844
8845 NotifyMotionArgs motionArgs;
8846 processPosition(mapper, 100, 100);
8847 processSync(mapper);
8848
8849 mFakeListener->assertNotifyMotionWasNotCalled();
8850}
8851
Garfield Tanc734e4f2021-01-15 20:01:39 -08008852TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8853 addConfigurationProperty("touch.deviceType", "touchScreen");
8854 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8855 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8856 ViewportType::INTERNAL);
8857 std::optional<DisplayViewport> optionalDisplayViewport =
8858 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8859 ASSERT_TRUE(optionalDisplayViewport.has_value());
8860 DisplayViewport displayViewport = *optionalDisplayViewport;
8861
8862 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8863 prepareAxes(POSITION);
8864 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8865
8866 // Finger down
8867 int32_t x = 100, y = 100;
8868 processPosition(mapper, x, y);
8869 processSync(mapper);
8870
8871 NotifyMotionArgs motionArgs;
8872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8873 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8874
8875 // Deactivate display viewport
8876 displayViewport.isActive = false;
8877 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8878 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8879
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008880 // The ongoing touch should be canceled immediately
8881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8882 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8883
8884 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008885 x += 10, y += 10;
8886 processPosition(mapper, x, y);
8887 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008889
8890 // Reactivate display viewport
8891 displayViewport.isActive = true;
8892 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8893 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8894
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008895 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008896 x += 10, y += 10;
8897 processPosition(mapper, x, y);
8898 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8900 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008901}
8902
Arthur Hung7c645402019-01-25 17:45:42 +08008903TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8904 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008905 prepareAxes(POSITION | ID | SLOT);
8906 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008907 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008908
8909 // Create the second touch screen device, and enable multi fingers.
8910 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008911 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008912 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008913 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008914 std::shared_ptr<InputDevice> device2 =
8915 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008916 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008917
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008918 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8919 0 /*flat*/, 0 /*fuzz*/);
8920 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8921 0 /*flat*/, 0 /*fuzz*/);
8922 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8923 0 /*flat*/, 0 /*fuzz*/);
8924 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8925 0 /*flat*/, 0 /*fuzz*/);
8926 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8927 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8928 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008929
8930 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008931 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008932 std::list<NotifyArgs> unused =
8933 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8934 0 /*changes*/);
8935 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08008936
8937 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008938 std::shared_ptr<FakePointerController> fakePointerController =
8939 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008940 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008941
8942 // Setup policy for associated displays and show touches.
8943 const uint8_t hdmi1 = 0;
8944 const uint8_t hdmi2 = 1;
8945 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8946 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8947 mFakePolicy->setShowTouches(true);
8948
8949 // Create displays.
8950 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008951 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008952
8953 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008954 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8955 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8956 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008957
8958 // Two fingers down at default display.
8959 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8960 processPosition(mapper, x1, y1);
8961 processId(mapper, 1);
8962 processSlot(mapper, 1);
8963 processPosition(mapper, x2, y2);
8964 processId(mapper, 2);
8965 processSync(mapper);
8966
8967 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8968 fakePointerController->getSpots().find(DISPLAY_ID);
8969 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8970 ASSERT_EQ(size_t(2), iter->second.size());
8971
8972 // Two fingers down at second display.
8973 processPosition(mapper2, x1, y1);
8974 processId(mapper2, 1);
8975 processSlot(mapper2, 1);
8976 processPosition(mapper2, x2, y2);
8977 processId(mapper2, 2);
8978 processSync(mapper2);
8979
8980 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8981 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8982 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008983
8984 // Disable the show touches configuration and ensure the spots are cleared.
8985 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008986 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8987 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00008988
8989 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008990}
8991
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008992TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008993 prepareAxes(POSITION);
8994 addConfigurationProperty("touch.deviceType", "touchScreen");
8995 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008996 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008997
8998 NotifyMotionArgs motionArgs;
8999 // Unrotated video frame
9000 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9001 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009002 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009003 processPosition(mapper, 100, 200);
9004 processSync(mapper);
9005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9006 ASSERT_EQ(frames, motionArgs.videoFrames);
9007
9008 // Subsequent touch events should not have any videoframes
9009 // This is implemented separately in FakeEventHub,
9010 // but that should match the behaviour of TouchVideoDevice.
9011 processPosition(mapper, 200, 200);
9012 processSync(mapper);
9013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9014 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9015}
9016
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009017TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009018 prepareAxes(POSITION);
9019 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009020 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009021 // Unrotated video frame
9022 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9023 NotifyMotionArgs motionArgs;
9024
9025 // Test all 4 orientations
9026 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009027 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
9028 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9029 clearViewports();
9030 prepareDisplay(orientation);
9031 std::vector<TouchVideoFrame> frames{frame};
9032 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9033 processPosition(mapper, 100, 200);
9034 processSync(mapper);
9035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9036 ASSERT_EQ(frames, motionArgs.videoFrames);
9037 }
9038}
9039
9040TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9041 prepareAxes(POSITION);
9042 addConfigurationProperty("touch.deviceType", "touchScreen");
9043 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9044 // orientation-aware are affected by display rotation.
9045 addConfigurationProperty("touch.orientationAware", "0");
9046 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9047 // Unrotated video frame
9048 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9049 NotifyMotionArgs motionArgs;
9050
9051 // Test all 4 orientations
9052 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009053 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
9054 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9055 clearViewports();
9056 prepareDisplay(orientation);
9057 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009058 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009059 processPosition(mapper, 100, 200);
9060 processSync(mapper);
9061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009062 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9063 // compared to the display. This is so that when the window transform (which contains the
9064 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9065 // window's coordinate space.
9066 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009067 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009068
9069 // Release finger.
9070 processSync(mapper);
9071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009072 }
9073}
9074
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009075TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009076 prepareAxes(POSITION);
9077 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009078 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009079 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9080 // so mix these.
9081 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9082 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9083 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9084 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9085 NotifyMotionArgs motionArgs;
9086
9087 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009088 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009089 processPosition(mapper, 100, 200);
9090 processSync(mapper);
9091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009092 ASSERT_EQ(frames, motionArgs.videoFrames);
9093}
9094
9095TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9096 prepareAxes(POSITION);
9097 addConfigurationProperty("touch.deviceType", "touchScreen");
9098 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9099 // orientation-aware are affected by display rotation.
9100 addConfigurationProperty("touch.orientationAware", "0");
9101 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9102 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9103 // so mix these.
9104 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9105 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9106 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9107 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9108 NotifyMotionArgs motionArgs;
9109
9110 prepareDisplay(DISPLAY_ORIENTATION_90);
9111 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9112 processPosition(mapper, 100, 200);
9113 processSync(mapper);
9114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9115 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9116 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9117 // compared to the display. This is so that when the window transform (which contains the
9118 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9119 // window's coordinate space.
9120 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
9121 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009122 ASSERT_EQ(frames, motionArgs.videoFrames);
9123}
9124
Arthur Hung9da14732019-09-02 16:16:58 +08009125/**
9126 * If we had defined port associations, but the viewport is not ready, the touch device would be
9127 * expected to be disabled, and it should be enabled after the viewport has found.
9128 */
9129TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009130 constexpr uint8_t hdmi2 = 1;
9131 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009132 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009133
9134 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9135
9136 addConfigurationProperty("touch.deviceType", "touchScreen");
9137 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009138 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009139
9140 ASSERT_EQ(mDevice->isEnabled(), false);
9141
9142 // Add display on hdmi2, the device should be enabled and can receive touch event.
9143 prepareSecondaryDisplay(type, hdmi2);
9144 ASSERT_EQ(mDevice->isEnabled(), true);
9145
9146 // Send a touch event.
9147 processPosition(mapper, 100, 100);
9148 processSync(mapper);
9149
9150 NotifyMotionArgs args;
9151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9152 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9153}
9154
Arthur Hung421eb1c2020-01-16 00:09:42 +08009155TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009156 addConfigurationProperty("touch.deviceType", "touchScreen");
9157 prepareDisplay(DISPLAY_ORIENTATION_0);
9158 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009159 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009160
9161 NotifyMotionArgs motionArgs;
9162
9163 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9164 // finger down
9165 processId(mapper, 1);
9166 processPosition(mapper, x1, y1);
9167 processSync(mapper);
9168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9169 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9170 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9171
9172 // finger move
9173 processId(mapper, 1);
9174 processPosition(mapper, x2, y2);
9175 processSync(mapper);
9176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9177 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9178 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9179
9180 // finger up.
9181 processId(mapper, -1);
9182 processSync(mapper);
9183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9184 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9185 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9186
9187 // new finger down
9188 processId(mapper, 1);
9189 processPosition(mapper, x3, y3);
9190 processSync(mapper);
9191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9192 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9193 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9194}
9195
9196/**
arthurhungcc7f9802020-04-30 17:55:40 +08009197 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9198 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009199 */
arthurhungcc7f9802020-04-30 17:55:40 +08009200TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009201 addConfigurationProperty("touch.deviceType", "touchScreen");
9202 prepareDisplay(DISPLAY_ORIENTATION_0);
9203 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009204 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009205
9206 NotifyMotionArgs motionArgs;
9207
9208 // default tool type is finger
9209 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009210 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009211 processPosition(mapper, x1, y1);
9212 processSync(mapper);
9213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9214 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9215 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9216
9217 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9218 processToolType(mapper, MT_TOOL_PALM);
9219 processSync(mapper);
9220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9221 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9222
9223 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009224 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009225 processPosition(mapper, x2, y2);
9226 processSync(mapper);
9227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9228
9229 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009230 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009231 processSync(mapper);
9232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9233
9234 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009235 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009236 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009237 processPosition(mapper, x3, y3);
9238 processSync(mapper);
9239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9240 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9241 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9242}
9243
arthurhungbf89a482020-04-17 17:37:55 +08009244/**
arthurhungcc7f9802020-04-30 17:55:40 +08009245 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9246 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009247 */
arthurhungcc7f9802020-04-30 17:55:40 +08009248TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009249 addConfigurationProperty("touch.deviceType", "touchScreen");
9250 prepareDisplay(DISPLAY_ORIENTATION_0);
9251 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9252 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9253
9254 NotifyMotionArgs motionArgs;
9255
9256 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009257 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9258 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009259 processPosition(mapper, x1, y1);
9260 processSync(mapper);
9261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9262 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9263 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9264
9265 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009266 processSlot(mapper, SECOND_SLOT);
9267 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009268 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009269 processSync(mapper);
9270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009271 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009272 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9273
9274 // If the tool type of the first finger changes to MT_TOOL_PALM,
9275 // we expect to receive ACTION_POINTER_UP with cancel flag.
9276 processSlot(mapper, FIRST_SLOT);
9277 processId(mapper, FIRST_TRACKING_ID);
9278 processToolType(mapper, MT_TOOL_PALM);
9279 processSync(mapper);
9280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009281 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009282 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9283
9284 // The following MOVE events of second finger should be processed.
9285 processSlot(mapper, SECOND_SLOT);
9286 processId(mapper, SECOND_TRACKING_ID);
9287 processPosition(mapper, x2 + 1, y2 + 1);
9288 processSync(mapper);
9289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9291 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9292
9293 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9294 // it. Second finger receive move.
9295 processSlot(mapper, FIRST_SLOT);
9296 processId(mapper, INVALID_TRACKING_ID);
9297 processSync(mapper);
9298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9299 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9300 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9301
9302 // Second finger keeps moving.
9303 processSlot(mapper, SECOND_SLOT);
9304 processId(mapper, SECOND_TRACKING_ID);
9305 processPosition(mapper, x2 + 2, y2 + 2);
9306 processSync(mapper);
9307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9308 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9309 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9310
9311 // Second finger up.
9312 processId(mapper, INVALID_TRACKING_ID);
9313 processSync(mapper);
9314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9315 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9316 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9317}
9318
9319/**
9320 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9321 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9322 */
9323TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9324 addConfigurationProperty("touch.deviceType", "touchScreen");
9325 prepareDisplay(DISPLAY_ORIENTATION_0);
9326 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9327 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9328
9329 NotifyMotionArgs motionArgs;
9330
9331 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9332 // First finger down.
9333 processId(mapper, FIRST_TRACKING_ID);
9334 processPosition(mapper, x1, y1);
9335 processSync(mapper);
9336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9337 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9338 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9339
9340 // Second finger down.
9341 processSlot(mapper, SECOND_SLOT);
9342 processId(mapper, SECOND_TRACKING_ID);
9343 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009344 processSync(mapper);
9345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009346 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009347 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9348
arthurhungcc7f9802020-04-30 17:55:40 +08009349 // If the tool type of the first finger changes to MT_TOOL_PALM,
9350 // we expect to receive ACTION_POINTER_UP with cancel flag.
9351 processSlot(mapper, FIRST_SLOT);
9352 processId(mapper, FIRST_TRACKING_ID);
9353 processToolType(mapper, MT_TOOL_PALM);
9354 processSync(mapper);
9355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009356 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009357 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9358
9359 // Second finger keeps moving.
9360 processSlot(mapper, SECOND_SLOT);
9361 processId(mapper, SECOND_TRACKING_ID);
9362 processPosition(mapper, x2 + 1, y2 + 1);
9363 processSync(mapper);
9364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9365 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9366
9367 // second finger becomes palm, receive cancel due to only 1 finger is active.
9368 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009369 processToolType(mapper, MT_TOOL_PALM);
9370 processSync(mapper);
9371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9372 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9373
arthurhungcc7f9802020-04-30 17:55:40 +08009374 // third finger down.
9375 processSlot(mapper, THIRD_SLOT);
9376 processId(mapper, THIRD_TRACKING_ID);
9377 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009378 processPosition(mapper, x3, y3);
9379 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9381 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9382 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009383 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9384
9385 // third finger move
9386 processId(mapper, THIRD_TRACKING_ID);
9387 processPosition(mapper, x3 + 1, y3 + 1);
9388 processSync(mapper);
9389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9390 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9391
9392 // first finger up, third finger receive move.
9393 processSlot(mapper, FIRST_SLOT);
9394 processId(mapper, INVALID_TRACKING_ID);
9395 processSync(mapper);
9396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9398 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9399
9400 // second finger up, third finger receive move.
9401 processSlot(mapper, SECOND_SLOT);
9402 processId(mapper, INVALID_TRACKING_ID);
9403 processSync(mapper);
9404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9405 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9406 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9407
9408 // third finger up.
9409 processSlot(mapper, THIRD_SLOT);
9410 processId(mapper, INVALID_TRACKING_ID);
9411 processSync(mapper);
9412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9413 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9414 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9415}
9416
9417/**
9418 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9419 * and the active finger could still be allowed to receive the events
9420 */
9421TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9422 addConfigurationProperty("touch.deviceType", "touchScreen");
9423 prepareDisplay(DISPLAY_ORIENTATION_0);
9424 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9425 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9426
9427 NotifyMotionArgs motionArgs;
9428
9429 // default tool type is finger
9430 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9431 processId(mapper, FIRST_TRACKING_ID);
9432 processPosition(mapper, x1, y1);
9433 processSync(mapper);
9434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9435 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9436 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9437
9438 // Second finger down.
9439 processSlot(mapper, SECOND_SLOT);
9440 processId(mapper, SECOND_TRACKING_ID);
9441 processPosition(mapper, x2, y2);
9442 processSync(mapper);
9443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009444 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009445 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9446
9447 // If the tool type of the second finger changes to MT_TOOL_PALM,
9448 // we expect to receive ACTION_POINTER_UP with cancel flag.
9449 processId(mapper, SECOND_TRACKING_ID);
9450 processToolType(mapper, MT_TOOL_PALM);
9451 processSync(mapper);
9452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009453 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009454 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9455
9456 // The following MOVE event should be processed.
9457 processSlot(mapper, FIRST_SLOT);
9458 processId(mapper, FIRST_TRACKING_ID);
9459 processPosition(mapper, x1 + 1, y1 + 1);
9460 processSync(mapper);
9461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9462 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9463 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9464
9465 // second finger up.
9466 processSlot(mapper, SECOND_SLOT);
9467 processId(mapper, INVALID_TRACKING_ID);
9468 processSync(mapper);
9469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9470 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9471
9472 // first finger keep moving
9473 processSlot(mapper, FIRST_SLOT);
9474 processId(mapper, FIRST_TRACKING_ID);
9475 processPosition(mapper, x1 + 2, y1 + 2);
9476 processSync(mapper);
9477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9478 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9479
9480 // first finger up.
9481 processId(mapper, INVALID_TRACKING_ID);
9482 processSync(mapper);
9483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9484 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9485 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009486}
9487
Arthur Hung9ad18942021-06-19 02:04:46 +00009488/**
9489 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9490 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9491 * cause slot be valid again.
9492 */
9493TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9494 addConfigurationProperty("touch.deviceType", "touchScreen");
9495 prepareDisplay(DISPLAY_ORIENTATION_0);
9496 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9497 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9498
9499 NotifyMotionArgs motionArgs;
9500
9501 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9502 // First finger down.
9503 processId(mapper, FIRST_TRACKING_ID);
9504 processPosition(mapper, x1, y1);
9505 processPressure(mapper, RAW_PRESSURE_MAX);
9506 processSync(mapper);
9507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9508 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9509 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9510
9511 // First finger move.
9512 processId(mapper, FIRST_TRACKING_ID);
9513 processPosition(mapper, x1 + 1, y1 + 1);
9514 processPressure(mapper, RAW_PRESSURE_MAX);
9515 processSync(mapper);
9516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9517 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9518 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9519
9520 // Second finger down.
9521 processSlot(mapper, SECOND_SLOT);
9522 processId(mapper, SECOND_TRACKING_ID);
9523 processPosition(mapper, x2, y2);
9524 processPressure(mapper, RAW_PRESSURE_MAX);
9525 processSync(mapper);
9526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009527 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009528 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9529
9530 // second finger up with some unexpected data.
9531 processSlot(mapper, SECOND_SLOT);
9532 processId(mapper, INVALID_TRACKING_ID);
9533 processPosition(mapper, x2, y2);
9534 processSync(mapper);
9535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009536 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009537 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9538
9539 // first finger up with some unexpected data.
9540 processSlot(mapper, FIRST_SLOT);
9541 processId(mapper, INVALID_TRACKING_ID);
9542 processPosition(mapper, x2, y2);
9543 processPressure(mapper, RAW_PRESSURE_MAX);
9544 processSync(mapper);
9545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9546 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9547 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9548}
9549
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009550TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9551 addConfigurationProperty("touch.deviceType", "touchScreen");
9552 prepareDisplay(DISPLAY_ORIENTATION_0);
9553 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9554 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9555
9556 // First finger down.
9557 processId(mapper, FIRST_TRACKING_ID);
9558 processPosition(mapper, 100, 200);
9559 processPressure(mapper, RAW_PRESSURE_MAX);
9560 processSync(mapper);
9561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9562 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9563
9564 // Second finger down.
9565 processSlot(mapper, SECOND_SLOT);
9566 processId(mapper, SECOND_TRACKING_ID);
9567 processPosition(mapper, 300, 400);
9568 processPressure(mapper, RAW_PRESSURE_MAX);
9569 processSync(mapper);
9570 ASSERT_NO_FATAL_FAILURE(
9571 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9572
9573 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009574 // preserved. Resetting should cancel the ongoing gesture.
9575 resetMapper(mapper, ARBITRARY_TIME);
9576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9577 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009578
9579 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9580 // the existing touch state to generate a down event.
9581 processPosition(mapper, 301, 302);
9582 processSync(mapper);
9583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9584 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
9585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9586 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
9587
9588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9589}
9590
9591TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9592 addConfigurationProperty("touch.deviceType", "touchScreen");
9593 prepareDisplay(DISPLAY_ORIENTATION_0);
9594 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9595 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9596
9597 // First finger touches down and releases.
9598 processId(mapper, FIRST_TRACKING_ID);
9599 processPosition(mapper, 100, 200);
9600 processPressure(mapper, RAW_PRESSURE_MAX);
9601 processSync(mapper);
9602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9603 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9604 processId(mapper, INVALID_TRACKING_ID);
9605 processSync(mapper);
9606 ASSERT_NO_FATAL_FAILURE(
9607 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9608
9609 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9610 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009611 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9613
9614 // Send an empty sync frame. Since there are no pointers, no events are generated.
9615 processSync(mapper);
9616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9617}
9618
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009619// --- MultiTouchInputMapperTest_ExternalDevice ---
9620
9621class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9622protected:
Chris Yea52ade12020-08-27 16:49:20 -07009623 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009624};
9625
9626/**
9627 * Expect fallback to internal viewport if device is external and external viewport is not present.
9628 */
9629TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9630 prepareAxes(POSITION);
9631 addConfigurationProperty("touch.deviceType", "touchScreen");
9632 prepareDisplay(DISPLAY_ORIENTATION_0);
9633 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9634
9635 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9636
9637 NotifyMotionArgs motionArgs;
9638
9639 // Expect the event to be sent to the internal viewport,
9640 // because an external viewport is not present.
9641 processPosition(mapper, 100, 100);
9642 processSync(mapper);
9643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9644 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9645
9646 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009647 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009648 processPosition(mapper, 100, 100);
9649 processSync(mapper);
9650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9651 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9652}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009653
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009654TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9655 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9656 std::shared_ptr<FakePointerController> fakePointerController =
9657 std::make_shared<FakePointerController>();
9658 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9659 fakePointerController->setPosition(0, 0);
9660 fakePointerController->setButtonState(0);
9661
9662 // prepare device and capture
9663 prepareDisplay(DISPLAY_ORIENTATION_0);
9664 prepareAxes(POSITION | ID | SLOT);
9665 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9666 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9667 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009668 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009669 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9670
9671 // captured touchpad should be a touchpad source
9672 NotifyDeviceResetArgs resetArgs;
9673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9674 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9675
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009676 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009677
9678 const InputDeviceInfo::MotionRange* relRangeX =
9679 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9680 ASSERT_NE(relRangeX, nullptr);
9681 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9682 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9683 const InputDeviceInfo::MotionRange* relRangeY =
9684 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9685 ASSERT_NE(relRangeY, nullptr);
9686 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9687 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9688
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009689 // run captured pointer tests - note that this is unscaled, so input listener events should be
9690 // identical to what the hardware sends (accounting for any
9691 // calibration).
9692 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009693 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009694 processId(mapper, 1);
9695 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9696 processKey(mapper, BTN_TOUCH, 1);
9697 processSync(mapper);
9698
9699 // expect coord[0] to contain initial location of touch 0
9700 NotifyMotionArgs args;
9701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9702 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9703 ASSERT_EQ(1U, args.pointerCount);
9704 ASSERT_EQ(0, args.pointerProperties[0].id);
9705 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9706 ASSERT_NO_FATAL_FAILURE(
9707 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9708
9709 // FINGER 1 DOWN
9710 processSlot(mapper, 1);
9711 processId(mapper, 2);
9712 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9713 processSync(mapper);
9714
9715 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009717 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009718 ASSERT_EQ(2U, args.pointerCount);
9719 ASSERT_EQ(0, args.pointerProperties[0].id);
9720 ASSERT_EQ(1, args.pointerProperties[1].id);
9721 ASSERT_NO_FATAL_FAILURE(
9722 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9723 ASSERT_NO_FATAL_FAILURE(
9724 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9725
9726 // FINGER 1 MOVE
9727 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9728 processSync(mapper);
9729
9730 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9731 // from move
9732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9733 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9734 ASSERT_NO_FATAL_FAILURE(
9735 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9736 ASSERT_NO_FATAL_FAILURE(
9737 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9738
9739 // FINGER 0 MOVE
9740 processSlot(mapper, 0);
9741 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9742 processSync(mapper);
9743
9744 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9746 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9747 ASSERT_NO_FATAL_FAILURE(
9748 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9749 ASSERT_NO_FATAL_FAILURE(
9750 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9751
9752 // BUTTON DOWN
9753 processKey(mapper, BTN_LEFT, 1);
9754 processSync(mapper);
9755
9756 // touchinputmapper design sends a move before button press
9757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9758 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9760 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9761
9762 // BUTTON UP
9763 processKey(mapper, BTN_LEFT, 0);
9764 processSync(mapper);
9765
9766 // touchinputmapper design sends a move after button release
9767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9768 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9770 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9771
9772 // FINGER 0 UP
9773 processId(mapper, -1);
9774 processSync(mapper);
9775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9776 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9777
9778 // FINGER 1 MOVE
9779 processSlot(mapper, 1);
9780 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9781 processSync(mapper);
9782
9783 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9785 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9786 ASSERT_EQ(1U, args.pointerCount);
9787 ASSERT_EQ(1, args.pointerProperties[0].id);
9788 ASSERT_NO_FATAL_FAILURE(
9789 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9790
9791 // FINGER 1 UP
9792 processId(mapper, -1);
9793 processKey(mapper, BTN_TOUCH, 0);
9794 processSync(mapper);
9795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9796 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9797
9798 // non captured touchpad should be a mouse source
9799 mFakePolicy->setPointerCapture(false);
9800 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9802 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9803}
9804
9805TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9806 std::shared_ptr<FakePointerController> fakePointerController =
9807 std::make_shared<FakePointerController>();
9808 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9809 fakePointerController->setPosition(0, 0);
9810 fakePointerController->setButtonState(0);
9811
9812 // prepare device and capture
9813 prepareDisplay(DISPLAY_ORIENTATION_0);
9814 prepareAxes(POSITION | ID | SLOT);
9815 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9816 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009817 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009818 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9819 // run uncaptured pointer tests - pushes out generic events
9820 // FINGER 0 DOWN
9821 processId(mapper, 3);
9822 processPosition(mapper, 100, 100);
9823 processKey(mapper, BTN_TOUCH, 1);
9824 processSync(mapper);
9825
9826 // start at (100,100), cursor should be at (0,0) * scale
9827 NotifyMotionArgs args;
9828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9829 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9830 ASSERT_NO_FATAL_FAILURE(
9831 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9832
9833 // FINGER 0 MOVE
9834 processPosition(mapper, 200, 200);
9835 processSync(mapper);
9836
9837 // compute scaling to help with touch position checking
9838 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9839 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9840 float scale =
9841 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9842
9843 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9845 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9846 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9847 0, 0, 0, 0, 0, 0, 0));
9848}
9849
9850TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9851 std::shared_ptr<FakePointerController> fakePointerController =
9852 std::make_shared<FakePointerController>();
9853
9854 prepareDisplay(DISPLAY_ORIENTATION_0);
9855 prepareAxes(POSITION | ID | SLOT);
9856 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009857 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009858 mFakePolicy->setPointerCapture(false);
9859 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9860
9861 // uncaptured touchpad should be a pointer device
9862 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9863
9864 // captured touchpad should be a touchpad device
9865 mFakePolicy->setPointerCapture(true);
9866 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9867 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9868}
9869
HQ Liue6983c72022-04-19 22:14:56 +00009870class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9871protected:
9872 float mPointerMovementScale;
9873 float mPointerXZoomScale;
9874 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9875 addConfigurationProperty("touch.deviceType", "pointer");
9876 std::shared_ptr<FakePointerController> fakePointerController =
9877 std::make_shared<FakePointerController>();
9878 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9879 fakePointerController->setPosition(0, 0);
9880 fakePointerController->setButtonState(0);
9881 prepareDisplay(DISPLAY_ORIENTATION_0);
9882
9883 prepareAxes(POSITION);
9884 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9885 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9886 // needs to be disabled, and the pointer gesture needs to be enabled.
9887 mFakePolicy->setPointerCapture(false);
9888 mFakePolicy->setPointerGestureEnabled(true);
9889 mFakePolicy->setPointerController(fakePointerController);
9890
9891 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9892 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9893 mPointerMovementScale =
9894 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9895 mPointerXZoomScale =
9896 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9897 }
9898
9899 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9900 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9901 /*flat*/ 0,
9902 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9903 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9904 /*flat*/ 0,
9905 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9906 }
9907};
9908
9909/**
9910 * Two fingers down on a pointer mode touch pad. The width
9911 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9912 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9913 * be greater than the both value to be freeform gesture, so that after two
9914 * fingers start to move downwards, the gesture should be swipe.
9915 */
9916TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9917 // The min freeform gesture width is 25units/mm x 30mm = 750
9918 // which is greater than fraction of the diagnal length of the touchpad (349).
9919 // Thus, MaxSwipWidth is 750.
9920 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9921 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9922 NotifyMotionArgs motionArgs;
9923
9924 // Two fingers down at once.
9925 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9926 // Pointer's initial position is used the [0,0] coordinate.
9927 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9928
9929 processId(mapper, FIRST_TRACKING_ID);
9930 processPosition(mapper, x1, y1);
9931 processMTSync(mapper);
9932 processId(mapper, SECOND_TRACKING_ID);
9933 processPosition(mapper, x2, y2);
9934 processMTSync(mapper);
9935 processSync(mapper);
9936
9937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9938 ASSERT_EQ(1U, motionArgs.pointerCount);
9939 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9940 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009941 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009942 ASSERT_NO_FATAL_FAILURE(
9943 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9944
9945 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9946 // that there should be 1 pointer.
9947 int32_t movingDistance = 200;
9948 y1 += movingDistance;
9949 y2 += movingDistance;
9950
9951 processId(mapper, FIRST_TRACKING_ID);
9952 processPosition(mapper, x1, y1);
9953 processMTSync(mapper);
9954 processId(mapper, SECOND_TRACKING_ID);
9955 processPosition(mapper, x2, y2);
9956 processMTSync(mapper);
9957 processSync(mapper);
9958
9959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9960 ASSERT_EQ(1U, motionArgs.pointerCount);
9961 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9962 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009963 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009964 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9965 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9966 0, 0, 0, 0));
9967}
9968
9969/**
9970 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9971 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9972 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9973 * value to be freeform gesture, so that after two fingers start to move downwards,
9974 * the gesture should be swipe.
9975 */
9976TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9977 // The min freeform gesture width is 5units/mm x 30mm = 150
9978 // which is greater than fraction of the diagnal length of the touchpad (349).
9979 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
9980 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
9981 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9982 NotifyMotionArgs motionArgs;
9983
9984 // Two fingers down at once.
9985 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9986 // Pointer's initial position is used the [0,0] coordinate.
9987 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9988
9989 processId(mapper, FIRST_TRACKING_ID);
9990 processPosition(mapper, x1, y1);
9991 processMTSync(mapper);
9992 processId(mapper, SECOND_TRACKING_ID);
9993 processPosition(mapper, x2, y2);
9994 processMTSync(mapper);
9995 processSync(mapper);
9996
9997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9998 ASSERT_EQ(1U, motionArgs.pointerCount);
9999 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10000 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010001 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010002 ASSERT_NO_FATAL_FAILURE(
10003 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10004
10005 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10006 // and there should be 1 pointer.
10007 int32_t movingDistance = 200;
10008 y1 += movingDistance;
10009 y2 += movingDistance;
10010
10011 processId(mapper, FIRST_TRACKING_ID);
10012 processPosition(mapper, x1, y1);
10013 processMTSync(mapper);
10014 processId(mapper, SECOND_TRACKING_ID);
10015 processPosition(mapper, x2, y2);
10016 processMTSync(mapper);
10017 processSync(mapper);
10018
10019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10020 ASSERT_EQ(1U, motionArgs.pointerCount);
10021 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10022 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010023 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010024 // New coordinate is the scaled relative coordinate from the initial coordinate.
10025 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10026 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10027 0, 0, 0, 0));
10028}
10029
10030/**
10031 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10032 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10033 * freeform gestures after two fingers start to move downwards.
10034 */
10035TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
10036 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10037 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10038
10039 NotifyMotionArgs motionArgs;
10040
10041 // Two fingers down at once. Wider than the max swipe width.
10042 // The gesture is expected to be PRESS, then transformed to FREEFORM
10043 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10044
10045 processId(mapper, FIRST_TRACKING_ID);
10046 processPosition(mapper, x1, y1);
10047 processMTSync(mapper);
10048 processId(mapper, SECOND_TRACKING_ID);
10049 processPosition(mapper, x2, y2);
10050 processMTSync(mapper);
10051 processSync(mapper);
10052
10053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10054 ASSERT_EQ(1U, motionArgs.pointerCount);
10055 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10056 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010057 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010058 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10059 ASSERT_NO_FATAL_FAILURE(
10060 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10061
10062 int32_t movingDistance = 200;
10063
10064 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10065 // then two down events for two pointers.
10066 y1 += movingDistance;
10067 y2 += movingDistance;
10068
10069 processId(mapper, FIRST_TRACKING_ID);
10070 processPosition(mapper, x1, y1);
10071 processMTSync(mapper);
10072 processId(mapper, SECOND_TRACKING_ID);
10073 processPosition(mapper, x2, y2);
10074 processMTSync(mapper);
10075 processSync(mapper);
10076
10077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10078 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10079 ASSERT_EQ(1U, motionArgs.pointerCount);
10080 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10082 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10083 ASSERT_EQ(1U, motionArgs.pointerCount);
10084 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10086 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010087 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010088 ASSERT_EQ(2U, motionArgs.pointerCount);
10089 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
10090 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010091 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010092 // Two pointers' scaled relative coordinates from their initial centroid.
10093 // Initial y coordinates are 0 as y1 and y2 have the same value.
10094 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10095 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10096 // When pointers move, the new coordinates equal to the initial coordinates plus
10097 // scaled moving distance.
10098 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10099 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10100 0, 0, 0, 0));
10101 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10102 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10103 0, 0, 0, 0));
10104
10105 // Move two fingers down again, expect one MOVE motion event.
10106 y1 += movingDistance;
10107 y2 += movingDistance;
10108
10109 processId(mapper, FIRST_TRACKING_ID);
10110 processPosition(mapper, x1, y1);
10111 processMTSync(mapper);
10112 processId(mapper, SECOND_TRACKING_ID);
10113 processPosition(mapper, x2, y2);
10114 processMTSync(mapper);
10115 processSync(mapper);
10116
10117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10118 ASSERT_EQ(2U, motionArgs.pointerCount);
10119 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10120 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010121 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010122 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10123 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10124 0, 0, 0, 0, 0));
10125 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10126 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10127 0, 0, 0, 0, 0));
10128}
10129
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010130// --- JoystickInputMapperTest ---
10131
10132class JoystickInputMapperTest : public InputMapperTest {
10133protected:
10134 static const int32_t RAW_X_MIN;
10135 static const int32_t RAW_X_MAX;
10136 static const int32_t RAW_Y_MIN;
10137 static const int32_t RAW_Y_MAX;
10138
10139 void SetUp() override {
10140 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10141 }
10142 void prepareAxes() {
10143 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10144 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10145 }
10146
10147 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10148 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10149 }
10150
10151 void processSync(JoystickInputMapper& mapper) {
10152 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10153 }
10154
10155 void prepareVirtualDisplay(int32_t orientation) {
10156 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10157 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10158 NO_PORT, ViewportType::VIRTUAL);
10159 }
10160};
10161
10162const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10163const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10164const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10165const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10166
10167TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10168 prepareAxes();
10169 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10170
10171 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10172
10173 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
10174
10175 // Send an axis event
10176 processAxis(mapper, ABS_X, 100);
10177 processSync(mapper);
10178
10179 NotifyMotionArgs args;
10180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10181 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10182
10183 // Send another axis event
10184 processAxis(mapper, ABS_Y, 100);
10185 processSync(mapper);
10186
10187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10188 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10189}
10190
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010191// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010192
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010193class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010194protected:
10195 static const char* DEVICE_NAME;
10196 static const char* DEVICE_LOCATION;
10197 static const int32_t DEVICE_ID;
10198 static const int32_t DEVICE_GENERATION;
10199 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010200 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010201 static const int32_t EVENTHUB_ID;
10202
10203 std::shared_ptr<FakeEventHub> mFakeEventHub;
10204 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010205 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010206 std::unique_ptr<InstrumentedInputReader> mReader;
10207 std::shared_ptr<InputDevice> mDevice;
10208
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010209 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010210 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010211 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010212 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010213 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010214 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010215 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10216 }
10217
10218 void SetUp() override { SetUp(DEVICE_CLASSES); }
10219
10220 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010221 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010222 mFakePolicy.clear();
10223 }
10224
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010225 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010226 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10227 mReader->requestRefreshConfiguration(changes);
10228 mReader->loopOnce();
10229 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010230 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010231 }
10232
10233 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10234 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010235 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010236 InputDeviceIdentifier identifier;
10237 identifier.name = name;
10238 identifier.location = location;
10239 std::shared_ptr<InputDevice> device =
10240 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10241 identifier);
10242 mReader->pushNextDevice(device);
10243 mFakeEventHub->addDevice(eventHubId, name, classes);
10244 mReader->loopOnce();
10245 return device;
10246 }
10247
10248 template <class T, typename... Args>
10249 T& addControllerAndConfigure(Args... args) {
10250 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10251
10252 return controller;
10253 }
10254};
10255
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010256const char* PeripheralControllerTest::DEVICE_NAME = "device";
10257const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10258const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10259const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10260const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010261const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10262 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010263const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010264
10265// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010266class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010267protected:
10268 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010269 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010270 }
10271};
10272
10273TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010274 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010275
10276 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
10277 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
10278}
10279
10280TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010281 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010282
10283 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
10284 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
10285}
10286
10287// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010288class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010289protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010290 void SetUp() override {
10291 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10292 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010293};
10294
Chris Ye85758332021-05-16 23:05:17 -070010295TEST_F(LightControllerTest, MonoLight) {
10296 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010297 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010298 .maxBrightness = 255,
10299 .flags = InputLightClass::BRIGHTNESS,
10300 .path = ""};
10301 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010302
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010303 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010304 InputDeviceInfo info;
10305 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010306 std::vector<InputDeviceLightInfo> lights = info.getLights();
10307 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010308 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10309 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10310
10311 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10312 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10313}
10314
10315TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10316 RawLightInfo infoMono = {.id = 1,
10317 .name = "mono_keyboard_backlight",
10318 .maxBrightness = 255,
10319 .flags = InputLightClass::BRIGHTNESS |
10320 InputLightClass::KEYBOARD_BACKLIGHT,
10321 .path = ""};
10322 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10323
10324 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10325 InputDeviceInfo info;
10326 controller.populateDeviceInfo(&info);
10327 std::vector<InputDeviceLightInfo> lights = info.getLights();
10328 ASSERT_EQ(1U, lights.size());
10329 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10330 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010331
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010332 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10333 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010334}
10335
10336TEST_F(LightControllerTest, RGBLight) {
10337 RawLightInfo infoRed = {.id = 1,
10338 .name = "red",
10339 .maxBrightness = 255,
10340 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10341 .path = ""};
10342 RawLightInfo infoGreen = {.id = 2,
10343 .name = "green",
10344 .maxBrightness = 255,
10345 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10346 .path = ""};
10347 RawLightInfo infoBlue = {.id = 3,
10348 .name = "blue",
10349 .maxBrightness = 255,
10350 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10351 .path = ""};
10352 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10353 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10354 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10355
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010356 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010357 InputDeviceInfo info;
10358 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010359 std::vector<InputDeviceLightInfo> lights = info.getLights();
10360 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010361 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10362 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10363 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10364
10365 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10366 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10367}
10368
10369TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10370 RawLightInfo infoRed = {.id = 1,
10371 .name = "red_keyboard_backlight",
10372 .maxBrightness = 255,
10373 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10374 InputLightClass::KEYBOARD_BACKLIGHT,
10375 .path = ""};
10376 RawLightInfo infoGreen = {.id = 2,
10377 .name = "green_keyboard_backlight",
10378 .maxBrightness = 255,
10379 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10380 InputLightClass::KEYBOARD_BACKLIGHT,
10381 .path = ""};
10382 RawLightInfo infoBlue = {.id = 3,
10383 .name = "blue_keyboard_backlight",
10384 .maxBrightness = 255,
10385 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10386 InputLightClass::KEYBOARD_BACKLIGHT,
10387 .path = ""};
10388 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10389 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10390 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10391
10392 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10393 InputDeviceInfo info;
10394 controller.populateDeviceInfo(&info);
10395 std::vector<InputDeviceLightInfo> lights = info.getLights();
10396 ASSERT_EQ(1U, lights.size());
10397 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10398 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10399 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10400
10401 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10402 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10403}
10404
10405TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10406 RawLightInfo infoRed = {.id = 1,
10407 .name = "red",
10408 .maxBrightness = 255,
10409 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10410 .path = ""};
10411 RawLightInfo infoGreen = {.id = 2,
10412 .name = "green",
10413 .maxBrightness = 255,
10414 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10415 .path = ""};
10416 RawLightInfo infoBlue = {.id = 3,
10417 .name = "blue",
10418 .maxBrightness = 255,
10419 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10420 .path = ""};
10421 RawLightInfo infoGlobal = {.id = 3,
10422 .name = "global_keyboard_backlight",
10423 .maxBrightness = 255,
10424 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10425 InputLightClass::KEYBOARD_BACKLIGHT,
10426 .path = ""};
10427 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10428 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10429 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10430 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10431
10432 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10433 InputDeviceInfo info;
10434 controller.populateDeviceInfo(&info);
10435 std::vector<InputDeviceLightInfo> lights = info.getLights();
10436 ASSERT_EQ(1U, lights.size());
10437 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10438 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10439 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010440
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010441 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10442 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010443}
10444
10445TEST_F(LightControllerTest, MultiColorRGBLight) {
10446 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010447 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010448 .maxBrightness = 255,
10449 .flags = InputLightClass::BRIGHTNESS |
10450 InputLightClass::MULTI_INTENSITY |
10451 InputLightClass::MULTI_INDEX,
10452 .path = ""};
10453
10454 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10455
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010456 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010457 InputDeviceInfo info;
10458 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010459 std::vector<InputDeviceLightInfo> lights = info.getLights();
10460 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010461 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10462 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10463 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10464
10465 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10466 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10467}
10468
10469TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10470 RawLightInfo infoColor = {.id = 1,
10471 .name = "multi_color_keyboard_backlight",
10472 .maxBrightness = 255,
10473 .flags = InputLightClass::BRIGHTNESS |
10474 InputLightClass::MULTI_INTENSITY |
10475 InputLightClass::MULTI_INDEX |
10476 InputLightClass::KEYBOARD_BACKLIGHT,
10477 .path = ""};
10478
10479 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10480
10481 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10482 InputDeviceInfo info;
10483 controller.populateDeviceInfo(&info);
10484 std::vector<InputDeviceLightInfo> lights = info.getLights();
10485 ASSERT_EQ(1U, lights.size());
10486 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10487 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10488 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010489
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010490 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10491 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010492}
10493
10494TEST_F(LightControllerTest, PlayerIdLight) {
10495 RawLightInfo info1 = {.id = 1,
10496 .name = "player1",
10497 .maxBrightness = 255,
10498 .flags = InputLightClass::BRIGHTNESS,
10499 .path = ""};
10500 RawLightInfo info2 = {.id = 2,
10501 .name = "player2",
10502 .maxBrightness = 255,
10503 .flags = InputLightClass::BRIGHTNESS,
10504 .path = ""};
10505 RawLightInfo info3 = {.id = 3,
10506 .name = "player3",
10507 .maxBrightness = 255,
10508 .flags = InputLightClass::BRIGHTNESS,
10509 .path = ""};
10510 RawLightInfo info4 = {.id = 4,
10511 .name = "player4",
10512 .maxBrightness = 255,
10513 .flags = InputLightClass::BRIGHTNESS,
10514 .path = ""};
10515 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10516 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10517 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10518 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10519
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010520 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010521 InputDeviceInfo info;
10522 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010523 std::vector<InputDeviceLightInfo> lights = info.getLights();
10524 ASSERT_EQ(1U, lights.size());
10525 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010526 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10527 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010528
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010529 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10530 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10531 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010532}
10533
Michael Wrightd02c5b62014-02-10 15:10:22 -080010534} // namespace android