blob: f1f1abd8b0ac6652176211a6797eb7fda1933a58 [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 Vishniakou370039c2021-02-04 22:09:01 +0000829 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
830 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800831
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000832 const size_t filledSize = std::min(mEvents.size(), bufferSize);
833 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
834
835 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700836 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000837 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800838 }
839
Chris Yea52ade12020-08-27 16:49:20 -0700840 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600841 auto it = mVideoFrames.find(deviceId);
842 if (it != mVideoFrames.end()) {
843 std::vector<TouchVideoFrame> frames = std::move(it->second);
844 mVideoFrames.erase(deviceId);
845 return frames;
846 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800847 return {};
848 }
849
Chris Yea52ade12020-08-27 16:49:20 -0700850 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800851 Device* device = getDevice(deviceId);
852 if (device) {
853 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
854 if (index >= 0) {
855 return device->scanCodeStates.valueAt(index);
856 }
857 }
858 return AKEY_STATE_UNKNOWN;
859 }
860
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000861 InputDeviceCountryCode getCountryCode(int32_t deviceId) const override {
862 Device* device = getDevice(deviceId);
863 if (device) {
864 return device->countryCode;
865 }
866 return InputDeviceCountryCode::INVALID;
867 }
868
Chris Yea52ade12020-08-27 16:49:20 -0700869 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800870 Device* device = getDevice(deviceId);
871 if (device) {
872 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
873 if (index >= 0) {
874 return device->keyCodeStates.valueAt(index);
875 }
876 }
877 return AKEY_STATE_UNKNOWN;
878 }
879
Chris Yea52ade12020-08-27 16:49:20 -0700880 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800881 Device* device = getDevice(deviceId);
882 if (device) {
883 ssize_t index = device->switchStates.indexOfKey(sw);
884 if (index >= 0) {
885 return device->switchStates.valueAt(index);
886 }
887 }
888 return AKEY_STATE_UNKNOWN;
889 }
890
Chris Yea52ade12020-08-27 16:49:20 -0700891 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
892 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800893 Device* device = getDevice(deviceId);
894 if (device) {
895 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
896 if (index >= 0) {
897 *outValue = device->absoluteAxisValue.valueAt(index);
898 return OK;
899 }
900 }
901 *outValue = 0;
902 return -1;
903 }
904
Philip Junker4af3b3d2021-12-14 10:36:55 +0100905 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
906 Device* device = getDevice(deviceId);
907 if (!device) {
908 return AKEYCODE_UNKNOWN;
909 }
910 auto it = device->keyCodeMapping.find(locationKeyCode);
911 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
912 }
913
Chris Yea52ade12020-08-27 16:49:20 -0700914 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700915 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700916 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800917 bool result = false;
918 Device* device = getDevice(deviceId);
919 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700920 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700921 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800922 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
923 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
924 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800925 }
926 }
927 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
928 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
929 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800930 }
931 }
932 }
933 }
934 return result;
935 }
936
Chris Yea52ade12020-08-27 16:49:20 -0700937 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800938 Device* device = getDevice(deviceId);
939 if (device) {
940 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
941 return index >= 0;
942 }
943 return false;
944 }
945
Arthur Hungcb40a002021-08-03 14:31:01 +0000946 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
947 Device* device = getDevice(deviceId);
948 if (!device) {
949 return false;
950 }
951 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
952 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
953 return true;
954 }
955 }
956 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
957 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
958 return true;
959 }
960 }
961 return false;
962 }
963
Chris Yea52ade12020-08-27 16:49:20 -0700964 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965 Device* device = getDevice(deviceId);
966 return device && device->leds.indexOfKey(led) >= 0;
967 }
968
Chris Yea52ade12020-08-27 16:49:20 -0700969 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800970 Device* device = getDevice(deviceId);
971 if (device) {
972 ssize_t index = device->leds.indexOfKey(led);
973 if (index >= 0) {
974 device->leds.replaceValueAt(led, on);
975 } else {
976 ADD_FAILURE()
977 << "Attempted to set the state of an LED that the EventHub declared "
978 "was not present. led=" << led;
979 }
980 }
981 }
982
Chris Yea52ade12020-08-27 16:49:20 -0700983 void getVirtualKeyDefinitions(
984 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800985 outVirtualKeys.clear();
986
987 Device* device = getDevice(deviceId);
988 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800989 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800990 }
991 }
992
Chris Yea52ade12020-08-27 16:49:20 -0700993 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700994 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800995 }
996
Chris Yea52ade12020-08-27 16:49:20 -0700997 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800998 return false;
999 }
1000
Chris Yea52ade12020-08-27 16:49:20 -07001001 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001002
Chris Yea52ade12020-08-27 16:49:20 -07001003 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001004
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001005 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override { return mVibrators; };
Chris Ye87143712020-11-10 05:05:58 +00001006
Chris Yee2b1e5c2021-03-10 22:45:12 -08001007 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
1008 return BATTERY_CAPACITY;
1009 }
Kim Low03ea0352020-11-06 12:45:07 -08001010
Chris Yee2b1e5c2021-03-10 22:45:12 -08001011 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
1012 return BATTERY_STATUS;
1013 }
1014
Andy Chenf9f1a022022-08-29 20:07:10 -04001015 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override {
1016 return {DEFAULT_BATTERY};
1017 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001018
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001019 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
1020 int32_t batteryId) const override {
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001021 if (batteryId != DEFAULT_BATTERY) return {};
1022 static const auto BATTERY_INFO = RawBatteryInfo{.id = DEFAULT_BATTERY,
1023 .name = "default battery",
1024 .flags = InputBatteryClass::CAPACITY,
1025 .path = BATTERY_DEVPATH};
1026 return BATTERY_INFO;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001027 }
Kim Low03ea0352020-11-06 12:45:07 -08001028
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001029 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001030 std::vector<int32_t> ids;
1031 for (const auto& [rawId, info] : mRawLightInfos) {
1032 ids.push_back(rawId);
1033 }
1034 return ids;
1035 }
1036
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001037 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001038 auto it = mRawLightInfos.find(lightId);
1039 if (it == mRawLightInfos.end()) {
1040 return std::nullopt;
1041 }
1042 return it->second;
1043 }
1044
1045 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1046 mLightBrightness.emplace(lightId, brightness);
1047 }
1048
1049 void setLightIntensities(int32_t deviceId, int32_t lightId,
1050 std::unordered_map<LightColor, int32_t> intensities) override {
1051 mLightIntensities.emplace(lightId, intensities);
1052 };
1053
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001054 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001055 auto lightIt = mLightBrightness.find(lightId);
1056 if (lightIt == mLightBrightness.end()) {
1057 return std::nullopt;
1058 }
1059 return lightIt->second;
1060 }
1061
1062 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001063 int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001064 auto lightIt = mLightIntensities.find(lightId);
1065 if (lightIt == mLightIntensities.end()) {
1066 return std::nullopt;
1067 }
1068 return lightIt->second;
1069 };
1070
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001071 void dump(std::string&) const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001072
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001073 void monitor() const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001074
Chris Yea52ade12020-08-27 16:49:20 -07001075 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001076
Chris Yea52ade12020-08-27 16:49:20 -07001077 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001078};
1079
Michael Wrightd02c5b62014-02-10 15:10:22 -08001080// --- FakeInputMapper ---
1081
1082class FakeInputMapper : public InputMapper {
1083 uint32_t mSources;
1084 int32_t mKeyboardType;
1085 int32_t mMetaState;
1086 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1087 KeyedVector<int32_t, int32_t> mScanCodeStates;
1088 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001089 // fake mapping which would normally come from keyCharacterMap
1090 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001091 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001092
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001093 std::mutex mLock;
1094 std::condition_variable mStateChangedCondition;
1095 bool mConfigureWasCalled GUARDED_BY(mLock);
1096 bool mResetWasCalled GUARDED_BY(mLock);
1097 bool mProcessWasCalled GUARDED_BY(mLock);
1098 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001099
Arthur Hungc23540e2018-11-29 20:42:11 +08001100 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001101public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001102 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1103 : InputMapper(deviceContext),
1104 mSources(sources),
1105 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001106 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001107 mConfigureWasCalled(false),
1108 mResetWasCalled(false),
1109 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001110
Chris Yea52ade12020-08-27 16:49:20 -07001111 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001112
1113 void setKeyboardType(int32_t keyboardType) {
1114 mKeyboardType = keyboardType;
1115 }
1116
1117 void setMetaState(int32_t metaState) {
1118 mMetaState = metaState;
1119 }
1120
1121 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001122 std::unique_lock<std::mutex> lock(mLock);
1123 base::ScopedLockAssertion assumeLocked(mLock);
1124 const bool configureCalled =
1125 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1126 return mConfigureWasCalled;
1127 });
1128 if (!configureCalled) {
1129 FAIL() << "Expected configure() to have been called.";
1130 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001131 mConfigureWasCalled = false;
1132 }
1133
1134 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001135 std::unique_lock<std::mutex> lock(mLock);
1136 base::ScopedLockAssertion assumeLocked(mLock);
1137 const bool resetCalled =
1138 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1139 return mResetWasCalled;
1140 });
1141 if (!resetCalled) {
1142 FAIL() << "Expected reset() to have been called.";
1143 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001144 mResetWasCalled = false;
1145 }
1146
Yi Kong9b14ac62018-07-17 13:48:38 -07001147 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001148 std::unique_lock<std::mutex> lock(mLock);
1149 base::ScopedLockAssertion assumeLocked(mLock);
1150 const bool processCalled =
1151 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1152 return mProcessWasCalled;
1153 });
1154 if (!processCalled) {
1155 FAIL() << "Expected process() to have been called.";
1156 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001157 if (outLastEvent) {
1158 *outLastEvent = mLastEvent;
1159 }
1160 mProcessWasCalled = false;
1161 }
1162
1163 void setKeyCodeState(int32_t keyCode, int32_t state) {
1164 mKeyCodeStates.replaceValueFor(keyCode, state);
1165 }
1166
1167 void setScanCodeState(int32_t scanCode, int32_t state) {
1168 mScanCodeStates.replaceValueFor(scanCode, state);
1169 }
1170
1171 void setSwitchState(int32_t switchCode, int32_t state) {
1172 mSwitchStates.replaceValueFor(switchCode, state);
1173 }
1174
1175 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001176 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001177 }
1178
Philip Junker4af3b3d2021-12-14 10:36:55 +01001179 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1180 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1181 }
1182
Michael Wrightd02c5b62014-02-10 15:10:22 -08001183private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001184 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001185
Chris Yea52ade12020-08-27 16:49:20 -07001186 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001187 InputMapper::populateDeviceInfo(deviceInfo);
1188
1189 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1190 deviceInfo->setKeyboardType(mKeyboardType);
1191 }
1192 }
1193
Chris Yea52ade12020-08-27 16:49:20 -07001194 void configure(nsecs_t, const InputReaderConfiguration* config, 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();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001205 }
1206
Chris Yea52ade12020-08-27 16:49:20 -07001207 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001208 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001209 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001210 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001211 }
1212
Chris Yea52ade12020-08-27 16:49:20 -07001213 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001214 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001215 mLastEvent = *rawEvent;
1216 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001217 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001218 }
1219
Chris Yea52ade12020-08-27 16:49:20 -07001220 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001221 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1222 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1223 }
1224
Philip Junker4af3b3d2021-12-14 10:36:55 +01001225 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1226 auto it = mKeyCodeMapping.find(locationKeyCode);
1227 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1228 }
1229
Chris Yea52ade12020-08-27 16:49:20 -07001230 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001231 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1232 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1233 }
1234
Chris Yea52ade12020-08-27 16:49:20 -07001235 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001236 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1237 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1238 }
1239
Chris Yea52ade12020-08-27 16:49:20 -07001240 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001241 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -07001242 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001243 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001244 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1245 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1246 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001247 }
1248 }
1249 }
Chris Yea52ade12020-08-27 16:49:20 -07001250 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001251 return result;
1252 }
1253
1254 virtual int32_t getMetaState() {
1255 return mMetaState;
1256 }
1257
1258 virtual void fadePointer() {
1259 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001260
1261 virtual std::optional<int32_t> getAssociatedDisplay() {
1262 if (mViewport) {
1263 return std::make_optional(mViewport->displayId);
1264 }
1265 return std::nullopt;
1266 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001267};
1268
1269
1270// --- InstrumentedInputReader ---
1271
1272class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001273 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001274
1275public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001276 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1277 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001278 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001279 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001280
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001281 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001282
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001283 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001284
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001285 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001286 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001287 InputDeviceIdentifier identifier;
1288 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001289 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001290 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001291 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001292 }
1293
Prabir Pradhan28efc192019-11-05 01:10:04 +00001294 // Make the protected loopOnce method accessible to tests.
1295 using InputReader::loopOnce;
1296
Michael Wrightd02c5b62014-02-10 15:10:22 -08001297protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001298 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1299 const InputDeviceIdentifier& identifier)
1300 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001301 if (!mNextDevices.empty()) {
1302 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1303 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001304 return device;
1305 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001306 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001307 }
1308
arthurhungdcef2dc2020-08-11 14:47:50 +08001309 // --- FakeInputReaderContext ---
1310 class FakeInputReaderContext : public ContextImpl {
1311 int32_t mGlobalMetaState;
1312 bool mUpdateGlobalMetaStateWasCalled;
1313 int32_t mGeneration;
1314
1315 public:
1316 FakeInputReaderContext(InputReader* reader)
1317 : ContextImpl(reader),
1318 mGlobalMetaState(0),
1319 mUpdateGlobalMetaStateWasCalled(false),
1320 mGeneration(1) {}
1321
1322 virtual ~FakeInputReaderContext() {}
1323
1324 void assertUpdateGlobalMetaStateWasCalled() {
1325 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1326 << "Expected updateGlobalMetaState() to have been called.";
1327 mUpdateGlobalMetaStateWasCalled = false;
1328 }
1329
1330 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1331
1332 uint32_t getGeneration() { return mGeneration; }
1333
1334 void updateGlobalMetaState() override {
1335 mUpdateGlobalMetaStateWasCalled = true;
1336 ContextImpl::updateGlobalMetaState();
1337 }
1338
1339 int32_t getGlobalMetaState() override {
1340 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1341 }
1342
1343 int32_t bumpGeneration() override {
1344 mGeneration = ContextImpl::bumpGeneration();
1345 return mGeneration;
1346 }
1347 } mFakeContext;
1348
Michael Wrightd02c5b62014-02-10 15:10:22 -08001349 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001350
1351public:
1352 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001353};
1354
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001355// --- InputReaderPolicyTest ---
1356class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001357protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001358 sp<FakeInputReaderPolicy> mFakePolicy;
1359
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001360 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -07001361 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001362};
1363
1364/**
1365 * Check that empty set of viewports is an acceptable configuration.
1366 * Also try to get internal viewport two different ways - by type and by uniqueId.
1367 *
1368 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1369 * Such configuration is not currently allowed.
1370 */
1371TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001372 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001373
1374 // We didn't add any viewports yet, so there shouldn't be any.
1375 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001376 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001377 ASSERT_FALSE(internalViewport);
1378
1379 // Add an internal viewport, then clear it
1380 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001381 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001382 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001383
1384 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001385 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001386 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001387 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001388
1389 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001390 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001391 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001392 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001393
1394 mFakePolicy->clearViewports();
1395 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001396 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001397 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001398 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001399 ASSERT_FALSE(internalViewport);
1400}
1401
1402TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1403 const std::string internalUniqueId = "local:0";
1404 const std::string externalUniqueId = "local:1";
1405 const std::string virtualUniqueId1 = "virtual:2";
1406 const std::string virtualUniqueId2 = "virtual:3";
1407 constexpr int32_t virtualDisplayId1 = 2;
1408 constexpr int32_t virtualDisplayId2 = 3;
1409
1410 // Add an internal viewport
1411 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001412 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1413 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001414 // Add an external viewport
1415 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001416 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1417 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001418 // Add an virtual viewport
1419 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001420 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1421 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001422 // Add another virtual viewport
1423 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001424 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1425 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001426
1427 // Check matching by type for internal
1428 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001429 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001430 ASSERT_TRUE(internalViewport);
1431 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1432
1433 // Check matching by type for external
1434 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001435 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001436 ASSERT_TRUE(externalViewport);
1437 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1438
1439 // Check matching by uniqueId for virtual viewport #1
1440 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001441 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001442 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001443 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001444 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1445 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1446
1447 // Check matching by uniqueId for virtual viewport #2
1448 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001449 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001450 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001451 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001452 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1453 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1454}
1455
1456
1457/**
1458 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1459 * that lookup works by checking display id.
1460 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1461 */
1462TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1463 const std::string uniqueId1 = "uniqueId1";
1464 const std::string uniqueId2 = "uniqueId2";
1465 constexpr int32_t displayId1 = 2;
1466 constexpr int32_t displayId2 = 3;
1467
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001468 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1469 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001470 for (const ViewportType& type : types) {
1471 mFakePolicy->clearViewports();
1472 // Add a viewport
1473 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001474 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1475 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001476 // Add another viewport
1477 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001478 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1479 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001480
1481 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001482 std::optional<DisplayViewport> viewport1 =
1483 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001484 ASSERT_TRUE(viewport1);
1485 ASSERT_EQ(displayId1, viewport1->displayId);
1486 ASSERT_EQ(type, viewport1->type);
1487
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001488 std::optional<DisplayViewport> viewport2 =
1489 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001490 ASSERT_TRUE(viewport2);
1491 ASSERT_EQ(displayId2, viewport2->displayId);
1492 ASSERT_EQ(type, viewport2->type);
1493
1494 // When there are multiple viewports of the same kind, and uniqueId is not specified
1495 // in the call to getDisplayViewport, then that situation is not supported.
1496 // The viewports can be stored in any order, so we cannot rely on the order, since that
1497 // is just implementation detail.
1498 // However, we can check that it still returns *a* viewport, we just cannot assert
1499 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001500 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001501 ASSERT_TRUE(someViewport);
1502 }
1503}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001504
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001505/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001506 * When we have multiple internal displays make sure we always return the default display when
1507 * querying by type.
1508 */
1509TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1510 const std::string uniqueId1 = "uniqueId1";
1511 const std::string uniqueId2 = "uniqueId2";
1512 constexpr int32_t nonDefaultDisplayId = 2;
1513 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1514 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1515
1516 // Add the default display first and ensure it gets returned.
1517 mFakePolicy->clearViewports();
1518 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001519 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001520 ViewportType::INTERNAL);
1521 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001522 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001523 ViewportType::INTERNAL);
1524
1525 std::optional<DisplayViewport> viewport =
1526 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1527 ASSERT_TRUE(viewport);
1528 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1529 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1530
1531 // Add the default display second to make sure order doesn't matter.
1532 mFakePolicy->clearViewports();
1533 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001534 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001535 ViewportType::INTERNAL);
1536 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001537 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001538 ViewportType::INTERNAL);
1539
1540 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1541 ASSERT_TRUE(viewport);
1542 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1543 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1544}
1545
1546/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001547 * Check getDisplayViewportByPort
1548 */
1549TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001550 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001551 const std::string uniqueId1 = "uniqueId1";
1552 const std::string uniqueId2 = "uniqueId2";
1553 constexpr int32_t displayId1 = 1;
1554 constexpr int32_t displayId2 = 2;
1555 const uint8_t hdmi1 = 0;
1556 const uint8_t hdmi2 = 1;
1557 const uint8_t hdmi3 = 2;
1558
1559 mFakePolicy->clearViewports();
1560 // Add a viewport that's associated with some display port that's not of interest.
1561 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001562 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1563 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001564 // Add another viewport, connected to HDMI1 port
1565 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001566 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1567 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001568
1569 // Check that correct display viewport was returned by comparing the display ports.
1570 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1571 ASSERT_TRUE(hdmi1Viewport);
1572 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1573 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1574
1575 // Check that we can still get the same viewport using the uniqueId
1576 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1577 ASSERT_TRUE(hdmi1Viewport);
1578 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1579 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1580 ASSERT_EQ(type, hdmi1Viewport->type);
1581
1582 // Check that we cannot find a port with "HDMI2", because we never added one
1583 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1584 ASSERT_FALSE(hdmi2Viewport);
1585}
1586
Michael Wrightd02c5b62014-02-10 15:10:22 -08001587// --- InputReaderTest ---
1588
1589class InputReaderTest : public testing::Test {
1590protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001591 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001592 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001593 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001594 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001595
Chris Yea52ade12020-08-27 16:49:20 -07001596 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001597 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001598 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001599 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001600
Prabir Pradhan28efc192019-11-05 01:10:04 +00001601 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001602 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001603 }
1604
Chris Yea52ade12020-08-27 16:49:20 -07001605 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001606 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001607 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001608 }
1609
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001610 void addDevice(int32_t eventHubId, const std::string& name,
1611 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001612 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001613
1614 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001615 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001616 }
1617 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001618 mReader->loopOnce();
1619 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001620 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1621 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001622 }
1623
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001624 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001625 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001626 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001627 }
1628
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001629 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001630 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001631 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001632 }
1633
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001634 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001635 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001636 ftl::Flags<InputDeviceClass> classes,
1637 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001638 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001639 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1640 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001641 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001642 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001643 return mapper;
1644 }
1645};
1646
Chris Ye98d3f532020-10-01 21:48:59 -07001647TEST_F(InputReaderTest, PolicyGetInputDevices) {
1648 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001649 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001650 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001651
1652 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001653 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001654 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001655 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001656 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001657 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1658 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001659 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001660}
1661
Chris Yee7310032020-09-22 15:36:28 -07001662TEST_F(InputReaderTest, GetMergedInputDevices) {
1663 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1664 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1665 // Add two subdevices to device
1666 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1667 // Must add at least one mapper or the device will be ignored!
1668 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1669 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1670
1671 // Push same device instance for next device to be added, so they'll have same identifier.
1672 mReader->pushNextDevice(device);
1673 mReader->pushNextDevice(device);
1674 ASSERT_NO_FATAL_FAILURE(
1675 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1676 ASSERT_NO_FATAL_FAILURE(
1677 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1678
1679 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001680 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001681}
1682
Chris Yee14523a2020-12-19 13:46:00 -08001683TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1684 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1685 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1686 // Add two subdevices to device
1687 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1688 // Must add at least one mapper or the device will be ignored!
1689 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1690 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1691
1692 // Push same device instance for next device to be added, so they'll have same identifier.
1693 mReader->pushNextDevice(device);
1694 mReader->pushNextDevice(device);
1695 // Sensor device is initially disabled
1696 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1697 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1698 nullptr));
1699 // Device is disabled because the only sub device is a sensor device and disabled initially.
1700 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1701 ASSERT_FALSE(device->isEnabled());
1702 ASSERT_NO_FATAL_FAILURE(
1703 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1704 // The merged device is enabled if any sub device is enabled
1705 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1706 ASSERT_TRUE(device->isEnabled());
1707}
1708
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001709TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001710 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001711 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001712 constexpr int32_t eventHubId = 1;
1713 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001714 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001715 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001716 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001717 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001718
Yi Kong9b14ac62018-07-17 13:48:38 -07001719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001720
1721 NotifyDeviceResetArgs resetArgs;
1722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001723 ASSERT_EQ(deviceId, resetArgs.deviceId);
1724
1725 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001726 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001727 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001728
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001730 ASSERT_EQ(deviceId, resetArgs.deviceId);
1731 ASSERT_EQ(device->isEnabled(), false);
1732
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001733 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001734 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001737 ASSERT_EQ(device->isEnabled(), false);
1738
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001739 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001740 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001742 ASSERT_EQ(deviceId, resetArgs.deviceId);
1743 ASSERT_EQ(device->isEnabled(), true);
1744}
1745
Michael Wrightd02c5b62014-02-10 15:10:22 -08001746TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001747 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001748 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001749 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001750 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001751 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001752 AINPUT_SOURCE_KEYBOARD, nullptr);
1753 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001754
1755 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1756 AINPUT_SOURCE_ANY, AKEYCODE_A))
1757 << "Should return unknown when the device id is >= 0 but unknown.";
1758
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001759 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1760 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1761 << "Should return unknown when the device id is valid but the sources are not "
1762 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001763
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001764 ASSERT_EQ(AKEY_STATE_DOWN,
1765 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1766 AKEYCODE_A))
1767 << "Should return value provided by mapper when device id is valid and the device "
1768 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001769
1770 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1771 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1772 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1773
1774 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1775 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1776 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1777}
1778
Philip Junker4af3b3d2021-12-14 10:36:55 +01001779TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1780 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1781 constexpr int32_t eventHubId = 1;
1782 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1783 InputDeviceClass::KEYBOARD,
1784 AINPUT_SOURCE_KEYBOARD, nullptr);
1785 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1786
1787 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1788 << "Should return unknown when the device with the specified id is not found.";
1789
1790 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1791 << "Should return correct mapping when device id is valid and mapping exists.";
1792
1793 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1794 << "Should return the location key code when device id is valid and there's no "
1795 "mapping.";
1796}
1797
1798TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1799 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1800 constexpr int32_t eventHubId = 1;
1801 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1802 InputDeviceClass::JOYSTICK,
1803 AINPUT_SOURCE_GAMEPAD, nullptr);
1804 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1805
1806 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1807 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1808}
1809
Michael Wrightd02c5b62014-02-10 15:10:22 -08001810TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001811 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001812 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001813 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001814 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001815 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001816 AINPUT_SOURCE_KEYBOARD, nullptr);
1817 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001818
1819 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1820 AINPUT_SOURCE_ANY, KEY_A))
1821 << "Should return unknown when the device id is >= 0 but unknown.";
1822
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001823 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1824 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1825 << "Should return unknown when the device id is valid but the sources are not "
1826 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001827
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001828 ASSERT_EQ(AKEY_STATE_DOWN,
1829 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1830 KEY_A))
1831 << "Should return value provided by mapper when device id is valid and the device "
1832 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001833
1834 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1835 AINPUT_SOURCE_TRACKBALL, KEY_A))
1836 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1837
1838 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1839 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1840 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1841}
1842
1843TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001844 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001845 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001846 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001847 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001848 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001849 AINPUT_SOURCE_KEYBOARD, nullptr);
1850 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001851
1852 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1853 AINPUT_SOURCE_ANY, SW_LID))
1854 << "Should return unknown when the device id is >= 0 but unknown.";
1855
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001856 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1857 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1858 << "Should return unknown when the device id is valid but the sources are not "
1859 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001860
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001861 ASSERT_EQ(AKEY_STATE_DOWN,
1862 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1863 SW_LID))
1864 << "Should return value provided by mapper when device id is valid and the device "
1865 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001866
1867 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1868 AINPUT_SOURCE_TRACKBALL, SW_LID))
1869 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1870
1871 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1872 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1873 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1874}
1875
1876TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001877 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001878 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001879 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001880 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001881 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001882 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001883
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001884 mapper.addSupportedKeyCode(AKEYCODE_A);
1885 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001886
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001887 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001888 uint8_t flags[4] = { 0, 0, 0, 1 };
1889
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001890 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08001891 << "Should return false when device id is >= 0 but unknown.";
1892 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1893
1894 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001895 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001896 << "Should return false when device id is valid but the sources are not supported by "
1897 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001898 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1899
1900 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001901 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001902 keyCodes, flags))
1903 << "Should return value provided by mapper when device id is valid and the device "
1904 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001905 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1906
1907 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001908 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1909 << "Should return false when the device id is < 0 but the sources are not supported by "
1910 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001911 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1912
1913 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001914 ASSERT_TRUE(
1915 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1916 << "Should return value provided by mapper when device id is < 0 and one of the "
1917 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001918 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1919}
1920
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001921TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001922 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001923 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001924
1925 NotifyConfigurationChangedArgs args;
1926
1927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1928 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1929}
1930
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001931TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001932 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001933 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001934 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001935 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001936 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001937 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001938 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001939 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001940
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001941 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001942 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001943 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1944
1945 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001946 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001947 ASSERT_EQ(when, event.when);
1948 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001949 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001950 ASSERT_EQ(EV_KEY, event.type);
1951 ASSERT_EQ(KEY_A, event.code);
1952 ASSERT_EQ(1, event.value);
1953}
1954
Garfield Tan1c7bc862020-01-28 13:24:04 -08001955TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001956 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001957 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001958 constexpr int32_t eventHubId = 1;
1959 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001960 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001961 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001962 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001963 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001964
1965 NotifyDeviceResetArgs resetArgs;
1966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001967 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001968
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001969 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001970 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001972 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001973 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001974
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001975 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001976 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001978 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001979 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001980
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001981 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001982 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001984 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001985 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001986}
1987
Garfield Tan1c7bc862020-01-28 13:24:04 -08001988TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1989 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001990 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001991 constexpr int32_t eventHubId = 1;
1992 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1993 // Must add at least one mapper or the device will be ignored!
1994 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001995 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001996 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1997
1998 NotifyDeviceResetArgs resetArgs;
1999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2000 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
2001}
2002
Arthur Hungc23540e2018-11-29 20:42:11 +08002003TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002004 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002005 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002006 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08002007 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002008 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2009 FakeInputMapper& mapper =
2010 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002011 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08002012
2013 const uint8_t hdmi1 = 1;
2014
2015 // Associated touch screen with second display.
2016 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2017
2018 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00002019 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08002020 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002021 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002022 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002023 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002024 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002025 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002026 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002027 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002028
2029 // Add the device, and make sure all of the callbacks are triggered.
2030 // The device is added after the input port associations are processed since
2031 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002032 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002035 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002036
Arthur Hung2c9a3342019-07-23 14:18:59 +08002037 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002038 ASSERT_EQ(deviceId, device->getId());
2039 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2040 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002041
2042 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002043 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002044 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002045 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002046}
2047
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002048TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2049 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002050 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002051 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2052 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2053 // Must add at least one mapper or the device will be ignored!
2054 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2055 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2056 mReader->pushNextDevice(device);
2057 mReader->pushNextDevice(device);
2058 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2059 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2060
2061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2062
2063 NotifyDeviceResetArgs resetArgs;
2064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2065 ASSERT_EQ(deviceId, resetArgs.deviceId);
2066 ASSERT_TRUE(device->isEnabled());
2067 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2068 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2069
2070 disableDevice(deviceId);
2071 mReader->loopOnce();
2072
2073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2074 ASSERT_EQ(deviceId, resetArgs.deviceId);
2075 ASSERT_FALSE(device->isEnabled());
2076 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2077 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2078
2079 enableDevice(deviceId);
2080 mReader->loopOnce();
2081
2082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2083 ASSERT_EQ(deviceId, resetArgs.deviceId);
2084 ASSERT_TRUE(device->isEnabled());
2085 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2086 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2087}
2088
2089TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2090 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002091 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002092 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2093 // Add two subdevices to device
2094 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2095 FakeInputMapper& mapperDevice1 =
2096 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2097 FakeInputMapper& mapperDevice2 =
2098 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2099 mReader->pushNextDevice(device);
2100 mReader->pushNextDevice(device);
2101 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2102 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2103
2104 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2105 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2106
2107 ASSERT_EQ(AKEY_STATE_DOWN,
2108 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2109 ASSERT_EQ(AKEY_STATE_DOWN,
2110 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2111 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2112 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2113}
2114
Prabir Pradhan7e186182020-11-10 13:56:45 -08002115TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2116 NotifyPointerCaptureChangedArgs args;
2117
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002118 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002119 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2120 mReader->loopOnce();
2121 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002122 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2123 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002124
2125 mFakePolicy->setPointerCapture(false);
2126 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2127 mReader->loopOnce();
2128 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002129 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002130
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002131 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002132 // does not change.
2133 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2134 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002135 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002136}
2137
Chris Ye87143712020-11-10 05:05:58 +00002138class FakeVibratorInputMapper : public FakeInputMapper {
2139public:
2140 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2141 : FakeInputMapper(deviceContext, sources) {}
2142
2143 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2144};
2145
2146TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2147 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002148 ftl::Flags<InputDeviceClass> deviceClass =
2149 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002150 constexpr int32_t eventHubId = 1;
2151 const char* DEVICE_LOCATION = "BLUETOOTH";
2152 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2153 FakeVibratorInputMapper& mapper =
2154 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2155 mReader->pushNextDevice(device);
2156
2157 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2158 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2159
2160 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2161 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2162}
2163
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002164// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002165
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002166class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002167public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002168 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002169
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002170 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002171
Andy Chenf9f1a022022-08-29 20:07:10 -04002172 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
2173
Chris Yee2b1e5c2021-03-10 22:45:12 -08002174 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2175
2176 void dump(std::string& dump) override {}
2177
2178 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2179 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002180 }
2181
Chris Yee2b1e5c2021-03-10 22:45:12 -08002182 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2183 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002184 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002185
2186 bool setLightColor(int32_t lightId, int32_t color) override {
2187 getDeviceContext().setLightBrightness(lightId, color >> 24);
2188 return true;
2189 }
2190
2191 std::optional<int32_t> getLightColor(int32_t lightId) override {
2192 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2193 if (!result.has_value()) {
2194 return std::nullopt;
2195 }
2196 return result.value() << 24;
2197 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002198
2199 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2200
2201 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2202
2203private:
2204 InputDeviceContext& mDeviceContext;
2205 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2206 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04002207 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002208};
2209
Chris Yee2b1e5c2021-03-10 22:45:12 -08002210TEST_F(InputReaderTest, BatteryGetCapacity) {
2211 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002212 ftl::Flags<InputDeviceClass> deviceClass =
2213 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002214 constexpr int32_t eventHubId = 1;
2215 const char* DEVICE_LOCATION = "BLUETOOTH";
2216 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002217 FakePeripheralController& controller =
2218 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002219 mReader->pushNextDevice(device);
2220
2221 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2222
2223 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2224 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2225}
2226
2227TEST_F(InputReaderTest, BatteryGetStatus) {
2228 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002229 ftl::Flags<InputDeviceClass> deviceClass =
2230 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002231 constexpr int32_t eventHubId = 1;
2232 const char* DEVICE_LOCATION = "BLUETOOTH";
2233 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002234 FakePeripheralController& controller =
2235 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002236 mReader->pushNextDevice(device);
2237
2238 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2239
2240 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2241 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2242}
2243
Prabir Pradhane287ecd2022-09-07 21:18:05 +00002244TEST_F(InputReaderTest, BatteryGetDevicePath) {
2245 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2246 ftl::Flags<InputDeviceClass> deviceClass =
2247 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2248 constexpr int32_t eventHubId = 1;
2249 const char* DEVICE_LOCATION = "BLUETOOTH";
2250 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2251 device->addController<FakePeripheralController>(eventHubId);
2252 mReader->pushNextDevice(device);
2253
2254 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2255
2256 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), BATTERY_DEVPATH);
2257}
2258
Chris Ye3fdbfef2021-01-06 18:45:18 -08002259TEST_F(InputReaderTest, LightGetColor) {
2260 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002261 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002262 constexpr int32_t eventHubId = 1;
2263 const char* DEVICE_LOCATION = "BLUETOOTH";
2264 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002265 FakePeripheralController& controller =
2266 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002267 mReader->pushNextDevice(device);
2268 RawLightInfo info = {.id = 1,
2269 .name = "Mono",
2270 .maxBrightness = 255,
2271 .flags = InputLightClass::BRIGHTNESS,
2272 .path = ""};
2273 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2274 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2275
2276 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002277
Chris Yee2b1e5c2021-03-10 22:45:12 -08002278 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2279 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002280 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2281 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2282}
2283
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002284// --- InputReaderIntegrationTest ---
2285
2286// These tests create and interact with the InputReader only through its interface.
2287// The InputReader is started during SetUp(), which starts its processing in its own
2288// thread. The tests use linux uinput to emulate input devices.
2289// NOTE: Interacting with the physical device while these tests are running may cause
2290// the tests to fail.
2291class InputReaderIntegrationTest : public testing::Test {
2292protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002293 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002294 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002295 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002296
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002297 std::shared_ptr<FakePointerController> mFakePointerController;
2298
Chris Yea52ade12020-08-27 16:49:20 -07002299 void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002300 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002301 mFakePointerController = std::make_shared<FakePointerController>();
2302 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002303 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2304 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002305
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002306 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2307 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002308 ASSERT_EQ(mReader->start(), OK);
2309
2310 // Since this test is run on a real device, all the input devices connected
2311 // to the test device will show up in mReader. We wait for those input devices to
2312 // show up before beginning the tests.
2313 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2314 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2315 }
2316
Chris Yea52ade12020-08-27 16:49:20 -07002317 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002318 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002319 mReader.reset();
2320 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002321 mFakePolicy.clear();
2322 }
2323};
2324
2325TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2326 // An invalid input device that is only used for this test.
2327 class InvalidUinputDevice : public UinputDevice {
2328 public:
2329 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2330
2331 private:
2332 void configureDevice(int fd, uinput_user_dev* device) override {}
2333 };
2334
2335 const size_t numDevices = mFakePolicy->getInputDevices().size();
2336
2337 // UinputDevice does not set any event or key bits, so InputReader should not
2338 // consider it as a valid device.
2339 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2340 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2341 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2342 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2343
2344 invalidDevice.reset();
2345 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2346 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2347 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2348}
2349
2350TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2351 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2352
2353 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2354 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2355 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2356 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2357
2358 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002359 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002360 const auto& it =
2361 std::find_if(inputDevices.begin(), inputDevices.end(),
2362 [&keyboard](const InputDeviceInfo& info) {
2363 return info.getIdentifier().name == keyboard->getName();
2364 });
2365
2366 ASSERT_NE(it, inputDevices.end());
2367 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2368 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2369 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002370
2371 keyboard.reset();
2372 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2373 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2374 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2375}
2376
2377TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2378 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2379 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2380
2381 NotifyConfigurationChangedArgs configChangedArgs;
2382 ASSERT_NO_FATAL_FAILURE(
2383 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002384 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002385 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2386
2387 NotifyKeyArgs keyArgs;
2388 keyboard->pressAndReleaseHomeKey();
2389 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2390 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002391 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002392 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002393 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002394 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002395 prevTimestamp = keyArgs.eventTime;
2396
2397 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2398 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002399 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002400 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002401 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002402}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002403
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002404/**
2405 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2406 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2407 * are passed to the listener.
2408 */
2409static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2410TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2411 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2412 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2413 NotifyKeyArgs keyArgs;
2414
2415 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2416 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2417 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2418 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2419
2420 controller->pressAndReleaseKey(BTN_GEAR_UP);
2421 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2422 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2423 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2424}
2425
Arthur Hungaab25622020-01-16 11:22:11 +08002426// --- TouchProcessTest ---
2427class TouchIntegrationTest : public InputReaderIntegrationTest {
2428protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002429 const std::string UNIQUE_ID = "local:0";
2430
Chris Yea52ade12020-08-27 16:49:20 -07002431 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002432 InputReaderIntegrationTest::SetUp();
2433 // At least add an internal display.
2434 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2435 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002436 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002437
2438 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2439 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2440 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2441 }
2442
2443 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2444 int32_t orientation, const std::string& uniqueId,
2445 std::optional<uint8_t> physicalPort,
2446 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002447 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2448 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002449 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2450 }
2451
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002452 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2453 NotifyMotionArgs args;
2454 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2455 EXPECT_EQ(action, args.action);
2456 ASSERT_EQ(points.size(), args.pointerCount);
2457 for (size_t i = 0; i < args.pointerCount; i++) {
2458 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2459 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2460 }
2461 }
2462
Arthur Hungaab25622020-01-16 11:22:11 +08002463 std::unique_ptr<UinputTouchScreen> mDevice;
2464};
2465
2466TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2467 NotifyMotionArgs args;
2468 const Point centerPoint = mDevice->getCenterPoint();
2469
2470 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002471 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002472 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002473 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002474 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2475 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2476
2477 // ACTION_MOVE
2478 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002479 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002480 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2482
2483 // ACTION_UP
2484 mDevice->sendUp();
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_UP, args.action);
2488}
2489
2490TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2491 NotifyMotionArgs args;
2492 const Point centerPoint = mDevice->getCenterPoint();
2493
2494 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002495 mDevice->sendSlot(FIRST_SLOT);
2496 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002497 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002498 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002499 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2500 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2501
2502 // ACTION_POINTER_DOWN (Second slot)
2503 const Point secondPoint = centerPoint + Point(100, 100);
2504 mDevice->sendSlot(SECOND_SLOT);
2505 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002506 mDevice->sendDown(secondPoint);
2507 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002508 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002509 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002510
2511 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002512 mDevice->sendMove(secondPoint + Point(1, 1));
2513 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002514 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2515 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2516
2517 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002518 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002519 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_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002522
2523 // ACTION_UP
2524 mDevice->sendSlot(FIRST_SLOT);
2525 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002526 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002527 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2528 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2529}
2530
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002531/**
2532 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2533 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2534 * data?
2535 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2536 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2537 * for Pointer 0 only is generated after.
2538 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2539 * events, we will not miss any information.
2540 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2541 * event generated afterwards that contains the newest movement of pointer 0.
2542 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2543 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2544 * losing information about non-palm pointers.
2545 */
2546TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2547 NotifyMotionArgs args;
2548 const Point centerPoint = mDevice->getCenterPoint();
2549
2550 // ACTION_DOWN
2551 mDevice->sendSlot(FIRST_SLOT);
2552 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2553 mDevice->sendDown(centerPoint);
2554 mDevice->sendSync();
2555 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2556
2557 // ACTION_POINTER_DOWN (Second slot)
2558 const Point secondPoint = centerPoint + Point(100, 100);
2559 mDevice->sendSlot(SECOND_SLOT);
2560 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2561 mDevice->sendDown(secondPoint);
2562 mDevice->sendSync();
2563 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2564
2565 // ACTION_MOVE (First slot)
2566 mDevice->sendSlot(FIRST_SLOT);
2567 mDevice->sendMove(centerPoint + Point(5, 5));
2568 // ACTION_POINTER_UP (Second slot)
2569 mDevice->sendSlot(SECOND_SLOT);
2570 mDevice->sendPointerUp();
2571 // Send a single sync for the above 2 pointer updates
2572 mDevice->sendSync();
2573
2574 // First, we should get POINTER_UP for the second pointer
2575 assertReceivedMotion(ACTION_POINTER_1_UP,
2576 {/*first pointer */ centerPoint + Point(5, 5),
2577 /*second pointer*/ secondPoint});
2578
2579 // Next, the MOVE event for the first pointer
2580 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2581}
2582
2583/**
2584 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2585 * move, and then it will go up, all in the same frame.
2586 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2587 * gets sent to the listener.
2588 */
2589TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2590 NotifyMotionArgs args;
2591 const Point centerPoint = mDevice->getCenterPoint();
2592
2593 // ACTION_DOWN
2594 mDevice->sendSlot(FIRST_SLOT);
2595 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2596 mDevice->sendDown(centerPoint);
2597 mDevice->sendSync();
2598 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2599
2600 // ACTION_POINTER_DOWN (Second slot)
2601 const Point secondPoint = centerPoint + Point(100, 100);
2602 mDevice->sendSlot(SECOND_SLOT);
2603 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2604 mDevice->sendDown(secondPoint);
2605 mDevice->sendSync();
2606 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2607
2608 // ACTION_MOVE (First slot)
2609 mDevice->sendSlot(FIRST_SLOT);
2610 mDevice->sendMove(centerPoint + Point(5, 5));
2611 // ACTION_POINTER_UP (Second slot)
2612 mDevice->sendSlot(SECOND_SLOT);
2613 mDevice->sendMove(secondPoint + Point(6, 6));
2614 mDevice->sendPointerUp();
2615 // Send a single sync for the above 2 pointer updates
2616 mDevice->sendSync();
2617
2618 // First, we should get POINTER_UP for the second pointer
2619 // The movement of the second pointer during the liftoff frame is ignored.
2620 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2621 assertReceivedMotion(ACTION_POINTER_1_UP,
2622 {/*first pointer */ centerPoint + Point(5, 5),
2623 /*second pointer*/ secondPoint});
2624
2625 // Next, the MOVE event for the first pointer
2626 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2627}
2628
Arthur Hungaab25622020-01-16 11:22:11 +08002629TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2630 NotifyMotionArgs args;
2631 const Point centerPoint = mDevice->getCenterPoint();
2632
2633 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002634 mDevice->sendSlot(FIRST_SLOT);
2635 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002636 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002637 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002638 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2639 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2640
arthurhungcc7f9802020-04-30 17:55:40 +08002641 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002642 const Point secondPoint = centerPoint + Point(100, 100);
2643 mDevice->sendSlot(SECOND_SLOT);
2644 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2645 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002646 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002647 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002648 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002649
arthurhungcc7f9802020-04-30 17:55:40 +08002650 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002651 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002652 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002653 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2654 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2655
arthurhungcc7f9802020-04-30 17:55:40 +08002656 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2657 // a palm event.
2658 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002659 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002660 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002661 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002662 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002663 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002664
arthurhungcc7f9802020-04-30 17:55:40 +08002665 // Send up to second slot, expect first slot send moving.
2666 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002667 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002668 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2669 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002670
arthurhungcc7f9802020-04-30 17:55:40 +08002671 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002672 mDevice->sendSlot(FIRST_SLOT);
2673 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002674 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002675
arthurhungcc7f9802020-04-30 17:55:40 +08002676 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2677 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002678}
2679
Michael Wrightd02c5b62014-02-10 15:10:22 -08002680// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002681class InputDeviceTest : public testing::Test {
2682protected:
2683 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002684 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002685 static const int32_t DEVICE_ID;
2686 static const int32_t DEVICE_GENERATION;
2687 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002688 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002689 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002690
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002691 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002692 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002693 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002694 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002695 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002696
Chris Yea52ade12020-08-27 16:49:20 -07002697 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002698 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002699 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002700 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002701 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002702 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002703 InputDeviceIdentifier identifier;
2704 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002705 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002706 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002707 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002708 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002709 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002710 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002711 }
2712
Chris Yea52ade12020-08-27 16:49:20 -07002713 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002714 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002715 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002716 }
2717};
2718
2719const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002720const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002721const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002722const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2723const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002724const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002725 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002726const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002727
2728TEST_F(InputDeviceTest, ImmutableProperties) {
2729 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002730 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002731 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002732}
2733
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002734TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
2735 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
2736
2737 // Configuration
2738 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2739 InputReaderConfiguration config;
2740 mDevice->configure(ARBITRARY_TIME, &config, 0);
2741
2742 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
2743}
2744
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002745TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2746 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002747}
2748
Michael Wrightd02c5b62014-02-10 15:10:22 -08002749TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2750 // Configuration.
2751 InputReaderConfiguration config;
2752 mDevice->configure(ARBITRARY_TIME, &config, 0);
2753
2754 // Reset.
2755 mDevice->reset(ARBITRARY_TIME);
2756
2757 NotifyDeviceResetArgs resetArgs;
2758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2759 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2760 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2761
2762 // Metadata.
2763 ASSERT_TRUE(mDevice->isIgnored());
2764 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2765
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002766 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002767 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002768 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002769 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2770 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2771
2772 // State queries.
2773 ASSERT_EQ(0, mDevice->getMetaState());
2774
2775 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2776 << "Ignored device should return unknown key code state.";
2777 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2778 << "Ignored device should return unknown scan code state.";
2779 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2780 << "Ignored device should return unknown switch state.";
2781
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002782 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002783 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002784 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002785 << "Ignored device should never mark any key codes.";
2786 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2787 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2788}
2789
2790TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2791 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002792 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08002793
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002794 FakeInputMapper& mapper1 =
2795 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002796 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2797 mapper1.setMetaState(AMETA_ALT_ON);
2798 mapper1.addSupportedKeyCode(AKEYCODE_A);
2799 mapper1.addSupportedKeyCode(AKEYCODE_B);
2800 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2801 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2802 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2803 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2804 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002805
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002806 FakeInputMapper& mapper2 =
2807 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002808 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002809
2810 InputReaderConfiguration config;
2811 mDevice->configure(ARBITRARY_TIME, &config, 0);
2812
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002813 std::string propertyValue;
2814 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002815 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002816 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002817
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002818 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2819 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002820
2821 // Reset
2822 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002823 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2824 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002825
2826 NotifyDeviceResetArgs resetArgs;
2827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2828 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2829 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2830
2831 // Metadata.
2832 ASSERT_FALSE(mDevice->isIgnored());
2833 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2834
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002835 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002836 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002837 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002838 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2839 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2840
2841 // State queries.
2842 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2843 << "Should query mappers and combine meta states.";
2844
2845 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2846 << "Should return unknown key code state when source not supported.";
2847 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2848 << "Should return unknown scan code state when source not supported.";
2849 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2850 << "Should return unknown switch state when source not supported.";
2851
2852 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2853 << "Should query mapper when source is supported.";
2854 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2855 << "Should query mapper when source is supported.";
2856 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2857 << "Should query mapper when source is supported.";
2858
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002859 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002860 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002861 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002862 << "Should do nothing when source is unsupported.";
2863 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2864 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2865 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2866 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2867
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002868 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002869 << "Should query mapper when source is supported.";
2870 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2871 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2872 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2873 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2874
2875 // Event handling.
2876 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002877 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002878 mDevice->process(&event, 1);
2879
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002880 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2881 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002882}
2883
Arthur Hung2c9a3342019-07-23 14:18:59 +08002884// A single input device is associated with a specific display. Check that:
2885// 1. Device is disabled if the viewport corresponding to the associated display is not found
2886// 2. Device is disabled when setEnabled API is called
2887TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002888 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002889
2890 // First Configuration.
2891 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2892
2893 // Device should be enabled by default.
2894 ASSERT_TRUE(mDevice->isEnabled());
2895
2896 // Prepare associated info.
2897 constexpr uint8_t hdmi = 1;
2898 const std::string UNIQUE_ID = "local:1";
2899
2900 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2901 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2902 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2903 // Device should be disabled because it is associated with a specific display via
2904 // input port <-> display port association, but the corresponding display is not found
2905 ASSERT_FALSE(mDevice->isEnabled());
2906
2907 // Prepare displays.
2908 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002909 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2910 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002911 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2912 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2913 ASSERT_TRUE(mDevice->isEnabled());
2914
2915 // Device should be disabled after set disable.
2916 mFakePolicy->addDisabledDevice(mDevice->getId());
2917 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2918 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2919 ASSERT_FALSE(mDevice->isEnabled());
2920
2921 // Device should still be disabled even found the associated display.
2922 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2923 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2924 ASSERT_FALSE(mDevice->isEnabled());
2925}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002926
Christine Franks1ba71cc2021-04-07 14:37:42 -07002927TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2928 // Device should be enabled by default.
2929 mFakePolicy->clearViewports();
2930 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2931 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2932 ASSERT_TRUE(mDevice->isEnabled());
2933
2934 // Device should be disabled because it is associated with a specific display, but the
2935 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002936 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002937 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2938 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2939 ASSERT_FALSE(mDevice->isEnabled());
2940
2941 // Device should be enabled when a display is found.
2942 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2943 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2944 NO_PORT, ViewportType::INTERNAL);
2945 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2946 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2947 ASSERT_TRUE(mDevice->isEnabled());
2948
2949 // Device should be disabled after set disable.
2950 mFakePolicy->addDisabledDevice(mDevice->getId());
2951 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2952 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2953 ASSERT_FALSE(mDevice->isEnabled());
2954
2955 // Device should still be disabled even found the associated display.
2956 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2957 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2958 ASSERT_FALSE(mDevice->isEnabled());
2959}
2960
Christine Franks2a2293c2022-01-18 11:51:16 -08002961TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2962 mFakePolicy->clearViewports();
2963 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2964 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2965
Christine Franks2a2293c2022-01-18 11:51:16 -08002966 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2967 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2968 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2969 NO_PORT, ViewportType::INTERNAL);
2970 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2971 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2972 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2973}
2974
Michael Wrightd02c5b62014-02-10 15:10:22 -08002975// --- InputMapperTest ---
2976
2977class InputMapperTest : public testing::Test {
2978protected:
2979 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002980 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002981 static const int32_t DEVICE_ID;
2982 static const int32_t DEVICE_GENERATION;
2983 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002984 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002985 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002986
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002987 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002988 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002989 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002990 std::unique_ptr<InstrumentedInputReader> mReader;
2991 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002992
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002993 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002994 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002995 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002996 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002997 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002998 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002999 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003000 // Consume the device reset notification generated when adding a new device.
3001 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003002 }
3003
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003004 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003005 SetUp(DEVICE_CLASSES);
3006 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003007
Chris Yea52ade12020-08-27 16:49:20 -07003008 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003009 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003010 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003011 }
3012
3013 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003014 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003015 }
3016
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003017 void configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00003018 if (!changes ||
3019 (changes &
3020 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
3021 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003022 mReader->requestRefreshConfiguration(changes);
3023 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08003024 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003025 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003026 // Loop the reader to flush the input listener queue.
3027 mReader->loopOnce();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003028 }
3029
arthurhungdcef2dc2020-08-11 14:47:50 +08003030 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
3031 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003032 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003033 InputDeviceIdentifier identifier;
3034 identifier.name = name;
3035 identifier.location = location;
3036 std::shared_ptr<InputDevice> device =
3037 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
3038 identifier);
3039 mReader->pushNextDevice(device);
3040 mFakeEventHub->addDevice(eventHubId, name, classes);
3041 mReader->loopOnce();
3042 return device;
3043 }
3044
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003045 template <class T, typename... Args>
3046 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003047 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003048 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003049 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07003050 mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003051 // Loop the reader to flush the input listener queue.
3052 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003053 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003054 }
3055
3056 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003057 int32_t orientation, const std::string& uniqueId,
3058 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003059 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3060 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003061 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3062 }
3063
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003064 void clearViewports() {
3065 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003066 }
3067
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003068 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
3069 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003070 RawEvent event;
3071 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003072 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003073 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003074 event.type = type;
3075 event.code = code;
3076 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003077 mapper.process(&event);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003078 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003079 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003080 }
3081
3082 static void assertMotionRange(const InputDeviceInfo& info,
3083 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3084 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003085 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3087 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3088 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3089 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3090 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3091 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3092 }
3093
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003094 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3095 float size, float touchMajor, float touchMinor, float toolMajor,
3096 float toolMinor, float orientation, float distance,
3097 float scaledAxisEpsilon = 1.f) {
3098 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3099 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3101 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003102 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3103 scaledAxisEpsilon);
3104 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3105 scaledAxisEpsilon);
3106 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3107 scaledAxisEpsilon);
3108 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3109 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003110 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3111 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3112 }
3113
Michael Wright17db18e2020-06-26 20:51:44 +01003114 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003115 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003116 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003117 ASSERT_NEAR(x, actualX, 1);
3118 ASSERT_NEAR(y, actualY, 1);
3119 }
3120};
3121
3122const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003123const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003124const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3126const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003127const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3128 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003129const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003130
3131// --- SwitchInputMapperTest ---
3132
3133class SwitchInputMapperTest : public InputMapperTest {
3134protected:
3135};
3136
3137TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003138 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003139
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003140 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003141}
3142
3143TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003144 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003145
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003146 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003147 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003148
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003149 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003150 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003151}
3152
3153TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003154 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003155
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003156 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3157 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3158 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3159 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003160
3161 NotifySwitchArgs args;
3162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3163 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003164 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3165 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003166 args.switchMask);
3167 ASSERT_EQ(uint32_t(0), args.policyFlags);
3168}
3169
Chris Ye87143712020-11-10 05:05:58 +00003170// --- VibratorInputMapperTest ---
3171class VibratorInputMapperTest : public InputMapperTest {
3172protected:
3173 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3174};
3175
3176TEST_F(VibratorInputMapperTest, GetSources) {
3177 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3178
3179 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3180}
3181
3182TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3183 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3184
3185 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3186}
3187
3188TEST_F(VibratorInputMapperTest, Vibrate) {
3189 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003190 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003191 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3192
3193 VibrationElement pattern(2);
3194 VibrationSequence sequence(2);
3195 pattern.duration = std::chrono::milliseconds(200);
3196 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3197 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3198 sequence.addElement(pattern);
3199 pattern.duration = std::chrono::milliseconds(500);
3200 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3201 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3202 sequence.addElement(pattern);
3203
3204 std::vector<int64_t> timings = {0, 1};
3205 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3206
3207 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003208 // Start vibrating
3209 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003210 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003211 // Verify vibrator state listener was notified.
3212 mReader->loopOnce();
3213 NotifyVibratorStateArgs args;
3214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3215 ASSERT_EQ(DEVICE_ID, args.deviceId);
3216 ASSERT_TRUE(args.isOn);
3217 // Stop vibrating
3218 mapper.cancelVibrate(VIBRATION_TOKEN);
3219 ASSERT_FALSE(mapper.isVibrating());
3220 // Verify vibrator state listener was notified.
3221 mReader->loopOnce();
3222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3223 ASSERT_EQ(DEVICE_ID, args.deviceId);
3224 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003225}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003226
Chris Yef59a2f42020-10-16 12:55:26 -07003227// --- SensorInputMapperTest ---
3228
3229class SensorInputMapperTest : public InputMapperTest {
3230protected:
3231 static const int32_t ACCEL_RAW_MIN;
3232 static const int32_t ACCEL_RAW_MAX;
3233 static const int32_t ACCEL_RAW_FUZZ;
3234 static const int32_t ACCEL_RAW_FLAT;
3235 static const int32_t ACCEL_RAW_RESOLUTION;
3236
3237 static const int32_t GYRO_RAW_MIN;
3238 static const int32_t GYRO_RAW_MAX;
3239 static const int32_t GYRO_RAW_FUZZ;
3240 static const int32_t GYRO_RAW_FLAT;
3241 static const int32_t GYRO_RAW_RESOLUTION;
3242
3243 static const float GRAVITY_MS2_UNIT;
3244 static const float DEGREE_RADIAN_UNIT;
3245
3246 void prepareAccelAxes();
3247 void prepareGyroAxes();
3248 void setAccelProperties();
3249 void setGyroProperties();
3250 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3251};
3252
3253const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3254const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3255const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3256const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3257const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3258
3259const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3260const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3261const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3262const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3263const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3264
3265const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3266const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3267
3268void SensorInputMapperTest::prepareAccelAxes() {
3269 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3270 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3271 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3272 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3273 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3274 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3275}
3276
3277void SensorInputMapperTest::prepareGyroAxes() {
3278 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3279 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3280 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3281 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3282 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3283 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3284}
3285
3286void SensorInputMapperTest::setAccelProperties() {
3287 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3288 /* sensorDataIndex */ 0);
3289 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3290 /* sensorDataIndex */ 1);
3291 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3292 /* sensorDataIndex */ 2);
3293 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3294 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3295 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3296 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3297 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3298}
3299
3300void SensorInputMapperTest::setGyroProperties() {
3301 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3302 /* sensorDataIndex */ 0);
3303 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3304 /* sensorDataIndex */ 1);
3305 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3306 /* sensorDataIndex */ 2);
3307 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3308 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3309 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3310 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3311 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3312}
3313
3314TEST_F(SensorInputMapperTest, GetSources) {
3315 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3316
3317 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3318}
3319
3320TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3321 setAccelProperties();
3322 prepareAccelAxes();
3323 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3324
3325 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3326 std::chrono::microseconds(10000),
3327 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003328 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003329 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3330 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3331 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3332 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3333 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003334
3335 NotifySensorArgs args;
3336 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3337 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3338 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3339
3340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3341 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3342 ASSERT_EQ(args.deviceId, DEVICE_ID);
3343 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3344 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3345 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3346 ASSERT_EQ(args.values, values);
3347 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3348}
3349
3350TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3351 setGyroProperties();
3352 prepareGyroAxes();
3353 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3354
3355 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3356 std::chrono::microseconds(10000),
3357 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003358 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003359 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3360 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3361 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3362 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3363 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003364
3365 NotifySensorArgs args;
3366 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3367 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3368 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3369
3370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3371 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3372 ASSERT_EQ(args.deviceId, DEVICE_ID);
3373 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3374 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3375 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3376 ASSERT_EQ(args.values, values);
3377 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3378}
3379
Michael Wrightd02c5b62014-02-10 15:10:22 -08003380// --- KeyboardInputMapperTest ---
3381
3382class KeyboardInputMapperTest : public InputMapperTest {
3383protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003384 const std::string UNIQUE_ID = "local:0";
3385
3386 void prepareDisplay(int32_t orientation);
3387
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003388 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003389 int32_t originalKeyCode, int32_t rotatedKeyCode,
3390 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003391};
3392
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003393/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3394 * orientation.
3395 */
3396void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003397 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3398 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003399}
3400
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003401void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003402 int32_t originalScanCode, int32_t originalKeyCode,
3403 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003404 NotifyKeyArgs args;
3405
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003406 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3408 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3409 ASSERT_EQ(originalScanCode, args.scanCode);
3410 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003411 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003412
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003413 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3415 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3416 ASSERT_EQ(originalScanCode, args.scanCode);
3417 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003418 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003419}
3420
Michael Wrightd02c5b62014-02-10 15:10:22 -08003421TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003422 KeyboardInputMapper& mapper =
3423 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3424 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003425
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003426 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003427}
3428
3429TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3430 const int32_t USAGE_A = 0x070004;
3431 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003432 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3433 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003434 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3435 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3436 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003437
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003438 KeyboardInputMapper& mapper =
3439 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3440 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003441 // Initial metastate is AMETA_NONE.
3442 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003443
3444 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003445 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003446 NotifyKeyArgs args;
3447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3448 ASSERT_EQ(DEVICE_ID, args.deviceId);
3449 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3450 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3451 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3452 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3453 ASSERT_EQ(KEY_HOME, args.scanCode);
3454 ASSERT_EQ(AMETA_NONE, args.metaState);
3455 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3456 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3457 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3458
3459 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003460 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3462 ASSERT_EQ(DEVICE_ID, args.deviceId);
3463 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3464 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3465 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3466 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3467 ASSERT_EQ(KEY_HOME, args.scanCode);
3468 ASSERT_EQ(AMETA_NONE, args.metaState);
3469 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3470 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3471 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3472
3473 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003474 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3477 ASSERT_EQ(DEVICE_ID, args.deviceId);
3478 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3479 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3480 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3481 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3482 ASSERT_EQ(0, args.scanCode);
3483 ASSERT_EQ(AMETA_NONE, args.metaState);
3484 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3485 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3486 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3487
3488 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003489 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3490 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3492 ASSERT_EQ(DEVICE_ID, args.deviceId);
3493 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3494 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3495 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3496 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3497 ASSERT_EQ(0, args.scanCode);
3498 ASSERT_EQ(AMETA_NONE, args.metaState);
3499 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3500 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3501 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3502
3503 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003504 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3505 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3507 ASSERT_EQ(DEVICE_ID, args.deviceId);
3508 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3509 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3510 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3511 ASSERT_EQ(0, args.keyCode);
3512 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3513 ASSERT_EQ(AMETA_NONE, args.metaState);
3514 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3515 ASSERT_EQ(0U, args.policyFlags);
3516 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3517
3518 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3520 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3522 ASSERT_EQ(DEVICE_ID, args.deviceId);
3523 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3524 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3525 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3526 ASSERT_EQ(0, args.keyCode);
3527 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3528 ASSERT_EQ(AMETA_NONE, args.metaState);
3529 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3530 ASSERT_EQ(0U, args.policyFlags);
3531 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3532}
3533
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003534/**
3535 * Ensure that the readTime is set to the time when the EV_KEY is received.
3536 */
3537TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3538 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3539
3540 KeyboardInputMapper& mapper =
3541 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3542 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3543 NotifyKeyArgs args;
3544
3545 // Key down
3546 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3548 ASSERT_EQ(12, args.readTime);
3549
3550 // Key up
3551 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3553 ASSERT_EQ(15, args.readTime);
3554}
3555
Michael Wrightd02c5b62014-02-10 15:10:22 -08003556TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003557 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3558 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003559 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3560 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3561 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003562
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003563 KeyboardInputMapper& mapper =
3564 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3565 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003566
Arthur Hung95f68612022-04-07 14:08:22 +08003567 // Initial metastate is AMETA_NONE.
3568 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003569
3570 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003571 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003572 NotifyKeyArgs args;
3573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3574 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003575 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003576 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003577
3578 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003579 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3581 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003582 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003583
3584 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003585 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3587 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003588 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003589
3590 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003591 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3593 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003594 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003595 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003596}
3597
3598TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003599 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3600 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3601 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3602 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003603
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003604 KeyboardInputMapper& mapper =
3605 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3606 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003607
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003608 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003609 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3610 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3611 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3612 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3613 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3614 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3615 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3616 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3617}
3618
3619TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003620 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3621 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3622 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3623 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003624
Michael Wrightd02c5b62014-02-10 15:10:22 -08003625 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003626 KeyboardInputMapper& mapper =
3627 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3628 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003629
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003630 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003631 ASSERT_NO_FATAL_FAILURE(
3632 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3633 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3634 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3635 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3636 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3637 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3638 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003639
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003640 clearViewports();
3641 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003642 ASSERT_NO_FATAL_FAILURE(
3643 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3644 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3645 AKEYCODE_DPAD_UP, DISPLAY_ID));
3646 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3647 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3648 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3649 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003650
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003651 clearViewports();
3652 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003653 ASSERT_NO_FATAL_FAILURE(
3654 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3655 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3656 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3657 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3658 AKEYCODE_DPAD_UP, DISPLAY_ID));
3659 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3660 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003661
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003662 clearViewports();
3663 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003664 ASSERT_NO_FATAL_FAILURE(
3665 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3666 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3667 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3668 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3669 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3670 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3671 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003672
3673 // Special case: if orientation changes while key is down, we still emit the same keycode
3674 // in the key up as we did in the key down.
3675 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003676 clearViewports();
3677 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003678 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3680 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3681 ASSERT_EQ(KEY_UP, args.scanCode);
3682 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3683
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003684 clearViewports();
3685 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3688 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3689 ASSERT_EQ(KEY_UP, args.scanCode);
3690 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3691}
3692
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003693TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3694 // If the keyboard is not orientation aware,
3695 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003696 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003697
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003698 KeyboardInputMapper& mapper =
3699 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3700 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003701 NotifyKeyArgs args;
3702
3703 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003704 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003706 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3708 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3709
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003710 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003711 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003713 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3715 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3716}
3717
3718TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3719 // If the keyboard is orientation aware,
3720 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003721 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003722
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003723 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003724 KeyboardInputMapper& mapper =
3725 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3726 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003727 NotifyKeyArgs args;
3728
3729 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3730 // ^--- already checked by the previous test
3731
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003732 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003733 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003734 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003736 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3738 ASSERT_EQ(DISPLAY_ID, args.displayId);
3739
3740 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003741 clearViewports();
3742 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003743 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003744 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003746 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3748 ASSERT_EQ(newDisplayId, args.displayId);
3749}
3750
Michael Wrightd02c5b62014-02-10 15:10:22 -08003751TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003752 KeyboardInputMapper& mapper =
3753 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3754 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003755
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003756 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003757 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003758
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003759 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003760 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003761}
3762
Philip Junker4af3b3d2021-12-14 10:36:55 +01003763TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3764 KeyboardInputMapper& mapper =
3765 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3766 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3767
3768 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3769 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3770 << "If a mapping is available, the result is equal to the mapping";
3771
3772 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3773 << "If no mapping is available, the result is the key location";
3774}
3775
Michael Wrightd02c5b62014-02-10 15:10:22 -08003776TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003777 KeyboardInputMapper& mapper =
3778 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3779 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003780
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003781 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003782 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003783
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003784 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003785 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003786}
3787
3788TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003789 KeyboardInputMapper& mapper =
3790 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3791 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003792
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003793 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003794
Michael Wrightd02c5b62014-02-10 15:10:22 -08003795 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003796 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003797 ASSERT_TRUE(flags[0]);
3798 ASSERT_FALSE(flags[1]);
3799}
3800
3801TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003802 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3803 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3804 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3805 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3806 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3807 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003808
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003809 KeyboardInputMapper& mapper =
3810 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3811 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003812 // Initial metastate is AMETA_NONE.
3813 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003814
3815 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003816 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3817 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3818 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003819
3820 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003821 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3822 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003823 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3824 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3825 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003826 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003827
3828 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003831 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3832 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3833 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003834 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003835
3836 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003837 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3838 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003839 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3840 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3841 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003842 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003843
3844 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003845 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3846 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003847 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3848 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3849 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003850 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003851
3852 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003853 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3854 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003855 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3856 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3857 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003858 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003859
3860 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003861 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3862 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003863 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3864 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3865 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003866 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003867}
3868
Chris Yea52ade12020-08-27 16:49:20 -07003869TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3870 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3871 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3872 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3873 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3874
3875 KeyboardInputMapper& mapper =
3876 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3877 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3878
Chris Yea52ade12020-08-27 16:49:20 -07003879 // Meta state should be AMETA_NONE after reset
3880 mapper.reset(ARBITRARY_TIME);
3881 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3882 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3883 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3884 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3885
3886 NotifyKeyArgs args;
3887 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003888 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3890 ASSERT_EQ(AMETA_NONE, args.metaState);
3891 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3892 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3893 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3894
3895 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003896 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3898 ASSERT_EQ(AMETA_NONE, args.metaState);
3899 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3900 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3901 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3902}
3903
Arthur Hung2c9a3342019-07-23 14:18:59 +08003904TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3905 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003906 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3907 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3908 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3909 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003910
3911 // keyboard 2.
3912 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003913 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003914 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003915 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003916 std::shared_ptr<InputDevice> device2 =
3917 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003918 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003919
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003920 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3921 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3922 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3923 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003924
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003925 KeyboardInputMapper& mapper =
3926 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3927 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003928
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003929 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003930 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003931 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003932 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3933 device2->reset(ARBITRARY_TIME);
3934
3935 // Prepared displays and associated info.
3936 constexpr uint8_t hdmi1 = 0;
3937 constexpr uint8_t hdmi2 = 1;
3938 const std::string SECONDARY_UNIQUE_ID = "local:1";
3939
3940 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3941 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3942
3943 // No associated display viewport found, should disable the device.
3944 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3945 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3946 ASSERT_FALSE(device2->isEnabled());
3947
3948 // Prepare second display.
3949 constexpr int32_t newDisplayId = 2;
3950 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003951 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003952 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003953 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003954 // Default device will reconfigure above, need additional reconfiguration for another device.
3955 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3956 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3957
3958 // Device should be enabled after the associated display is found.
3959 ASSERT_TRUE(mDevice->isEnabled());
3960 ASSERT_TRUE(device2->isEnabled());
3961
3962 // Test pad key events
3963 ASSERT_NO_FATAL_FAILURE(
3964 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3965 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3966 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3967 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3968 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3969 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3970 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3971
3972 ASSERT_NO_FATAL_FAILURE(
3973 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3974 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3975 AKEYCODE_DPAD_RIGHT, newDisplayId));
3976 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3977 AKEYCODE_DPAD_DOWN, newDisplayId));
3978 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3979 AKEYCODE_DPAD_LEFT, newDisplayId));
3980}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003981
arthurhungc903df12020-08-11 15:08:42 +08003982TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3983 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3984 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3985 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3986 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3987 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3988 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3989
3990 KeyboardInputMapper& mapper =
3991 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3992 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003993 // Initial metastate is AMETA_NONE.
3994 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003995
3996 // Initialization should have turned all of the lights off.
3997 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3998 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3999 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4000
4001 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004002 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4003 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004004 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4005 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4006
4007 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004008 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4009 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004010 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4011 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
4012
4013 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004014 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4015 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004016 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4017 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
4018
4019 mFakeEventHub->removeDevice(EVENTHUB_ID);
4020 mReader->loopOnce();
4021
4022 // keyboard 2 should default toggle keys.
4023 const std::string USB2 = "USB2";
4024 const std::string DEVICE_NAME2 = "KEYBOARD2";
4025 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4026 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4027 std::shared_ptr<InputDevice> device2 =
4028 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004029 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08004030 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4031 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4032 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4033 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4034 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4035 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4036
arthurhung6fe95782020-10-05 22:41:16 +08004037 KeyboardInputMapper& mapper2 =
4038 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4039 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08004040 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4041 device2->reset(ARBITRARY_TIME);
4042
4043 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4044 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4045 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004046 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4047 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004048}
4049
Arthur Hungcb40a002021-08-03 14:31:01 +00004050TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4051 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4052 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4053 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4054
4055 // Suppose we have two mappers. (DPAD + KEYBOARD)
4056 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4057 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4058 KeyboardInputMapper& mapper =
4059 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4060 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004061 // Initial metastate is AMETA_NONE.
4062 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004063
4064 mReader->toggleCapsLockState(DEVICE_ID);
4065 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4066}
4067
Arthur Hungfb3cc112022-04-13 07:39:50 +00004068TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4069 // keyboard 1.
4070 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4071 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4072 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4073 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4074 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4075 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4076
4077 KeyboardInputMapper& mapper1 =
4078 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4079 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4080
4081 // keyboard 2.
4082 const std::string USB2 = "USB2";
4083 const std::string DEVICE_NAME2 = "KEYBOARD2";
4084 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4085 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4086 std::shared_ptr<InputDevice> device2 =
4087 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4088 ftl::Flags<InputDeviceClass>(0));
4089 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4090 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4091 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4092 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4093 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4094 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4095
4096 KeyboardInputMapper& mapper2 =
4097 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4098 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4099 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4100 device2->reset(ARBITRARY_TIME);
4101
Arthur Hung95f68612022-04-07 14:08:22 +08004102 // Initial metastate is AMETA_NONE.
4103 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4104 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4105
4106 // Toggle num lock on and off.
4107 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4108 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004109 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4110 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4111 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4112
4113 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4114 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4115 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4116 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4117 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4118
4119 // Toggle caps lock on and off.
4120 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4121 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4122 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4123 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4124 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4125
4126 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4127 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4128 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4129 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4130 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4131
4132 // Toggle scroll lock on and off.
4133 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4134 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4135 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4136 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4137 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4138
4139 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4140 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4141 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4142 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4143 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4144}
4145
Arthur Hung2141d542022-08-23 07:45:21 +00004146TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4147 const int32_t USAGE_A = 0x070004;
4148 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4149 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4150
4151 KeyboardInputMapper& mapper =
4152 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4153 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4154 // Key down by scan code.
4155 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4156 NotifyKeyArgs args;
4157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4158 ASSERT_EQ(DEVICE_ID, args.deviceId);
4159 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4160 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4161 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4162 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4163 ASSERT_EQ(KEY_HOME, args.scanCode);
4164 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4165
4166 // Disable device, it should synthesize cancellation events for down events.
4167 mFakePolicy->addDisabledDevice(DEVICE_ID);
4168 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
4169
4170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4171 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4172 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4173 ASSERT_EQ(KEY_HOME, args.scanCode);
4174 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4175}
4176
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004177// --- KeyboardInputMapperTest_ExternalDevice ---
4178
4179class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4180protected:
Chris Yea52ade12020-08-27 16:49:20 -07004181 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004182};
4183
4184TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004185 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4186 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004187
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004188 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4189 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4190 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4191 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004192
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004193 KeyboardInputMapper& mapper =
4194 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4195 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004196
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004198 NotifyKeyArgs args;
4199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4200 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4201
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004202 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4204 ASSERT_EQ(uint32_t(0), args.policyFlags);
4205
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004206 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4208 ASSERT_EQ(uint32_t(0), args.policyFlags);
4209
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004210 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4212 ASSERT_EQ(uint32_t(0), args.policyFlags);
4213
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004214 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4216 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4217
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004218 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4220 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4221}
4222
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004223TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004224 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004225
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004226 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4227 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4228 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004229
Powei Fengd041c5d2019-05-03 17:11:33 -07004230 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004231 KeyboardInputMapper& mapper =
4232 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4233 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004234
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004235 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004236 NotifyKeyArgs args;
4237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4238 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4239
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004240 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4242 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4243
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004244 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4246 ASSERT_EQ(uint32_t(0), args.policyFlags);
4247
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004248 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4250 ASSERT_EQ(uint32_t(0), args.policyFlags);
4251
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004252 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4254 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4255
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004256 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4258 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4259}
4260
Michael Wrightd02c5b62014-02-10 15:10:22 -08004261// --- CursorInputMapperTest ---
4262
4263class CursorInputMapperTest : public InputMapperTest {
4264protected:
4265 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4266
Michael Wright17db18e2020-06-26 20:51:44 +01004267 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004268
Chris Yea52ade12020-08-27 16:49:20 -07004269 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004270 InputMapperTest::SetUp();
4271
Michael Wright17db18e2020-06-26 20:51:44 +01004272 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004273 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274 }
4275
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004276 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4277 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004278
4279 void prepareDisplay(int32_t orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004280 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4281 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4282 }
4283
4284 void prepareSecondaryDisplay() {
4285 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4286 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
4287 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004288 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004289
4290 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4291 float pressure) {
4292 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4293 0.0f, 0.0f, 0.0f, EPSILON));
4294 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295};
4296
4297const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4298
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004299void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4300 int32_t originalY, int32_t rotatedX,
4301 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004302 NotifyMotionArgs args;
4303
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004304 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4305 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4306 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4308 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004309 ASSERT_NO_FATAL_FAILURE(
4310 assertCursorPointerCoords(args.pointerCoords[0],
4311 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4312 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004313}
4314
4315TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004316 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004317 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004318
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004319 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004320}
4321
4322TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004323 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004324 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004325
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004326 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004327}
4328
4329TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004330 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004331 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332
4333 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004334 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004335
4336 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004337 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4338 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004339 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4340 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4341
4342 // When the bounds are set, then there should be a valid motion range.
4343 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4344
4345 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004346 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004347
4348 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4349 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4350 1, 800 - 1, 0.0f, 0.0f));
4351 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4352 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4353 2, 480 - 1, 0.0f, 0.0f));
4354 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4355 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4356 0.0f, 1.0f, 0.0f, 0.0f));
4357}
4358
4359TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004360 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004361 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004362
4363 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004364 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004365
4366 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4367 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4368 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4369 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4370 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4371 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4372 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4373 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4374 0.0f, 1.0f, 0.0f, 0.0f));
4375}
4376
4377TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004378 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004379 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004380
arthurhungdcef2dc2020-08-11 14:47:50 +08004381 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004382
4383 NotifyMotionArgs args;
4384
4385 // Button press.
4386 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004387 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4388 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4390 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4391 ASSERT_EQ(DEVICE_ID, args.deviceId);
4392 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4393 ASSERT_EQ(uint32_t(0), args.policyFlags);
4394 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4395 ASSERT_EQ(0, args.flags);
4396 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4397 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4398 ASSERT_EQ(0, args.edgeFlags);
4399 ASSERT_EQ(uint32_t(1), args.pointerCount);
4400 ASSERT_EQ(0, args.pointerProperties[0].id);
4401 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004402 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4404 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4405 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4406
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4408 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4409 ASSERT_EQ(DEVICE_ID, args.deviceId);
4410 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4411 ASSERT_EQ(uint32_t(0), args.policyFlags);
4412 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4413 ASSERT_EQ(0, args.flags);
4414 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4415 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4416 ASSERT_EQ(0, args.edgeFlags);
4417 ASSERT_EQ(uint32_t(1), args.pointerCount);
4418 ASSERT_EQ(0, args.pointerProperties[0].id);
4419 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004420 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004421 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4422 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4423 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4424
Michael Wrightd02c5b62014-02-10 15:10:22 -08004425 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004426 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4427 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4429 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4430 ASSERT_EQ(DEVICE_ID, args.deviceId);
4431 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4432 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004433 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4434 ASSERT_EQ(0, args.flags);
4435 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4436 ASSERT_EQ(0, args.buttonState);
4437 ASSERT_EQ(0, args.edgeFlags);
4438 ASSERT_EQ(uint32_t(1), args.pointerCount);
4439 ASSERT_EQ(0, args.pointerProperties[0].id);
4440 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004441 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004442 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4443 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4444 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4445
4446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4447 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4448 ASSERT_EQ(DEVICE_ID, args.deviceId);
4449 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4450 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004451 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4452 ASSERT_EQ(0, args.flags);
4453 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4454 ASSERT_EQ(0, args.buttonState);
4455 ASSERT_EQ(0, args.edgeFlags);
4456 ASSERT_EQ(uint32_t(1), args.pointerCount);
4457 ASSERT_EQ(0, args.pointerProperties[0].id);
4458 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004459 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004460 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4461 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4462 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4463}
4464
4465TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004467 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004468
4469 NotifyMotionArgs args;
4470
4471 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004472 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4473 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4475 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004476 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4477 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4478 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479
4480 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004481 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4482 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4484 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004485 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4486 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004487}
4488
4489TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004490 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004491 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004492
4493 NotifyMotionArgs args;
4494
4495 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004496 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4497 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4499 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004500 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004501
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4503 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004504 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004505
Michael Wrightd02c5b62014-02-10 15:10:22 -08004506 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004507 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4508 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004510 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004511 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004512
4513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004514 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004515 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004516}
4517
4518TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004519 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004520 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004521
4522 NotifyMotionArgs args;
4523
4524 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004525 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4526 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4527 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4528 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4530 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004531 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4532 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4533 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004534
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4536 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004537 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4538 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4539 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004540
Michael Wrightd02c5b62014-02-10 15:10:22 -08004541 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004542 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4543 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4544 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4546 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004547 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4548 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4549 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004550
4551 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004552 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4553 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004555 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004556 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004557
4558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004559 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004560 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004561}
4562
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004563TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004564 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004565 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004566 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4567 // need to be rotated.
4568 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004569 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004570
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004571 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004572 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4573 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4574 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4575 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4576 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4577 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4578 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4579 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4580}
4581
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004582TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004583 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004584 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004585 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4586 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004587 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004588
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004589 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004590 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004591 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4592 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4593 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4594 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4595 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4596 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4597 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4598 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4599
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004600 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004601 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004602 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4603 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4604 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4605 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4606 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4607 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4608 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4609 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004610
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004611 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004612 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004613 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4614 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4615 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4616 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4617 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4618 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4619 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4620 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4621
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004622 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004623 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004624 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4625 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4626 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4627 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4628 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4629 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4630 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4631 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632}
4633
4634TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004635 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004636 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004637
4638 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4639 mFakePointerController->setPosition(100, 200);
4640 mFakePointerController->setButtonState(0);
4641
4642 NotifyMotionArgs motionArgs;
4643 NotifyKeyArgs keyArgs;
4644
4645 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004646 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4647 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4649 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4650 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4651 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004652 ASSERT_NO_FATAL_FAILURE(
4653 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004654
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4656 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4657 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4658 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004659 ASSERT_NO_FATAL_FAILURE(
4660 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004661
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004662 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4663 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004665 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004666 ASSERT_EQ(0, motionArgs.buttonState);
4667 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004668 ASSERT_NO_FATAL_FAILURE(
4669 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004670
4671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004672 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004673 ASSERT_EQ(0, motionArgs.buttonState);
4674 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004675 ASSERT_NO_FATAL_FAILURE(
4676 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004677
4678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004679 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004680 ASSERT_EQ(0, motionArgs.buttonState);
4681 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004682 ASSERT_NO_FATAL_FAILURE(
4683 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004684
4685 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4687 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4688 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4690 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4691 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4692 motionArgs.buttonState);
4693 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4694 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004695 ASSERT_NO_FATAL_FAILURE(
4696 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004697
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4699 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4700 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4701 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4702 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004703 ASSERT_NO_FATAL_FAILURE(
4704 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004705
4706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4707 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4708 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4709 motionArgs.buttonState);
4710 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4711 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004712 ASSERT_NO_FATAL_FAILURE(
4713 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004714
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004715 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4716 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004718 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004719 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4720 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004721 ASSERT_NO_FATAL_FAILURE(
4722 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004723
4724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004725 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004726 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4727 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004728 ASSERT_NO_FATAL_FAILURE(
4729 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004730
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004731 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4732 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004734 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4735 ASSERT_EQ(0, motionArgs.buttonState);
4736 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004737 ASSERT_NO_FATAL_FAILURE(
4738 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004739 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4740 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004741
4742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004743 ASSERT_EQ(0, motionArgs.buttonState);
4744 ASSERT_EQ(0, mFakePointerController->getButtonState());
4745 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004746 ASSERT_NO_FATAL_FAILURE(
4747 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004748
Michael Wrightd02c5b62014-02-10 15:10:22 -08004749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4750 ASSERT_EQ(0, motionArgs.buttonState);
4751 ASSERT_EQ(0, mFakePointerController->getButtonState());
4752 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004753 ASSERT_NO_FATAL_FAILURE(
4754 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004755
4756 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004757 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4758 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4760 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4761 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004762
Michael Wrightd02c5b62014-02-10 15:10:22 -08004763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004764 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004765 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4766 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004767 ASSERT_NO_FATAL_FAILURE(
4768 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004769
4770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4771 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4772 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4773 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004774 ASSERT_NO_FATAL_FAILURE(
4775 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004776
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004777 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4778 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004780 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004781 ASSERT_EQ(0, motionArgs.buttonState);
4782 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004783 ASSERT_NO_FATAL_FAILURE(
4784 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004785
4786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004787 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004788 ASSERT_EQ(0, motionArgs.buttonState);
4789 ASSERT_EQ(0, mFakePointerController->getButtonState());
4790
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004791 ASSERT_NO_FATAL_FAILURE(
4792 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4794 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4795 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4796
4797 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004798 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4801 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4802 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004803
Michael Wrightd02c5b62014-02-10 15:10:22 -08004804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004805 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004806 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4807 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004808 ASSERT_NO_FATAL_FAILURE(
4809 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004810
4811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4812 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4813 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4814 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004815 ASSERT_NO_FATAL_FAILURE(
4816 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004817
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004818 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
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->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004821 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004822 ASSERT_EQ(0, motionArgs.buttonState);
4823 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004824 ASSERT_NO_FATAL_FAILURE(
4825 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004826
4827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4828 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4829 ASSERT_EQ(0, motionArgs.buttonState);
4830 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004831 ASSERT_NO_FATAL_FAILURE(
4832 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004833
Michael Wrightd02c5b62014-02-10 15:10:22 -08004834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4835 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4836 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4837
4838 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004839 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4840 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4842 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4843 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004844
Michael Wrightd02c5b62014-02-10 15:10:22 -08004845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004846 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004847 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4848 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004849 ASSERT_NO_FATAL_FAILURE(
4850 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004851
4852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4853 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4854 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4855 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004856 ASSERT_NO_FATAL_FAILURE(
4857 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004858
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004859 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004862 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004863 ASSERT_EQ(0, motionArgs.buttonState);
4864 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004865 ASSERT_NO_FATAL_FAILURE(
4866 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004867
4868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4869 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4870 ASSERT_EQ(0, motionArgs.buttonState);
4871 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004872 ASSERT_NO_FATAL_FAILURE(
4873 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004874
Michael Wrightd02c5b62014-02-10 15:10:22 -08004875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4876 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4877 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4878
4879 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004880 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4881 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4883 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4884 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004885
Michael Wrightd02c5b62014-02-10 15:10:22 -08004886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004887 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004888 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4889 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004890 ASSERT_NO_FATAL_FAILURE(
4891 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004892
4893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4894 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4895 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4896 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004897 ASSERT_NO_FATAL_FAILURE(
4898 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004899
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004900 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
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->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004903 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004904 ASSERT_EQ(0, motionArgs.buttonState);
4905 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004906 ASSERT_NO_FATAL_FAILURE(
4907 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004908
4909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4910 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4911 ASSERT_EQ(0, motionArgs.buttonState);
4912 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004913 ASSERT_NO_FATAL_FAILURE(
4914 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004915
Michael Wrightd02c5b62014-02-10 15:10:22 -08004916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4917 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4918 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4919}
4920
4921TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004922 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004923 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004924
4925 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4926 mFakePointerController->setPosition(100, 200);
4927 mFakePointerController->setButtonState(0);
4928
4929 NotifyMotionArgs args;
4930
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004931 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4932 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4933 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004935 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4936 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4937 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4938 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 +01004939 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004940}
4941
4942TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004943 addConfigurationProperty("cursor.mode", "pointer");
4944 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004945 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004946
4947 NotifyDeviceResetArgs resetArgs;
4948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4949 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4950 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4951
4952 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4953 mFakePointerController->setPosition(100, 200);
4954 mFakePointerController->setButtonState(0);
4955
4956 NotifyMotionArgs args;
4957
4958 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004959 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4960 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4961 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4963 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4964 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4965 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4966 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 +01004967 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004968
4969 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004970 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4971 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4973 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4974 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4975 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4976 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4978 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4979 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4980 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4981 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4982
4983 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004984 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4985 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4987 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4988 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4989 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4990 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4992 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4993 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4994 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4995 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4996
4997 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004998 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4999 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
5000 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5002 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5003 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5004 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5005 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 +01005006 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005007
5008 // Disable pointer capture and check that the device generation got bumped
5009 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08005010 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005011 mFakePolicy->setPointerCapture(false);
5012 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08005013 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005014
5015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005016 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5017
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005018 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5019 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5020 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5022 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005023 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5024 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5025 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 +01005026 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005027}
5028
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00005029/**
5030 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
5031 * pointer acceleration or speed processing should not be applied.
5032 */
5033TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
5034 addConfigurationProperty("cursor.mode", "pointer");
5035 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
5036 100.f /*high threshold*/, 10.f /*acceleration*/);
5037 mFakePolicy->setVelocityControlParams(testParams);
5038 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5039
5040 NotifyDeviceResetArgs resetArgs;
5041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5042 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5043 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5044
5045 NotifyMotionArgs args;
5046
5047 // Move and verify scale is applied.
5048 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5049 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5050 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5052 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5053 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5054 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5055 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5056 ASSERT_GT(relX, 10);
5057 ASSERT_GT(relY, 20);
5058
5059 // Enable Pointer Capture
5060 mFakePolicy->setPointerCapture(true);
5061 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5062 NotifyPointerCaptureChangedArgs captureArgs;
5063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5064 ASSERT_TRUE(captureArgs.request.enable);
5065
5066 // Move and verify scale is not applied.
5067 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5068 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5069 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5071 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5072 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5073 ASSERT_EQ(10, args.pointerCoords[0].getX());
5074 ASSERT_EQ(20, args.pointerCoords[0].getY());
5075}
5076
Prabir Pradhan208360b2022-06-24 18:37:04 +00005077TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5078 addConfigurationProperty("cursor.mode", "pointer");
5079 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5080
5081 NotifyDeviceResetArgs resetArgs;
5082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5083 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5084 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5085
5086 // Ensure the display is rotated.
5087 prepareDisplay(DISPLAY_ORIENTATION_90);
5088
5089 NotifyMotionArgs args;
5090
5091 // Verify that the coordinates are rotated.
5092 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5093 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5094 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5096 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5097 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5098 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5099 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5100
5101 // Enable Pointer Capture.
5102 mFakePolicy->setPointerCapture(true);
5103 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5104 NotifyPointerCaptureChangedArgs captureArgs;
5105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5106 ASSERT_TRUE(captureArgs.request.enable);
5107
5108 // Move and verify rotation is not 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_RELATIVE, args.source);
5114 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5115 ASSERT_EQ(10, args.pointerCoords[0].getX());
5116 ASSERT_EQ(20, args.pointerCoords[0].getY());
5117}
5118
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005119TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005120 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005121
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005122 // Set up the default display.
5123 prepareDisplay(DISPLAY_ORIENTATION_90);
5124
5125 // Set up the secondary display as the display on which the pointer should be shown.
5126 // The InputDevice is not associated with any display.
5127 prepareSecondaryDisplay();
5128 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08005129 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5130
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005131 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005132 mFakePointerController->setPosition(100, 200);
5133 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005134
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005135 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5137 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5138 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005140 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5141 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5142 WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01005143 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005144}
5145
5146TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
5147 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5148
5149 // Set up the default display.
5150 prepareDisplay(DISPLAY_ORIENTATION_90);
5151
5152 // Set up the secondary display as the display on which the pointer should be shown,
5153 // and associate the InputDevice with the secondary display.
5154 prepareSecondaryDisplay();
5155 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5156 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5157 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5158
5159 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5160 mFakePointerController->setPosition(100, 200);
5161 mFakePointerController->setButtonState(0);
5162
5163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5165 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005167 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5168 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5169 WithCoords(110.0f, 220.0f))));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005170 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
5171}
5172
5173TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
5174 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5175
5176 // Set up the default display as the display on which the pointer should be shown.
5177 prepareDisplay(DISPLAY_ORIENTATION_90);
5178 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5179
5180 // Associate the InputDevice with the secondary display.
5181 prepareSecondaryDisplay();
5182 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5183 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5184
5185 // The mapper should not generate any events because it is associated with a display that is
5186 // different from the pointer display.
5187 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5188 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5189 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005191}
5192
Michael Wrightd02c5b62014-02-10 15:10:22 -08005193// --- TouchInputMapperTest ---
5194
5195class TouchInputMapperTest : public InputMapperTest {
5196protected:
5197 static const int32_t RAW_X_MIN;
5198 static const int32_t RAW_X_MAX;
5199 static const int32_t RAW_Y_MIN;
5200 static const int32_t RAW_Y_MAX;
5201 static const int32_t RAW_TOUCH_MIN;
5202 static const int32_t RAW_TOUCH_MAX;
5203 static const int32_t RAW_TOOL_MIN;
5204 static const int32_t RAW_TOOL_MAX;
5205 static const int32_t RAW_PRESSURE_MIN;
5206 static const int32_t RAW_PRESSURE_MAX;
5207 static const int32_t RAW_ORIENTATION_MIN;
5208 static const int32_t RAW_ORIENTATION_MAX;
5209 static const int32_t RAW_DISTANCE_MIN;
5210 static const int32_t RAW_DISTANCE_MAX;
5211 static const int32_t RAW_TILT_MIN;
5212 static const int32_t RAW_TILT_MAX;
5213 static const int32_t RAW_ID_MIN;
5214 static const int32_t RAW_ID_MAX;
5215 static const int32_t RAW_SLOT_MIN;
5216 static const int32_t RAW_SLOT_MAX;
5217 static const float X_PRECISION;
5218 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005219 static const float X_PRECISION_VIRTUAL;
5220 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005221
5222 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005223 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005224
5225 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5226
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005227 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005228 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005229
Michael Wrightd02c5b62014-02-10 15:10:22 -08005230 enum Axes {
5231 POSITION = 1 << 0,
5232 TOUCH = 1 << 1,
5233 TOOL = 1 << 2,
5234 PRESSURE = 1 << 3,
5235 ORIENTATION = 1 << 4,
5236 MINOR = 1 << 5,
5237 ID = 1 << 6,
5238 DISTANCE = 1 << 7,
5239 TILT = 1 << 8,
5240 SLOT = 1 << 9,
5241 TOOL_TYPE = 1 << 10,
5242 };
5243
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005244 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5245 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005246 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005247 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005248 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005249 int32_t toRawX(float displayX);
5250 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005251 int32_t toRotatedRawX(float displayX);
5252 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005253 float toCookedX(float rawX, float rawY);
5254 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005255 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005256 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005257 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005258 float toDisplayY(int32_t rawY, int32_t displayHeight);
5259
Michael Wrightd02c5b62014-02-10 15:10:22 -08005260};
5261
5262const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5263const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5264const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5265const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5266const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5267const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5268const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5269const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005270const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5271const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005272const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5273const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5274const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5275const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5276const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5277const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5278const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5279const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5280const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5281const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5282const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5283const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005284const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5285 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5286const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5287 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005288const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5289 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005290
5291const float TouchInputMapperTest::GEOMETRIC_SCALE =
5292 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5293 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5294
5295const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5296 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5297 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5298};
5299
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005300void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005301 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5302 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005303}
5304
5305void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5306 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5307 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005308}
5309
Santos Cordonfa5cf462017-04-05 10:37:00 -07005310void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005311 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5312 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5313 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005314}
5315
Michael Wrightd02c5b62014-02-10 15:10:22 -08005316void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005317 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5318 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5319 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5320 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005321}
5322
Jason Gerecke489fda82012-09-07 17:19:40 -07005323void TouchInputMapperTest::prepareLocationCalibration() {
5324 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5325}
5326
Michael Wrightd02c5b62014-02-10 15:10:22 -08005327int32_t TouchInputMapperTest::toRawX(float displayX) {
5328 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5329}
5330
5331int32_t TouchInputMapperTest::toRawY(float displayY) {
5332 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5333}
5334
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005335int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5336 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5337}
5338
5339int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5340 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5341}
5342
Jason Gerecke489fda82012-09-07 17:19:40 -07005343float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5344 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5345 return rawX;
5346}
5347
5348float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5349 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5350 return rawY;
5351}
5352
Michael Wrightd02c5b62014-02-10 15:10:22 -08005353float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005354 return toDisplayX(rawX, DISPLAY_WIDTH);
5355}
5356
5357float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5358 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005359}
5360
5361float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005362 return toDisplayY(rawY, DISPLAY_HEIGHT);
5363}
5364
5365float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5366 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005367}
5368
5369
5370// --- SingleTouchInputMapperTest ---
5371
5372class SingleTouchInputMapperTest : public TouchInputMapperTest {
5373protected:
5374 void prepareButtons();
5375 void prepareAxes(int axes);
5376
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005377 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5378 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5379 void processUp(SingleTouchInputMapper& mappery);
5380 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5381 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5382 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5383 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5384 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5385 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005386};
5387
5388void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005389 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005390}
5391
5392void SingleTouchInputMapperTest::prepareAxes(int axes) {
5393 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005394 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5395 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005396 }
5397 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005398 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5399 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005400 }
5401 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005402 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5403 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005404 }
5405 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005406 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5407 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005408 }
5409 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005410 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5411 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005412 }
5413}
5414
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005415void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005416 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5417 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5418 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005419}
5420
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005421void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005422 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5423 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005424}
5425
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005426void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005427 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005428}
5429
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005430void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005431 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005432}
5433
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005434void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5435 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005436 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005437}
5438
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005439void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005440 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005441}
5442
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005443void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5444 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005445 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005447}
5448
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005449void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5450 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005451 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005452}
5453
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005454void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005455 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005456}
5457
Michael Wrightd02c5b62014-02-10 15:10:22 -08005458TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005459 prepareButtons();
5460 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005461 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005462
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005463 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005464}
5465
Michael Wrightd02c5b62014-02-10 15:10:22 -08005466TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005467 prepareButtons();
5468 prepareAxes(POSITION);
5469 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005470 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005471
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005472 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005473}
5474
5475TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005476 addConfigurationProperty("touch.deviceType", "touchScreen");
5477 prepareDisplay(DISPLAY_ORIENTATION_0);
5478 prepareButtons();
5479 prepareAxes(POSITION);
5480 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005481 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005482
5483 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005484 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005485
5486 // Virtual key is down.
5487 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5488 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5489 processDown(mapper, x, y);
5490 processSync(mapper);
5491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5492
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005493 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005494
5495 // Virtual key is up.
5496 processUp(mapper);
5497 processSync(mapper);
5498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5499
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005500 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005501}
5502
5503TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005504 addConfigurationProperty("touch.deviceType", "touchScreen");
5505 prepareDisplay(DISPLAY_ORIENTATION_0);
5506 prepareButtons();
5507 prepareAxes(POSITION);
5508 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005509 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005510
5511 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005512 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005513
5514 // Virtual key is down.
5515 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5516 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5517 processDown(mapper, x, y);
5518 processSync(mapper);
5519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5520
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005521 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005522
5523 // Virtual key is up.
5524 processUp(mapper);
5525 processSync(mapper);
5526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5527
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005528 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005529}
5530
5531TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005532 addConfigurationProperty("touch.deviceType", "touchScreen");
5533 prepareDisplay(DISPLAY_ORIENTATION_0);
5534 prepareButtons();
5535 prepareAxes(POSITION);
5536 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005537 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005538
Michael Wrightd02c5b62014-02-10 15:10:22 -08005539 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005540 ASSERT_TRUE(
5541 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005542 ASSERT_TRUE(flags[0]);
5543 ASSERT_FALSE(flags[1]);
5544}
5545
5546TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005547 addConfigurationProperty("touch.deviceType", "touchScreen");
5548 prepareDisplay(DISPLAY_ORIENTATION_0);
5549 prepareButtons();
5550 prepareAxes(POSITION);
5551 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005552 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005553
arthurhungdcef2dc2020-08-11 14:47:50 +08005554 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005555
5556 NotifyKeyArgs args;
5557
5558 // Press virtual key.
5559 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5560 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5561 processDown(mapper, x, y);
5562 processSync(mapper);
5563
5564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5565 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5566 ASSERT_EQ(DEVICE_ID, args.deviceId);
5567 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5568 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5569 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5570 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5571 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5572 ASSERT_EQ(KEY_HOME, args.scanCode);
5573 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5574 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5575
5576 // Release virtual key.
5577 processUp(mapper);
5578 processSync(mapper);
5579
5580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5581 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5582 ASSERT_EQ(DEVICE_ID, args.deviceId);
5583 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5584 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5585 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5586 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5587 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5588 ASSERT_EQ(KEY_HOME, args.scanCode);
5589 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5590 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5591
5592 // Should not have sent any motions.
5593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5594}
5595
5596TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005597 addConfigurationProperty("touch.deviceType", "touchScreen");
5598 prepareDisplay(DISPLAY_ORIENTATION_0);
5599 prepareButtons();
5600 prepareAxes(POSITION);
5601 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005602 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005603
arthurhungdcef2dc2020-08-11 14:47:50 +08005604 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005605
5606 NotifyKeyArgs keyArgs;
5607
5608 // Press virtual key.
5609 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5610 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5611 processDown(mapper, x, y);
5612 processSync(mapper);
5613
5614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5615 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5616 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5617 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5618 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5619 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5620 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5621 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5622 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5623 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5624 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5625
5626 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5627 // into the display area.
5628 y -= 100;
5629 processMove(mapper, x, y);
5630 processSync(mapper);
5631
5632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5633 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5634 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5635 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5636 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5637 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5638 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5639 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5640 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5641 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5642 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5643 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5644
5645 NotifyMotionArgs motionArgs;
5646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5647 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5648 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5649 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5650 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5651 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5652 ASSERT_EQ(0, motionArgs.flags);
5653 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5654 ASSERT_EQ(0, motionArgs.buttonState);
5655 ASSERT_EQ(0, motionArgs.edgeFlags);
5656 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5657 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5658 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5660 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5661 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5662 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5663 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5664
5665 // Keep moving out of bounds. Should generate a pointer move.
5666 y -= 50;
5667 processMove(mapper, x, y);
5668 processSync(mapper);
5669
5670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5671 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5672 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5673 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5674 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5675 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5676 ASSERT_EQ(0, motionArgs.flags);
5677 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5678 ASSERT_EQ(0, motionArgs.buttonState);
5679 ASSERT_EQ(0, motionArgs.edgeFlags);
5680 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5681 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5682 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5683 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5684 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5685 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5686 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5687 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5688
5689 // Release out of bounds. Should generate a pointer up.
5690 processUp(mapper);
5691 processSync(mapper);
5692
5693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5694 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5695 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5696 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5697 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5698 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5699 ASSERT_EQ(0, motionArgs.flags);
5700 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5701 ASSERT_EQ(0, motionArgs.buttonState);
5702 ASSERT_EQ(0, motionArgs.edgeFlags);
5703 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5704 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5705 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5706 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5707 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5708 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5709 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5710 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5711
5712 // Should not have sent any more keys or motions.
5713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5715}
5716
5717TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005718 addConfigurationProperty("touch.deviceType", "touchScreen");
5719 prepareDisplay(DISPLAY_ORIENTATION_0);
5720 prepareButtons();
5721 prepareAxes(POSITION);
5722 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005723 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005724
arthurhungdcef2dc2020-08-11 14:47:50 +08005725 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005726
5727 NotifyMotionArgs motionArgs;
5728
5729 // Initially go down out of bounds.
5730 int32_t x = -10;
5731 int32_t y = -10;
5732 processDown(mapper, x, y);
5733 processSync(mapper);
5734
5735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5736
5737 // Move into the display area. Should generate a pointer down.
5738 x = 50;
5739 y = 75;
5740 processMove(mapper, x, y);
5741 processSync(mapper);
5742
5743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5744 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5745 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5746 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5747 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5748 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5749 ASSERT_EQ(0, motionArgs.flags);
5750 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5751 ASSERT_EQ(0, motionArgs.buttonState);
5752 ASSERT_EQ(0, motionArgs.edgeFlags);
5753 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5754 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5755 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5756 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5757 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5758 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5759 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5760 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5761
5762 // Release. Should generate a pointer up.
5763 processUp(mapper);
5764 processSync(mapper);
5765
5766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5767 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5768 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5769 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5770 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5771 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5772 ASSERT_EQ(0, motionArgs.flags);
5773 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5774 ASSERT_EQ(0, motionArgs.buttonState);
5775 ASSERT_EQ(0, motionArgs.edgeFlags);
5776 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5777 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5778 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5780 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5781 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5782 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5783 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5784
5785 // Should not have sent any more keys or motions.
5786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5788}
5789
Santos Cordonfa5cf462017-04-05 10:37:00 -07005790TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005791 addConfigurationProperty("touch.deviceType", "touchScreen");
5792 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5793
5794 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5795 prepareButtons();
5796 prepareAxes(POSITION);
5797 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005798 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005799
arthurhungdcef2dc2020-08-11 14:47:50 +08005800 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005801
5802 NotifyMotionArgs motionArgs;
5803
5804 // Down.
5805 int32_t x = 100;
5806 int32_t y = 125;
5807 processDown(mapper, x, y);
5808 processSync(mapper);
5809
5810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5811 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5812 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5813 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5814 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5815 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5816 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5817 ASSERT_EQ(0, motionArgs.flags);
5818 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5819 ASSERT_EQ(0, motionArgs.buttonState);
5820 ASSERT_EQ(0, motionArgs.edgeFlags);
5821 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5822 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5823 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5824 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5825 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5826 1, 0, 0, 0, 0, 0, 0, 0));
5827 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5828 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5829 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5830
5831 // Move.
5832 x += 50;
5833 y += 75;
5834 processMove(mapper, x, y);
5835 processSync(mapper);
5836
5837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5838 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5839 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5840 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5841 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5842 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5843 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5844 ASSERT_EQ(0, motionArgs.flags);
5845 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5846 ASSERT_EQ(0, motionArgs.buttonState);
5847 ASSERT_EQ(0, motionArgs.edgeFlags);
5848 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5849 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5850 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5851 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5852 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5853 1, 0, 0, 0, 0, 0, 0, 0));
5854 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5855 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5856 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5857
5858 // Up.
5859 processUp(mapper);
5860 processSync(mapper);
5861
5862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5863 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5864 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5865 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5866 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5867 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5868 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5869 ASSERT_EQ(0, motionArgs.flags);
5870 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5871 ASSERT_EQ(0, motionArgs.buttonState);
5872 ASSERT_EQ(0, motionArgs.edgeFlags);
5873 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5874 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5875 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5876 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5877 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5878 1, 0, 0, 0, 0, 0, 0, 0));
5879 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5880 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5881 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5882
5883 // Should not have sent any more keys or motions.
5884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5886}
5887
Michael Wrightd02c5b62014-02-10 15:10:22 -08005888TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005889 addConfigurationProperty("touch.deviceType", "touchScreen");
5890 prepareDisplay(DISPLAY_ORIENTATION_0);
5891 prepareButtons();
5892 prepareAxes(POSITION);
5893 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005894 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005895
arthurhungdcef2dc2020-08-11 14:47:50 +08005896 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005897
5898 NotifyMotionArgs motionArgs;
5899
5900 // Down.
5901 int32_t x = 100;
5902 int32_t y = 125;
5903 processDown(mapper, x, y);
5904 processSync(mapper);
5905
5906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5907 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5908 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5909 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5910 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5911 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5912 ASSERT_EQ(0, motionArgs.flags);
5913 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5914 ASSERT_EQ(0, motionArgs.buttonState);
5915 ASSERT_EQ(0, motionArgs.edgeFlags);
5916 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5917 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5918 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5920 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5921 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5922 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5923 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5924
5925 // Move.
5926 x += 50;
5927 y += 75;
5928 processMove(mapper, x, y);
5929 processSync(mapper);
5930
5931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5932 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5933 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5934 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5935 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5936 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5937 ASSERT_EQ(0, motionArgs.flags);
5938 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5939 ASSERT_EQ(0, motionArgs.buttonState);
5940 ASSERT_EQ(0, motionArgs.edgeFlags);
5941 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5942 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5943 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5944 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5945 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5946 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5947 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5948 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5949
5950 // Up.
5951 processUp(mapper);
5952 processSync(mapper);
5953
5954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5955 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5956 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5957 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5958 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5959 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5960 ASSERT_EQ(0, motionArgs.flags);
5961 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5962 ASSERT_EQ(0, motionArgs.buttonState);
5963 ASSERT_EQ(0, motionArgs.edgeFlags);
5964 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5965 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5966 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5967 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5968 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5969 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5970 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5971 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5972
5973 // Should not have sent any more keys or motions.
5974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5976}
5977
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005978TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005979 addConfigurationProperty("touch.deviceType", "touchScreen");
5980 prepareButtons();
5981 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005982 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5983 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005984 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005985
5986 NotifyMotionArgs args;
5987
5988 // Rotation 90.
5989 prepareDisplay(DISPLAY_ORIENTATION_90);
5990 processDown(mapper, toRawX(50), toRawY(75));
5991 processSync(mapper);
5992
5993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5994 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5995 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5996
5997 processUp(mapper);
5998 processSync(mapper);
5999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6000}
6001
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006002TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006003 addConfigurationProperty("touch.deviceType", "touchScreen");
6004 prepareButtons();
6005 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006006 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6007 // orientation-aware are affected by display rotation.
6008 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006009 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006010
6011 NotifyMotionArgs args;
6012
6013 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006014 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006015 prepareDisplay(DISPLAY_ORIENTATION_0);
6016 processDown(mapper, toRawX(50), toRawY(75));
6017 processSync(mapper);
6018
6019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6020 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6021 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6022
6023 processUp(mapper);
6024 processSync(mapper);
6025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6026
6027 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006028 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006029 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006030 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006031 processSync(mapper);
6032
6033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6034 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6035 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6036
6037 processUp(mapper);
6038 processSync(mapper);
6039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6040
6041 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006042 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006043 prepareDisplay(DISPLAY_ORIENTATION_180);
6044 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6045 processSync(mapper);
6046
6047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6048 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6049 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6050
6051 processUp(mapper);
6052 processSync(mapper);
6053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6054
6055 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006056 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006057 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006058 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006059 processSync(mapper);
6060
6061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6062 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6063 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6064
6065 processUp(mapper);
6066 processSync(mapper);
6067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6068}
6069
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006070TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6071 addConfigurationProperty("touch.deviceType", "touchScreen");
6072 prepareButtons();
6073 prepareAxes(POSITION);
6074 addConfigurationProperty("touch.orientationAware", "1");
6075 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6076 clearViewports();
6077 prepareDisplay(DISPLAY_ORIENTATION_0);
6078 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6079 NotifyMotionArgs args;
6080
6081 // Orientation 0.
6082 processDown(mapper, toRawX(50), toRawY(75));
6083 processSync(mapper);
6084
6085 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6086 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6087 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6088
6089 processUp(mapper);
6090 processSync(mapper);
6091 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6092}
6093
6094TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6095 addConfigurationProperty("touch.deviceType", "touchScreen");
6096 prepareButtons();
6097 prepareAxes(POSITION);
6098 addConfigurationProperty("touch.orientationAware", "1");
6099 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6100 clearViewports();
6101 prepareDisplay(DISPLAY_ORIENTATION_0);
6102 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6103 NotifyMotionArgs args;
6104
6105 // Orientation 90.
6106 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6107 processSync(mapper);
6108
6109 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6110 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6111 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6112
6113 processUp(mapper);
6114 processSync(mapper);
6115 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6116}
6117
6118TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6119 addConfigurationProperty("touch.deviceType", "touchScreen");
6120 prepareButtons();
6121 prepareAxes(POSITION);
6122 addConfigurationProperty("touch.orientationAware", "1");
6123 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6124 clearViewports();
6125 prepareDisplay(DISPLAY_ORIENTATION_0);
6126 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6127 NotifyMotionArgs args;
6128
6129 // Orientation 180.
6130 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6131 processSync(mapper);
6132
6133 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6134 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6135 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6136
6137 processUp(mapper);
6138 processSync(mapper);
6139 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6140}
6141
6142TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6143 addConfigurationProperty("touch.deviceType", "touchScreen");
6144 prepareButtons();
6145 prepareAxes(POSITION);
6146 addConfigurationProperty("touch.orientationAware", "1");
6147 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6148 clearViewports();
6149 prepareDisplay(DISPLAY_ORIENTATION_0);
6150 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6151 NotifyMotionArgs args;
6152
6153 // Orientation 270.
6154 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6155 processSync(mapper);
6156
6157 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6158 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6159 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6160
6161 processUp(mapper);
6162 processSync(mapper);
6163 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6164}
6165
6166TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6167 addConfigurationProperty("touch.deviceType", "touchScreen");
6168 prepareButtons();
6169 prepareAxes(POSITION);
6170 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6171 // orientation-aware are affected by display rotation.
6172 addConfigurationProperty("touch.orientationAware", "0");
6173 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6174 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6175
6176 NotifyMotionArgs args;
6177
6178 // Orientation 90, Rotation 0.
6179 clearViewports();
6180 prepareDisplay(DISPLAY_ORIENTATION_0);
6181 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6182 processSync(mapper);
6183
6184 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6185 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6186 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6187
6188 processUp(mapper);
6189 processSync(mapper);
6190 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6191
6192 // Orientation 90, Rotation 90.
6193 clearViewports();
6194 prepareDisplay(DISPLAY_ORIENTATION_90);
6195 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6196 processSync(mapper);
6197
6198 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6199 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6200 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6201
6202 processUp(mapper);
6203 processSync(mapper);
6204 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6205
6206 // Orientation 90, Rotation 180.
6207 clearViewports();
6208 prepareDisplay(DISPLAY_ORIENTATION_180);
6209 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6210 processSync(mapper);
6211
6212 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6213 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6214 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6215
6216 processUp(mapper);
6217 processSync(mapper);
6218 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6219
6220 // Orientation 90, Rotation 270.
6221 clearViewports();
6222 prepareDisplay(DISPLAY_ORIENTATION_270);
6223 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6224 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6225 processSync(mapper);
6226
6227 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6228 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6229 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6230
6231 processUp(mapper);
6232 processSync(mapper);
6233 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6234}
6235
Michael Wrightd02c5b62014-02-10 15:10:22 -08006236TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006237 addConfigurationProperty("touch.deviceType", "touchScreen");
6238 prepareDisplay(DISPLAY_ORIENTATION_0);
6239 prepareButtons();
6240 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006241 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006242
6243 // These calculations are based on the input device calibration documentation.
6244 int32_t rawX = 100;
6245 int32_t rawY = 200;
6246 int32_t rawPressure = 10;
6247 int32_t rawToolMajor = 12;
6248 int32_t rawDistance = 2;
6249 int32_t rawTiltX = 30;
6250 int32_t rawTiltY = 110;
6251
6252 float x = toDisplayX(rawX);
6253 float y = toDisplayY(rawY);
6254 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6255 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6256 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6257 float distance = float(rawDistance);
6258
6259 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6260 float tiltScale = M_PI / 180;
6261 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6262 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6263 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6264 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6265
6266 processDown(mapper, rawX, rawY);
6267 processPressure(mapper, rawPressure);
6268 processToolMajor(mapper, rawToolMajor);
6269 processDistance(mapper, rawDistance);
6270 processTilt(mapper, rawTiltX, rawTiltY);
6271 processSync(mapper);
6272
6273 NotifyMotionArgs args;
6274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6276 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6277 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6278}
6279
Jason Gerecke489fda82012-09-07 17:19:40 -07006280TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006281 addConfigurationProperty("touch.deviceType", "touchScreen");
6282 prepareDisplay(DISPLAY_ORIENTATION_0);
6283 prepareLocationCalibration();
6284 prepareButtons();
6285 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006286 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006287
6288 int32_t rawX = 100;
6289 int32_t rawY = 200;
6290
6291 float x = toDisplayX(toCookedX(rawX, rawY));
6292 float y = toDisplayY(toCookedY(rawX, rawY));
6293
6294 processDown(mapper, rawX, rawY);
6295 processSync(mapper);
6296
6297 NotifyMotionArgs args;
6298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6299 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6300 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6301}
6302
Michael Wrightd02c5b62014-02-10 15:10:22 -08006303TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006304 addConfigurationProperty("touch.deviceType", "touchScreen");
6305 prepareDisplay(DISPLAY_ORIENTATION_0);
6306 prepareButtons();
6307 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006308 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006309
6310 NotifyMotionArgs motionArgs;
6311 NotifyKeyArgs keyArgs;
6312
6313 processDown(mapper, 100, 200);
6314 processSync(mapper);
6315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6316 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6317 ASSERT_EQ(0, motionArgs.buttonState);
6318
6319 // press BTN_LEFT, release BTN_LEFT
6320 processKey(mapper, BTN_LEFT, 1);
6321 processSync(mapper);
6322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6323 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6324 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6325
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6327 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6328 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6329
Michael Wrightd02c5b62014-02-10 15:10:22 -08006330 processKey(mapper, BTN_LEFT, 0);
6331 processSync(mapper);
6332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006333 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006334 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006335
6336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006337 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006338 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006339
6340 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6341 processKey(mapper, BTN_RIGHT, 1);
6342 processKey(mapper, BTN_MIDDLE, 1);
6343 processSync(mapper);
6344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6345 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6346 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6347 motionArgs.buttonState);
6348
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6350 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6351 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6352
6353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6354 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6355 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6356 motionArgs.buttonState);
6357
Michael Wrightd02c5b62014-02-10 15:10:22 -08006358 processKey(mapper, BTN_RIGHT, 0);
6359 processSync(mapper);
6360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006361 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006362 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006363
6364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006365 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006366 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006367
6368 processKey(mapper, BTN_MIDDLE, 0);
6369 processSync(mapper);
6370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006371 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006372 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006373
6374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006376 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006377
6378 // press BTN_BACK, release BTN_BACK
6379 processKey(mapper, BTN_BACK, 1);
6380 processSync(mapper);
6381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6382 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6383 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006384
Michael Wrightd02c5b62014-02-10 15:10:22 -08006385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006386 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006387 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6388
6389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6390 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6391 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006392
6393 processKey(mapper, BTN_BACK, 0);
6394 processSync(mapper);
6395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006396 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006397 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006398
6399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006400 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006401 ASSERT_EQ(0, motionArgs.buttonState);
6402
Michael Wrightd02c5b62014-02-10 15:10:22 -08006403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6404 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6405 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6406
6407 // press BTN_SIDE, release BTN_SIDE
6408 processKey(mapper, BTN_SIDE, 1);
6409 processSync(mapper);
6410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6411 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6412 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006413
Michael Wrightd02c5b62014-02-10 15:10:22 -08006414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006415 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006416 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6417
6418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6419 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6420 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006421
6422 processKey(mapper, BTN_SIDE, 0);
6423 processSync(mapper);
6424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006425 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006426 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006427
6428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006429 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006430 ASSERT_EQ(0, motionArgs.buttonState);
6431
Michael Wrightd02c5b62014-02-10 15:10:22 -08006432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6433 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6434 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6435
6436 // press BTN_FORWARD, release BTN_FORWARD
6437 processKey(mapper, BTN_FORWARD, 1);
6438 processSync(mapper);
6439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6440 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6441 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006442
Michael Wrightd02c5b62014-02-10 15:10:22 -08006443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006444 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006445 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6446
6447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6448 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6449 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006450
6451 processKey(mapper, BTN_FORWARD, 0);
6452 processSync(mapper);
6453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006454 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006455 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006456
6457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006458 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006459 ASSERT_EQ(0, motionArgs.buttonState);
6460
Michael Wrightd02c5b62014-02-10 15:10:22 -08006461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6462 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6463 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6464
6465 // press BTN_EXTRA, release BTN_EXTRA
6466 processKey(mapper, BTN_EXTRA, 1);
6467 processSync(mapper);
6468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6469 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6470 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006471
Michael Wrightd02c5b62014-02-10 15:10:22 -08006472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006473 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006474 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6475
6476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6477 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6478 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006479
6480 processKey(mapper, BTN_EXTRA, 0);
6481 processSync(mapper);
6482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006483 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006484 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006485
6486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006487 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006488 ASSERT_EQ(0, motionArgs.buttonState);
6489
Michael Wrightd02c5b62014-02-10 15:10:22 -08006490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6491 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6492 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6493
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6495
Michael Wrightd02c5b62014-02-10 15:10:22 -08006496 // press BTN_STYLUS, release BTN_STYLUS
6497 processKey(mapper, BTN_STYLUS, 1);
6498 processSync(mapper);
6499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6500 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006501 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6502
6503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6504 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6505 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006506
6507 processKey(mapper, BTN_STYLUS, 0);
6508 processSync(mapper);
6509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006510 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006511 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006512
6513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006514 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006515 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006516
6517 // press BTN_STYLUS2, release BTN_STYLUS2
6518 processKey(mapper, BTN_STYLUS2, 1);
6519 processSync(mapper);
6520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6521 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006522 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6523
6524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6525 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6526 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006527
6528 processKey(mapper, BTN_STYLUS2, 0);
6529 processSync(mapper);
6530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006531 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006532 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006533
6534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006535 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006536 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006537
6538 // release touch
6539 processUp(mapper);
6540 processSync(mapper);
6541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6542 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6543 ASSERT_EQ(0, motionArgs.buttonState);
6544}
6545
6546TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006547 addConfigurationProperty("touch.deviceType", "touchScreen");
6548 prepareDisplay(DISPLAY_ORIENTATION_0);
6549 prepareButtons();
6550 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006551 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006552
6553 NotifyMotionArgs motionArgs;
6554
6555 // default tool type is finger
6556 processDown(mapper, 100, 200);
6557 processSync(mapper);
6558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6559 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6560 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6561
6562 // eraser
6563 processKey(mapper, BTN_TOOL_RUBBER, 1);
6564 processSync(mapper);
6565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6566 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6567 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6568
6569 // stylus
6570 processKey(mapper, BTN_TOOL_RUBBER, 0);
6571 processKey(mapper, BTN_TOOL_PEN, 1);
6572 processSync(mapper);
6573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6574 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6575 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6576
6577 // brush
6578 processKey(mapper, BTN_TOOL_PEN, 0);
6579 processKey(mapper, BTN_TOOL_BRUSH, 1);
6580 processSync(mapper);
6581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6582 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6583 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6584
6585 // pencil
6586 processKey(mapper, BTN_TOOL_BRUSH, 0);
6587 processKey(mapper, BTN_TOOL_PENCIL, 1);
6588 processSync(mapper);
6589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6590 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6591 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6592
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006593 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006594 processKey(mapper, BTN_TOOL_PENCIL, 0);
6595 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6596 processSync(mapper);
6597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6598 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6599 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6600
6601 // mouse
6602 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6603 processKey(mapper, BTN_TOOL_MOUSE, 1);
6604 processSync(mapper);
6605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6606 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6607 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6608
6609 // lens
6610 processKey(mapper, BTN_TOOL_MOUSE, 0);
6611 processKey(mapper, BTN_TOOL_LENS, 1);
6612 processSync(mapper);
6613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6614 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6615 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6616
6617 // double-tap
6618 processKey(mapper, BTN_TOOL_LENS, 0);
6619 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6620 processSync(mapper);
6621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6622 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6623 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6624
6625 // triple-tap
6626 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6627 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6628 processSync(mapper);
6629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6630 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6631 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6632
6633 // quad-tap
6634 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6635 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6636 processSync(mapper);
6637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6638 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6639 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6640
6641 // finger
6642 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6643 processKey(mapper, BTN_TOOL_FINGER, 1);
6644 processSync(mapper);
6645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6646 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6647 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6648
6649 // stylus trumps finger
6650 processKey(mapper, BTN_TOOL_PEN, 1);
6651 processSync(mapper);
6652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6653 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6654 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6655
6656 // eraser trumps stylus
6657 processKey(mapper, BTN_TOOL_RUBBER, 1);
6658 processSync(mapper);
6659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6660 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6661 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6662
6663 // mouse trumps eraser
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 // back to default tool type
6671 processKey(mapper, BTN_TOOL_MOUSE, 0);
6672 processKey(mapper, BTN_TOOL_RUBBER, 0);
6673 processKey(mapper, BTN_TOOL_PEN, 0);
6674 processKey(mapper, BTN_TOOL_FINGER, 0);
6675 processSync(mapper);
6676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6677 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6678 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6679}
6680
6681TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006682 addConfigurationProperty("touch.deviceType", "touchScreen");
6683 prepareDisplay(DISPLAY_ORIENTATION_0);
6684 prepareButtons();
6685 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006686 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006687 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006688
6689 NotifyMotionArgs motionArgs;
6690
6691 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6692 processKey(mapper, BTN_TOOL_FINGER, 1);
6693 processMove(mapper, 100, 200);
6694 processSync(mapper);
6695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6696 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6697 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6698 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6699
6700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6701 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6703 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6704
6705 // move a little
6706 processMove(mapper, 150, 250);
6707 processSync(mapper);
6708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6709 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6710 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6711 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6712
6713 // down when BTN_TOUCH is pressed, pressure defaults to 1
6714 processKey(mapper, BTN_TOUCH, 1);
6715 processSync(mapper);
6716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6717 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6719 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6720
6721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6722 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6723 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6724 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6725
6726 // up when BTN_TOUCH is released, hover restored
6727 processKey(mapper, BTN_TOUCH, 0);
6728 processSync(mapper);
6729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6730 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6731 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6732 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6733
6734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6735 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6737 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6738
6739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6740 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6741 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6742 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6743
6744 // exit hover when pointer goes away
6745 processKey(mapper, BTN_TOOL_FINGER, 0);
6746 processSync(mapper);
6747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6748 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6749 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6750 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6751}
6752
6753TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006754 addConfigurationProperty("touch.deviceType", "touchScreen");
6755 prepareDisplay(DISPLAY_ORIENTATION_0);
6756 prepareButtons();
6757 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006758 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006759
6760 NotifyMotionArgs motionArgs;
6761
6762 // initially hovering because pressure is 0
6763 processDown(mapper, 100, 200);
6764 processPressure(mapper, 0);
6765 processSync(mapper);
6766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6767 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6769 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6770
6771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6772 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6774 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6775
6776 // move a little
6777 processMove(mapper, 150, 250);
6778 processSync(mapper);
6779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6780 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6781 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6782 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6783
6784 // down when pressure is non-zero
6785 processPressure(mapper, RAW_PRESSURE_MAX);
6786 processSync(mapper);
6787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6788 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6789 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6790 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6791
6792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6793 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6794 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6795 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6796
6797 // up when pressure becomes 0, hover restored
6798 processPressure(mapper, 0);
6799 processSync(mapper);
6800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6801 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6803 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6804
6805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6806 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6807 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6808 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6809
6810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6811 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6812 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6813 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6814
6815 // exit hover when pointer goes away
6816 processUp(mapper);
6817 processSync(mapper);
6818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6819 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6820 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6821 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6822}
6823
lilinnan687e58f2022-07-19 16:00:50 +08006824TEST_F(SingleTouchInputMapperTest,
6825 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6826 addConfigurationProperty("touch.deviceType", "touchScreen");
6827 prepareDisplay(DISPLAY_ORIENTATION_0);
6828 prepareButtons();
6829 prepareAxes(POSITION);
6830 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6831 NotifyMotionArgs motionArgs;
6832
6833 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006834 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006835 processSync(mapper);
6836
6837 // We should receive a down event
6838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6839 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6840
6841 // Change display id
6842 clearViewports();
6843 prepareSecondaryDisplay(ViewportType::INTERNAL);
6844
6845 // We should receive a cancel event
6846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6847 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6848 // Then receive reset called
6849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6850}
6851
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006852TEST_F(SingleTouchInputMapperTest,
6853 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6854 addConfigurationProperty("touch.deviceType", "touchScreen");
6855 prepareDisplay(DISPLAY_ORIENTATION_0);
6856 prepareButtons();
6857 prepareAxes(POSITION);
6858 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6860 NotifyMotionArgs motionArgs;
6861
6862 // Start a new gesture.
6863 processDown(mapper, 100, 200);
6864 processSync(mapper);
6865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6866 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6867
6868 // Make the viewport inactive. This will put the device in disabled mode.
6869 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6870 viewport->isActive = false;
6871 mFakePolicy->updateViewport(*viewport);
6872 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6873
6874 // We should receive a cancel event for the ongoing gesture.
6875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6876 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6877 // Then we should be notified that the device was reset.
6878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6879
6880 // No events are generated while the viewport is inactive.
6881 processMove(mapper, 101, 201);
6882 processSync(mapper);
6883 processDown(mapper, 102, 202);
6884 processSync(mapper);
6885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6886
6887 // Make the viewport active again. The device should resume processing events.
6888 viewport->isActive = true;
6889 mFakePolicy->updateViewport(*viewport);
6890 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6891
6892 // The device is reset because it changes back to direct mode, without generating any events.
6893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6895
6896 // Start a new gesture.
6897 processDown(mapper, 100, 200);
6898 processSync(mapper);
6899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6900 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6901
6902 // No more events.
6903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6905}
6906
Prabir Pradhan5632d622021-09-06 07:57:20 -07006907// --- TouchDisplayProjectionTest ---
6908
6909class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6910public:
6911 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6912 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6913 // rotated equivalent of the given un-rotated physical display bounds.
6914 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6915 uint32_t inverseRotationFlags;
6916 auto width = DISPLAY_WIDTH;
6917 auto height = DISPLAY_HEIGHT;
6918 switch (orientation) {
6919 case DISPLAY_ORIENTATION_90:
6920 inverseRotationFlags = ui::Transform::ROT_270;
6921 std::swap(width, height);
6922 break;
6923 case DISPLAY_ORIENTATION_180:
6924 inverseRotationFlags = ui::Transform::ROT_180;
6925 break;
6926 case DISPLAY_ORIENTATION_270:
6927 inverseRotationFlags = ui::Transform::ROT_90;
6928 std::swap(width, height);
6929 break;
6930 case DISPLAY_ORIENTATION_0:
6931 inverseRotationFlags = ui::Transform::ROT_0;
6932 break;
6933 default:
6934 FAIL() << "Invalid orientation: " << orientation;
6935 }
6936
6937 const ui::Transform rotation(inverseRotationFlags, width, height);
6938 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6939
6940 std::optional<DisplayViewport> internalViewport =
6941 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6942 DisplayViewport& v = *internalViewport;
6943 v.displayId = DISPLAY_ID;
6944 v.orientation = orientation;
6945
6946 v.logicalLeft = 0;
6947 v.logicalTop = 0;
6948 v.logicalRight = 100;
6949 v.logicalBottom = 100;
6950
6951 v.physicalLeft = rotatedPhysicalDisplay.left;
6952 v.physicalTop = rotatedPhysicalDisplay.top;
6953 v.physicalRight = rotatedPhysicalDisplay.right;
6954 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6955
6956 v.deviceWidth = width;
6957 v.deviceHeight = height;
6958
6959 v.isActive = true;
6960 v.uniqueId = UNIQUE_ID;
6961 v.type = ViewportType::INTERNAL;
6962 mFakePolicy->updateViewport(v);
6963 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6964 }
6965
6966 void assertReceivedMove(const Point& point) {
6967 NotifyMotionArgs motionArgs;
6968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6969 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6970 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6971 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6972 1, 0, 0, 0, 0, 0, 0, 0));
6973 }
6974};
6975
6976TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6977 addConfigurationProperty("touch.deviceType", "touchScreen");
6978 prepareDisplay(DISPLAY_ORIENTATION_0);
6979
6980 prepareButtons();
6981 prepareAxes(POSITION);
6982 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6983
6984 NotifyMotionArgs motionArgs;
6985
6986 // Configure the DisplayViewport such that the logical display maps to a subsection of
6987 // the display panel called the physical display. Here, the physical display is bounded by the
6988 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6989 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6990 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6991 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6992
6993 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6994 DISPLAY_ORIENTATION_270}) {
6995 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6996
6997 // Touches outside the physical display should be ignored, and should not generate any
6998 // events. Ensure touches at the following points that lie outside of the physical display
6999 // area do not generate any events.
7000 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7001 processDown(mapper, toRawX(point.x), toRawY(point.y));
7002 processSync(mapper);
7003 processUp(mapper);
7004 processSync(mapper);
7005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7006 << "Unexpected event generated for touch outside physical display at point: "
7007 << point.x << ", " << point.y;
7008 }
7009 }
7010}
7011
7012TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7013 addConfigurationProperty("touch.deviceType", "touchScreen");
7014 prepareDisplay(DISPLAY_ORIENTATION_0);
7015
7016 prepareButtons();
7017 prepareAxes(POSITION);
7018 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7019
7020 NotifyMotionArgs motionArgs;
7021
7022 // Configure the DisplayViewport such that the logical display maps to a subsection of
7023 // the display panel called the physical display. Here, the physical display is bounded by the
7024 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7025 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7026
7027 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7028 DISPLAY_ORIENTATION_270}) {
7029 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7030
7031 // Touches that start outside the physical display should be ignored until it enters the
7032 // physical display bounds, at which point it should generate a down event. Start a touch at
7033 // the point (5, 100), which is outside the physical display bounds.
7034 static const Point kOutsidePoint{5, 100};
7035 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7036 processSync(mapper);
7037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7038
7039 // Move the touch into the physical display area. This should generate a pointer down.
7040 processMove(mapper, toRawX(11), toRawY(21));
7041 processSync(mapper);
7042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7043 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7044 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7045 ASSERT_NO_FATAL_FAILURE(
7046 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7047
7048 // Move the touch inside the physical display area. This should generate a pointer move.
7049 processMove(mapper, toRawX(69), toRawY(159));
7050 processSync(mapper);
7051 assertReceivedMove({69, 159});
7052
7053 // Move outside the physical display area. Since the pointer is already down, this should
7054 // now continue generating events.
7055 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7056 processSync(mapper);
7057 assertReceivedMove(kOutsidePoint);
7058
7059 // Release. This should generate a pointer up.
7060 processUp(mapper);
7061 processSync(mapper);
7062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7063 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7064 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7065 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7066
7067 // Ensure no more events were generated.
7068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7070 }
7071}
7072
Michael Wrightd02c5b62014-02-10 15:10:22 -08007073// --- MultiTouchInputMapperTest ---
7074
7075class MultiTouchInputMapperTest : public TouchInputMapperTest {
7076protected:
7077 void prepareAxes(int axes);
7078
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007079 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7080 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7081 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7082 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7083 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7084 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7085 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7086 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7087 void processId(MultiTouchInputMapper& mapper, int32_t id);
7088 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7089 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7090 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
7091 void processMTSync(MultiTouchInputMapper& mapper);
7092 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007093};
7094
7095void MultiTouchInputMapperTest::prepareAxes(int axes) {
7096 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007097 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7098 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007099 }
7100 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007101 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7102 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007103 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007104 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7105 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007106 }
7107 }
7108 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007109 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7110 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007111 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007112 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007113 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007114 }
7115 }
7116 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007117 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7118 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007119 }
7120 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007121 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7122 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007123 }
7124 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007125 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7126 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007127 }
7128 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007129 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7130 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007131 }
7132 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007133 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7134 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007135 }
7136 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007137 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007138 }
7139}
7140
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007141void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7142 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007143 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7144 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007145}
7146
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007147void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7148 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007149 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007150}
7151
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007152void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7153 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007154 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007155}
7156
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007157void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007158 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007159}
7160
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007161void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007162 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007163}
7164
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007165void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7166 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007167 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007168}
7169
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007170void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007171 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007172}
7173
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007174void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007175 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007176}
7177
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007178void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007179 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007180}
7181
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007182void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007183 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007184}
7185
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007186void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007187 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007188}
7189
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007190void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7191 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007192 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007193}
7194
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007195void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007196 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007197}
7198
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007199void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007200 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007201}
7202
Michael Wrightd02c5b62014-02-10 15:10:22 -08007203TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007204 addConfigurationProperty("touch.deviceType", "touchScreen");
7205 prepareDisplay(DISPLAY_ORIENTATION_0);
7206 prepareAxes(POSITION);
7207 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007208 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007209
arthurhungdcef2dc2020-08-11 14:47:50 +08007210 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007211
7212 NotifyMotionArgs motionArgs;
7213
7214 // Two fingers down at once.
7215 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7216 processPosition(mapper, x1, y1);
7217 processMTSync(mapper);
7218 processPosition(mapper, x2, y2);
7219 processMTSync(mapper);
7220 processSync(mapper);
7221
7222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7223 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7224 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7225 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7226 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7227 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7228 ASSERT_EQ(0, motionArgs.flags);
7229 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7230 ASSERT_EQ(0, motionArgs.buttonState);
7231 ASSERT_EQ(0, motionArgs.edgeFlags);
7232 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7233 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7234 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7235 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7236 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7237 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7238 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7239 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7240
7241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7242 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7243 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7244 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7245 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007246 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007247 ASSERT_EQ(0, motionArgs.flags);
7248 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7249 ASSERT_EQ(0, motionArgs.buttonState);
7250 ASSERT_EQ(0, motionArgs.edgeFlags);
7251 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7252 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7253 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7254 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7255 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7256 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7257 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7258 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7259 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7260 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7261 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7262 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7263
7264 // Move.
7265 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7266 processPosition(mapper, x1, y1);
7267 processMTSync(mapper);
7268 processPosition(mapper, x2, y2);
7269 processMTSync(mapper);
7270 processSync(mapper);
7271
7272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7273 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7274 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7275 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7276 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7277 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7278 ASSERT_EQ(0, motionArgs.flags);
7279 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7280 ASSERT_EQ(0, motionArgs.buttonState);
7281 ASSERT_EQ(0, motionArgs.edgeFlags);
7282 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7283 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7284 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7285 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7286 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7288 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7289 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7290 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7291 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7292 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7293 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7294
7295 // First finger up.
7296 x2 += 15; y2 -= 20;
7297 processPosition(mapper, x2, y2);
7298 processMTSync(mapper);
7299 processSync(mapper);
7300
7301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7302 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7303 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7304 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7305 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007306 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007307 ASSERT_EQ(0, motionArgs.flags);
7308 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7309 ASSERT_EQ(0, motionArgs.buttonState);
7310 ASSERT_EQ(0, motionArgs.edgeFlags);
7311 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7312 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7313 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7314 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7315 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7316 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7317 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7318 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7319 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7320 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7321 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7322 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7323
7324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7325 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7326 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7327 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7328 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7329 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7330 ASSERT_EQ(0, motionArgs.flags);
7331 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7332 ASSERT_EQ(0, motionArgs.buttonState);
7333 ASSERT_EQ(0, motionArgs.edgeFlags);
7334 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7335 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7336 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7337 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7338 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7339 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7340 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7341 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7342
7343 // Move.
7344 x2 += 20; y2 -= 25;
7345 processPosition(mapper, x2, y2);
7346 processMTSync(mapper);
7347 processSync(mapper);
7348
7349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7350 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7351 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7352 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7353 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7355 ASSERT_EQ(0, motionArgs.flags);
7356 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7357 ASSERT_EQ(0, motionArgs.buttonState);
7358 ASSERT_EQ(0, motionArgs.edgeFlags);
7359 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7360 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7361 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7363 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7364 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7365 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7366 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7367
7368 // New finger down.
7369 int32_t x3 = 700, y3 = 300;
7370 processPosition(mapper, x2, y2);
7371 processMTSync(mapper);
7372 processPosition(mapper, x3, y3);
7373 processMTSync(mapper);
7374 processSync(mapper);
7375
7376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7377 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7378 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7379 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7380 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007381 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007382 ASSERT_EQ(0, motionArgs.flags);
7383 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7384 ASSERT_EQ(0, motionArgs.buttonState);
7385 ASSERT_EQ(0, motionArgs.edgeFlags);
7386 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7387 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7388 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7389 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7390 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7392 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7393 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7394 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7395 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7396 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7397 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7398
7399 // Second finger up.
7400 x3 += 30; y3 -= 20;
7401 processPosition(mapper, x3, y3);
7402 processMTSync(mapper);
7403 processSync(mapper);
7404
7405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7406 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7407 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7408 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7409 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007410 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007411 ASSERT_EQ(0, motionArgs.flags);
7412 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7413 ASSERT_EQ(0, motionArgs.buttonState);
7414 ASSERT_EQ(0, motionArgs.edgeFlags);
7415 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7416 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7417 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7418 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7419 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7420 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7421 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7422 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7423 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7424 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7425 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7426 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7427
7428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7429 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7430 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7431 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7432 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7433 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7434 ASSERT_EQ(0, motionArgs.flags);
7435 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7436 ASSERT_EQ(0, motionArgs.buttonState);
7437 ASSERT_EQ(0, motionArgs.edgeFlags);
7438 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7439 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7440 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7442 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7443 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7444 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7445 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7446
7447 // Last finger up.
7448 processMTSync(mapper);
7449 processSync(mapper);
7450
7451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7452 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7453 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7454 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7455 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7456 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7457 ASSERT_EQ(0, motionArgs.flags);
7458 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7459 ASSERT_EQ(0, motionArgs.buttonState);
7460 ASSERT_EQ(0, motionArgs.edgeFlags);
7461 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7462 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7463 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7464 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7465 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7466 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7467 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7468 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7469
7470 // Should not have sent any more keys or motions.
7471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7473}
7474
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007475TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7476 addConfigurationProperty("touch.deviceType", "touchScreen");
7477 prepareDisplay(DISPLAY_ORIENTATION_0);
7478
7479 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7480 /*fuzz*/ 0, /*resolution*/ 10);
7481 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7482 /*fuzz*/ 0, /*resolution*/ 11);
7483 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7484 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7485 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7486 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7487 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7488 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7489 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7490 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7491
7492 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7493
7494 // X and Y axes
7495 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7496 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7497 // Touch major and minor
7498 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7499 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7500 // Tool major and minor
7501 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7502 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7503}
7504
7505TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7506 addConfigurationProperty("touch.deviceType", "touchScreen");
7507 prepareDisplay(DISPLAY_ORIENTATION_0);
7508
7509 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7510 /*fuzz*/ 0, /*resolution*/ 10);
7511 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7512 /*fuzz*/ 0, /*resolution*/ 11);
7513
7514 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7515
7516 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7517
7518 // Touch major and minor
7519 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7520 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7521 // Tool major and minor
7522 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7523 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7524}
7525
Michael Wrightd02c5b62014-02-10 15:10:22 -08007526TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007527 addConfigurationProperty("touch.deviceType", "touchScreen");
7528 prepareDisplay(DISPLAY_ORIENTATION_0);
7529 prepareAxes(POSITION | ID);
7530 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007531 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007532
arthurhungdcef2dc2020-08-11 14:47:50 +08007533 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007534
7535 NotifyMotionArgs motionArgs;
7536
7537 // Two fingers down at once.
7538 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7539 processPosition(mapper, x1, y1);
7540 processId(mapper, 1);
7541 processMTSync(mapper);
7542 processPosition(mapper, x2, y2);
7543 processId(mapper, 2);
7544 processMTSync(mapper);
7545 processSync(mapper);
7546
7547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7548 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7549 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7550 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7551 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7553 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7554
7555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007556 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007557 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7558 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7559 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7560 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7561 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7562 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7563 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7564 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7565 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7566
7567 // Move.
7568 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7569 processPosition(mapper, x1, y1);
7570 processId(mapper, 1);
7571 processMTSync(mapper);
7572 processPosition(mapper, x2, y2);
7573 processId(mapper, 2);
7574 processMTSync(mapper);
7575 processSync(mapper);
7576
7577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7578 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7579 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7580 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7581 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7582 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7583 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7585 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7586 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7587 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7588
7589 // First finger up.
7590 x2 += 15; y2 -= 20;
7591 processPosition(mapper, x2, y2);
7592 processId(mapper, 2);
7593 processMTSync(mapper);
7594 processSync(mapper);
7595
7596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007597 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007598 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7599 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7600 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7601 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7602 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7604 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7606 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7607
7608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7609 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7610 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7611 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7612 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7614 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7615
7616 // Move.
7617 x2 += 20; y2 -= 25;
7618 processPosition(mapper, x2, y2);
7619 processId(mapper, 2);
7620 processMTSync(mapper);
7621 processSync(mapper);
7622
7623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7624 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7625 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7626 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7627 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7628 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7629 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7630
7631 // New finger down.
7632 int32_t x3 = 700, y3 = 300;
7633 processPosition(mapper, x2, y2);
7634 processId(mapper, 2);
7635 processMTSync(mapper);
7636 processPosition(mapper, x3, y3);
7637 processId(mapper, 3);
7638 processMTSync(mapper);
7639 processSync(mapper);
7640
7641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007642 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007643 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7644 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7645 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7646 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7647 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7649 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7650 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7651 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7652
7653 // Second finger up.
7654 x3 += 30; y3 -= 20;
7655 processPosition(mapper, x3, y3);
7656 processId(mapper, 3);
7657 processMTSync(mapper);
7658 processSync(mapper);
7659
7660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007661 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007662 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7663 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7664 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7665 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7666 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7667 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7668 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7669 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7670 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7671
7672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7673 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7674 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7675 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7676 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7678 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7679
7680 // Last finger up.
7681 processMTSync(mapper);
7682 processSync(mapper);
7683
7684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7685 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7686 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7687 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7688 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7689 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7690 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7691
7692 // Should not have sent any more keys or motions.
7693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7695}
7696
7697TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007698 addConfigurationProperty("touch.deviceType", "touchScreen");
7699 prepareDisplay(DISPLAY_ORIENTATION_0);
7700 prepareAxes(POSITION | ID | SLOT);
7701 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007702 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007703
arthurhungdcef2dc2020-08-11 14:47:50 +08007704 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007705
7706 NotifyMotionArgs motionArgs;
7707
7708 // Two fingers down at once.
7709 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7710 processPosition(mapper, x1, y1);
7711 processId(mapper, 1);
7712 processSlot(mapper, 1);
7713 processPosition(mapper, x2, y2);
7714 processId(mapper, 2);
7715 processSync(mapper);
7716
7717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7718 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7719 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7720 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7721 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7722 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7723 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7724
7725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007726 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007727 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7728 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7729 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7730 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7731 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7732 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7733 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7734 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7735 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7736
7737 // Move.
7738 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7739 processSlot(mapper, 0);
7740 processPosition(mapper, x1, y1);
7741 processSlot(mapper, 1);
7742 processPosition(mapper, x2, y2);
7743 processSync(mapper);
7744
7745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7746 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7747 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7748 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7749 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7750 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7751 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7753 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7754 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7755 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7756
7757 // First finger up.
7758 x2 += 15; y2 -= 20;
7759 processSlot(mapper, 0);
7760 processId(mapper, -1);
7761 processSlot(mapper, 1);
7762 processPosition(mapper, x2, y2);
7763 processSync(mapper);
7764
7765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007766 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007767 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7768 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7769 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7770 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7771 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7772 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7773 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7774 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7775 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7776
7777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7778 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7779 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7780 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7781 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7782 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7783 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7784
7785 // Move.
7786 x2 += 20; y2 -= 25;
7787 processPosition(mapper, x2, y2);
7788 processSync(mapper);
7789
7790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7791 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7792 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7793 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7794 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7795 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7796 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7797
7798 // New finger down.
7799 int32_t x3 = 700, y3 = 300;
7800 processPosition(mapper, x2, y2);
7801 processSlot(mapper, 0);
7802 processId(mapper, 3);
7803 processPosition(mapper, x3, y3);
7804 processSync(mapper);
7805
7806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007807 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007808 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7809 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7810 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7811 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7812 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7814 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7815 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7816 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7817
7818 // Second finger up.
7819 x3 += 30; y3 -= 20;
7820 processSlot(mapper, 1);
7821 processId(mapper, -1);
7822 processSlot(mapper, 0);
7823 processPosition(mapper, x3, y3);
7824 processSync(mapper);
7825
7826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007827 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007828 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7829 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7830 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7831 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7832 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7833 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7834 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7836 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7837
7838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7839 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7840 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7841 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7842 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7843 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7844 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7845
7846 // Last finger up.
7847 processId(mapper, -1);
7848 processSync(mapper);
7849
7850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7851 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7852 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7853 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7854 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7855 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7856 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7857
7858 // Should not have sent any more keys or motions.
7859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7861}
7862
7863TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007864 addConfigurationProperty("touch.deviceType", "touchScreen");
7865 prepareDisplay(DISPLAY_ORIENTATION_0);
7866 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007867 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007868
7869 // These calculations are based on the input device calibration documentation.
7870 int32_t rawX = 100;
7871 int32_t rawY = 200;
7872 int32_t rawTouchMajor = 7;
7873 int32_t rawTouchMinor = 6;
7874 int32_t rawToolMajor = 9;
7875 int32_t rawToolMinor = 8;
7876 int32_t rawPressure = 11;
7877 int32_t rawDistance = 0;
7878 int32_t rawOrientation = 3;
7879 int32_t id = 5;
7880
7881 float x = toDisplayX(rawX);
7882 float y = toDisplayY(rawY);
7883 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7884 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7885 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7886 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7887 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7888 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7889 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7890 float distance = float(rawDistance);
7891
7892 processPosition(mapper, rawX, rawY);
7893 processTouchMajor(mapper, rawTouchMajor);
7894 processTouchMinor(mapper, rawTouchMinor);
7895 processToolMajor(mapper, rawToolMajor);
7896 processToolMinor(mapper, rawToolMinor);
7897 processPressure(mapper, rawPressure);
7898 processOrientation(mapper, rawOrientation);
7899 processDistance(mapper, rawDistance);
7900 processId(mapper, id);
7901 processMTSync(mapper);
7902 processSync(mapper);
7903
7904 NotifyMotionArgs args;
7905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7906 ASSERT_EQ(0, args.pointerProperties[0].id);
7907 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7908 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7909 orientation, distance));
7910}
7911
7912TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007913 addConfigurationProperty("touch.deviceType", "touchScreen");
7914 prepareDisplay(DISPLAY_ORIENTATION_0);
7915 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7916 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007917 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007918
7919 // These calculations are based on the input device calibration documentation.
7920 int32_t rawX = 100;
7921 int32_t rawY = 200;
7922 int32_t rawTouchMajor = 140;
7923 int32_t rawTouchMinor = 120;
7924 int32_t rawToolMajor = 180;
7925 int32_t rawToolMinor = 160;
7926
7927 float x = toDisplayX(rawX);
7928 float y = toDisplayY(rawY);
7929 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7930 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7931 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7932 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7933 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7934
7935 processPosition(mapper, rawX, rawY);
7936 processTouchMajor(mapper, rawTouchMajor);
7937 processTouchMinor(mapper, rawTouchMinor);
7938 processToolMajor(mapper, rawToolMajor);
7939 processToolMinor(mapper, rawToolMinor);
7940 processMTSync(mapper);
7941 processSync(mapper);
7942
7943 NotifyMotionArgs args;
7944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7945 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7946 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7947}
7948
7949TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007950 addConfigurationProperty("touch.deviceType", "touchScreen");
7951 prepareDisplay(DISPLAY_ORIENTATION_0);
7952 prepareAxes(POSITION | TOUCH | TOOL);
7953 addConfigurationProperty("touch.size.calibration", "diameter");
7954 addConfigurationProperty("touch.size.scale", "10");
7955 addConfigurationProperty("touch.size.bias", "160");
7956 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007957 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007958
7959 // These calculations are based on the input device calibration documentation.
7960 // Note: We only provide a single common touch/tool value because the device is assumed
7961 // not to emit separate values for each pointer (isSummed = 1).
7962 int32_t rawX = 100;
7963 int32_t rawY = 200;
7964 int32_t rawX2 = 150;
7965 int32_t rawY2 = 250;
7966 int32_t rawTouchMajor = 5;
7967 int32_t rawToolMajor = 8;
7968
7969 float x = toDisplayX(rawX);
7970 float y = toDisplayY(rawY);
7971 float x2 = toDisplayX(rawX2);
7972 float y2 = toDisplayY(rawY2);
7973 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7974 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7975 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7976
7977 processPosition(mapper, rawX, rawY);
7978 processTouchMajor(mapper, rawTouchMajor);
7979 processToolMajor(mapper, rawToolMajor);
7980 processMTSync(mapper);
7981 processPosition(mapper, rawX2, rawY2);
7982 processTouchMajor(mapper, rawTouchMajor);
7983 processToolMajor(mapper, rawToolMajor);
7984 processMTSync(mapper);
7985 processSync(mapper);
7986
7987 NotifyMotionArgs args;
7988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7989 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7990
7991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007992 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007993 ASSERT_EQ(size_t(2), args.pointerCount);
7994 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7995 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7996 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7997 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7998}
7999
8000TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008001 addConfigurationProperty("touch.deviceType", "touchScreen");
8002 prepareDisplay(DISPLAY_ORIENTATION_0);
8003 prepareAxes(POSITION | TOUCH | TOOL);
8004 addConfigurationProperty("touch.size.calibration", "area");
8005 addConfigurationProperty("touch.size.scale", "43");
8006 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008007 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008008
8009 // These calculations are based on the input device calibration documentation.
8010 int32_t rawX = 100;
8011 int32_t rawY = 200;
8012 int32_t rawTouchMajor = 5;
8013 int32_t rawToolMajor = 8;
8014
8015 float x = toDisplayX(rawX);
8016 float y = toDisplayY(rawY);
8017 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8018 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8019 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8020
8021 processPosition(mapper, rawX, rawY);
8022 processTouchMajor(mapper, rawTouchMajor);
8023 processToolMajor(mapper, rawToolMajor);
8024 processMTSync(mapper);
8025 processSync(mapper);
8026
8027 NotifyMotionArgs args;
8028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8029 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8030 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8031}
8032
8033TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008034 addConfigurationProperty("touch.deviceType", "touchScreen");
8035 prepareDisplay(DISPLAY_ORIENTATION_0);
8036 prepareAxes(POSITION | PRESSURE);
8037 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8038 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008039 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008040
Michael Wrightaa449c92017-12-13 21:21:43 +00008041 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008042 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008043 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8044 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8045 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8046
Michael Wrightd02c5b62014-02-10 15:10:22 -08008047 // These calculations are based on the input device calibration documentation.
8048 int32_t rawX = 100;
8049 int32_t rawY = 200;
8050 int32_t rawPressure = 60;
8051
8052 float x = toDisplayX(rawX);
8053 float y = toDisplayY(rawY);
8054 float pressure = float(rawPressure) * 0.01f;
8055
8056 processPosition(mapper, rawX, rawY);
8057 processPressure(mapper, rawPressure);
8058 processMTSync(mapper);
8059 processSync(mapper);
8060
8061 NotifyMotionArgs args;
8062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8063 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8064 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8065}
8066
8067TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008068 addConfigurationProperty("touch.deviceType", "touchScreen");
8069 prepareDisplay(DISPLAY_ORIENTATION_0);
8070 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008071 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008072
8073 NotifyMotionArgs motionArgs;
8074 NotifyKeyArgs keyArgs;
8075
8076 processId(mapper, 1);
8077 processPosition(mapper, 100, 200);
8078 processSync(mapper);
8079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8080 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8081 ASSERT_EQ(0, motionArgs.buttonState);
8082
8083 // press BTN_LEFT, release BTN_LEFT
8084 processKey(mapper, BTN_LEFT, 1);
8085 processSync(mapper);
8086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8087 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8088 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8089
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8091 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8092 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8093
Michael Wrightd02c5b62014-02-10 15:10:22 -08008094 processKey(mapper, BTN_LEFT, 0);
8095 processSync(mapper);
8096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008097 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008098 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008099
8100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008101 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008102 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008103
8104 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8105 processKey(mapper, BTN_RIGHT, 1);
8106 processKey(mapper, BTN_MIDDLE, 1);
8107 processSync(mapper);
8108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8109 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8110 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8111 motionArgs.buttonState);
8112
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8114 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8115 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8116
8117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8118 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8119 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8120 motionArgs.buttonState);
8121
Michael Wrightd02c5b62014-02-10 15:10:22 -08008122 processKey(mapper, BTN_RIGHT, 0);
8123 processSync(mapper);
8124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008125 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008126 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008127
8128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008129 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008130 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008131
8132 processKey(mapper, BTN_MIDDLE, 0);
8133 processSync(mapper);
8134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008135 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008136 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008137
8138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008139 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008140 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008141
8142 // press BTN_BACK, release BTN_BACK
8143 processKey(mapper, BTN_BACK, 1);
8144 processSync(mapper);
8145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8146 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8147 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008148
Michael Wrightd02c5b62014-02-10 15:10:22 -08008149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008150 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008151 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8152
8153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8154 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8155 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008156
8157 processKey(mapper, BTN_BACK, 0);
8158 processSync(mapper);
8159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008160 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008161 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008162
8163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008164 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008165 ASSERT_EQ(0, motionArgs.buttonState);
8166
Michael Wrightd02c5b62014-02-10 15:10:22 -08008167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8168 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8169 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8170
8171 // press BTN_SIDE, release BTN_SIDE
8172 processKey(mapper, BTN_SIDE, 1);
8173 processSync(mapper);
8174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8175 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8176 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008177
Michael Wrightd02c5b62014-02-10 15:10:22 -08008178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008179 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008180 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8181
8182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8183 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8184 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008185
8186 processKey(mapper, BTN_SIDE, 0);
8187 processSync(mapper);
8188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008189 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008190 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008191
8192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008193 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008194 ASSERT_EQ(0, motionArgs.buttonState);
8195
Michael Wrightd02c5b62014-02-10 15:10:22 -08008196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8197 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8198 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8199
8200 // press BTN_FORWARD, release BTN_FORWARD
8201 processKey(mapper, BTN_FORWARD, 1);
8202 processSync(mapper);
8203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8204 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8205 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008206
Michael Wrightd02c5b62014-02-10 15:10:22 -08008207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008208 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008209 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8210
8211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8212 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8213 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008214
8215 processKey(mapper, BTN_FORWARD, 0);
8216 processSync(mapper);
8217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008218 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008219 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008220
8221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008222 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008223 ASSERT_EQ(0, motionArgs.buttonState);
8224
Michael Wrightd02c5b62014-02-10 15:10:22 -08008225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8226 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8227 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8228
8229 // press BTN_EXTRA, release BTN_EXTRA
8230 processKey(mapper, BTN_EXTRA, 1);
8231 processSync(mapper);
8232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8233 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8234 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008235
Michael Wrightd02c5b62014-02-10 15:10:22 -08008236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008237 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008238 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8239
8240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8241 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8242 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008243
8244 processKey(mapper, BTN_EXTRA, 0);
8245 processSync(mapper);
8246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008247 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008248 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008249
8250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008251 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008252 ASSERT_EQ(0, motionArgs.buttonState);
8253
Michael Wrightd02c5b62014-02-10 15:10:22 -08008254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8255 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8256 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8257
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8259
Michael Wrightd02c5b62014-02-10 15:10:22 -08008260 // press BTN_STYLUS, release BTN_STYLUS
8261 processKey(mapper, BTN_STYLUS, 1);
8262 processSync(mapper);
8263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8264 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008265 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8266
8267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8268 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8269 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008270
8271 processKey(mapper, BTN_STYLUS, 0);
8272 processSync(mapper);
8273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008274 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008275 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008276
8277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008278 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008279 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008280
8281 // press BTN_STYLUS2, release BTN_STYLUS2
8282 processKey(mapper, BTN_STYLUS2, 1);
8283 processSync(mapper);
8284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8285 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008286 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8287
8288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8289 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8290 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008291
8292 processKey(mapper, BTN_STYLUS2, 0);
8293 processSync(mapper);
8294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008295 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008296 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008297
8298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008299 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008300 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008301
8302 // release touch
8303 processId(mapper, -1);
8304 processSync(mapper);
8305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8306 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8307 ASSERT_EQ(0, motionArgs.buttonState);
8308}
8309
8310TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008311 addConfigurationProperty("touch.deviceType", "touchScreen");
8312 prepareDisplay(DISPLAY_ORIENTATION_0);
8313 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008314 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008315
8316 NotifyMotionArgs motionArgs;
8317
8318 // default tool type is finger
8319 processId(mapper, 1);
8320 processPosition(mapper, 100, 200);
8321 processSync(mapper);
8322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8323 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8324 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8325
8326 // eraser
8327 processKey(mapper, BTN_TOOL_RUBBER, 1);
8328 processSync(mapper);
8329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8330 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8331 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8332
8333 // stylus
8334 processKey(mapper, BTN_TOOL_RUBBER, 0);
8335 processKey(mapper, BTN_TOOL_PEN, 1);
8336 processSync(mapper);
8337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8338 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8339 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8340
8341 // brush
8342 processKey(mapper, BTN_TOOL_PEN, 0);
8343 processKey(mapper, BTN_TOOL_BRUSH, 1);
8344 processSync(mapper);
8345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8346 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8347 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8348
8349 // pencil
8350 processKey(mapper, BTN_TOOL_BRUSH, 0);
8351 processKey(mapper, BTN_TOOL_PENCIL, 1);
8352 processSync(mapper);
8353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8355 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8356
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008357 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008358 processKey(mapper, BTN_TOOL_PENCIL, 0);
8359 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8360 processSync(mapper);
8361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8362 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8363 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8364
8365 // mouse
8366 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8367 processKey(mapper, BTN_TOOL_MOUSE, 1);
8368 processSync(mapper);
8369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8370 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8371 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8372
8373 // lens
8374 processKey(mapper, BTN_TOOL_MOUSE, 0);
8375 processKey(mapper, BTN_TOOL_LENS, 1);
8376 processSync(mapper);
8377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8378 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8379 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8380
8381 // double-tap
8382 processKey(mapper, BTN_TOOL_LENS, 0);
8383 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8384 processSync(mapper);
8385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8386 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8387 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8388
8389 // triple-tap
8390 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8391 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8392 processSync(mapper);
8393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8394 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8395 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8396
8397 // quad-tap
8398 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8399 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8400 processSync(mapper);
8401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8402 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8403 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8404
8405 // finger
8406 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8407 processKey(mapper, BTN_TOOL_FINGER, 1);
8408 processSync(mapper);
8409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8411 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8412
8413 // stylus trumps finger
8414 processKey(mapper, BTN_TOOL_PEN, 1);
8415 processSync(mapper);
8416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8417 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8418 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8419
8420 // eraser trumps stylus
8421 processKey(mapper, BTN_TOOL_RUBBER, 1);
8422 processSync(mapper);
8423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8424 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8425 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8426
8427 // mouse trumps eraser
8428 processKey(mapper, BTN_TOOL_MOUSE, 1);
8429 processSync(mapper);
8430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8432 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8433
8434 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8435 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8436 processSync(mapper);
8437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8438 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8439 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8440
8441 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8442 processToolType(mapper, MT_TOOL_PEN);
8443 processSync(mapper);
8444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8445 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8446 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8447
8448 // back to default tool type
8449 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8450 processKey(mapper, BTN_TOOL_MOUSE, 0);
8451 processKey(mapper, BTN_TOOL_RUBBER, 0);
8452 processKey(mapper, BTN_TOOL_PEN, 0);
8453 processKey(mapper, BTN_TOOL_FINGER, 0);
8454 processSync(mapper);
8455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8456 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8457 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8458}
8459
8460TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008461 addConfigurationProperty("touch.deviceType", "touchScreen");
8462 prepareDisplay(DISPLAY_ORIENTATION_0);
8463 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008464 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008465 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008466
8467 NotifyMotionArgs motionArgs;
8468
8469 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8470 processId(mapper, 1);
8471 processPosition(mapper, 100, 200);
8472 processSync(mapper);
8473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8474 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8476 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8477
8478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8479 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8480 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8481 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8482
8483 // move a little
8484 processPosition(mapper, 150, 250);
8485 processSync(mapper);
8486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8487 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8488 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8489 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8490
8491 // down when BTN_TOUCH is pressed, pressure defaults to 1
8492 processKey(mapper, BTN_TOUCH, 1);
8493 processSync(mapper);
8494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8495 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8497 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8498
8499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8500 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8501 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8502 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8503
8504 // up when BTN_TOUCH is released, hover restored
8505 processKey(mapper, BTN_TOUCH, 0);
8506 processSync(mapper);
8507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8508 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8510 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8511
8512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8513 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8514 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8515 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8516
8517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8518 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8519 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8520 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8521
8522 // exit hover when pointer goes away
8523 processId(mapper, -1);
8524 processSync(mapper);
8525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8526 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8527 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8528 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8529}
8530
8531TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008532 addConfigurationProperty("touch.deviceType", "touchScreen");
8533 prepareDisplay(DISPLAY_ORIENTATION_0);
8534 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008535 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008536
8537 NotifyMotionArgs motionArgs;
8538
8539 // initially hovering because pressure is 0
8540 processId(mapper, 1);
8541 processPosition(mapper, 100, 200);
8542 processPressure(mapper, 0);
8543 processSync(mapper);
8544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8545 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8546 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8547 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8548
8549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8550 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8551 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8552 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8553
8554 // move a little
8555 processPosition(mapper, 150, 250);
8556 processSync(mapper);
8557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8558 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8560 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8561
8562 // down when pressure becomes non-zero
8563 processPressure(mapper, RAW_PRESSURE_MAX);
8564 processSync(mapper);
8565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8566 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8567 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8568 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8569
8570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8571 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8572 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8573 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8574
8575 // up when pressure becomes 0, hover restored
8576 processPressure(mapper, 0);
8577 processSync(mapper);
8578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8579 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8581 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8582
8583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8584 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8585 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8586 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8587
8588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8589 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8590 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8591 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8592
8593 // exit hover when pointer goes away
8594 processId(mapper, -1);
8595 processSync(mapper);
8596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8597 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8599 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8600}
8601
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008602/**
8603 * Set the input device port <--> display port associations, and check that the
8604 * events are routed to the display that matches the display port.
8605 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8606 */
8607TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008608 const std::string usb2 = "USB2";
8609 const uint8_t hdmi1 = 0;
8610 const uint8_t hdmi2 = 1;
8611 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008612 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008613
8614 addConfigurationProperty("touch.deviceType", "touchScreen");
8615 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008616 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008617
8618 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8619 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8620
8621 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8622 // for this input device is specified, and the matching viewport is not present,
8623 // the input device should be disabled (at the mapper level).
8624
8625 // Add viewport for display 2 on hdmi2
8626 prepareSecondaryDisplay(type, hdmi2);
8627 // Send a touch event
8628 processPosition(mapper, 100, 100);
8629 processSync(mapper);
8630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8631
8632 // Add viewport for display 1 on hdmi1
8633 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8634 // Send a touch event again
8635 processPosition(mapper, 100, 100);
8636 processSync(mapper);
8637
8638 NotifyMotionArgs args;
8639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8640 ASSERT_EQ(DISPLAY_ID, args.displayId);
8641}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008642
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008643TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8644 addConfigurationProperty("touch.deviceType", "touchScreen");
8645 prepareAxes(POSITION);
8646 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8647
8648 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8649
8650 prepareDisplay(DISPLAY_ORIENTATION_0);
8651 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8652
8653 // Send a touch event
8654 processPosition(mapper, 100, 100);
8655 processSync(mapper);
8656
8657 NotifyMotionArgs args;
8658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8659 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8660}
8661
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008662TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008663 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008664 std::shared_ptr<FakePointerController> fakePointerController =
8665 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008666 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008667 fakePointerController->setPosition(100, 200);
8668 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008669 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008670
Garfield Tan888a6a42020-01-09 11:39:16 -08008671 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008672 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008673
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008674 prepareDisplay(DISPLAY_ORIENTATION_0);
8675 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008676 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008677
8678 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008679 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008680
8681 NotifyMotionArgs motionArgs;
8682 processPosition(mapper, 100, 100);
8683 processSync(mapper);
8684
8685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8686 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8687 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8688}
8689
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008690/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008691 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8692 */
8693TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8694 addConfigurationProperty("touch.deviceType", "touchScreen");
8695 prepareAxes(POSITION);
8696 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8697
8698 prepareDisplay(DISPLAY_ORIENTATION_0);
8699 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8700 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8701 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8702 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8703
8704 NotifyMotionArgs args;
8705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8706 ASSERT_EQ(26, args.readTime);
8707
8708 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8709 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8710 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8711
8712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8713 ASSERT_EQ(33, args.readTime);
8714}
8715
8716/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008717 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8718 * events should not be delivered to the listener.
8719 */
8720TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8721 addConfigurationProperty("touch.deviceType", "touchScreen");
8722 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8723 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8724 ViewportType::INTERNAL);
8725 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8726 prepareAxes(POSITION);
8727 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8728
8729 NotifyMotionArgs motionArgs;
8730 processPosition(mapper, 100, 100);
8731 processSync(mapper);
8732
8733 mFakeListener->assertNotifyMotionWasNotCalled();
8734}
8735
Garfield Tanc734e4f2021-01-15 20:01:39 -08008736TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8737 addConfigurationProperty("touch.deviceType", "touchScreen");
8738 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8739 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8740 ViewportType::INTERNAL);
8741 std::optional<DisplayViewport> optionalDisplayViewport =
8742 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8743 ASSERT_TRUE(optionalDisplayViewport.has_value());
8744 DisplayViewport displayViewport = *optionalDisplayViewport;
8745
8746 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8747 prepareAxes(POSITION);
8748 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8749
8750 // Finger down
8751 int32_t x = 100, y = 100;
8752 processPosition(mapper, x, y);
8753 processSync(mapper);
8754
8755 NotifyMotionArgs motionArgs;
8756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8757 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8758
8759 // Deactivate display viewport
8760 displayViewport.isActive = false;
8761 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8762 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8763
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008764 // The ongoing touch should be canceled immediately
8765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8766 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8767
8768 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008769 x += 10, y += 10;
8770 processPosition(mapper, x, y);
8771 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008773
8774 // Reactivate display viewport
8775 displayViewport.isActive = true;
8776 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8777 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8778
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008779 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008780 x += 10, y += 10;
8781 processPosition(mapper, x, y);
8782 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8784 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008785}
8786
Arthur Hung7c645402019-01-25 17:45:42 +08008787TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8788 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008789 prepareAxes(POSITION | ID | SLOT);
8790 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008791 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008792
8793 // Create the second touch screen device, and enable multi fingers.
8794 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008795 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008796 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008797 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008798 std::shared_ptr<InputDevice> device2 =
8799 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008800 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008801
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008802 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8803 0 /*flat*/, 0 /*fuzz*/);
8804 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8805 0 /*flat*/, 0 /*fuzz*/);
8806 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8807 0 /*flat*/, 0 /*fuzz*/);
8808 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8809 0 /*flat*/, 0 /*fuzz*/);
8810 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8811 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8812 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008813
8814 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008815 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008816 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8817 device2->reset(ARBITRARY_TIME);
8818
8819 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008820 std::shared_ptr<FakePointerController> fakePointerController =
8821 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008822 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008823
8824 // Setup policy for associated displays and show touches.
8825 const uint8_t hdmi1 = 0;
8826 const uint8_t hdmi2 = 1;
8827 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8828 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8829 mFakePolicy->setShowTouches(true);
8830
8831 // Create displays.
8832 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008833 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008834
8835 // Default device will reconfigure above, need additional reconfiguration for another device.
8836 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan197e0862022-07-01 14:28:00 +00008837 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8838 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008839
8840 // Two fingers down at default display.
8841 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8842 processPosition(mapper, x1, y1);
8843 processId(mapper, 1);
8844 processSlot(mapper, 1);
8845 processPosition(mapper, x2, y2);
8846 processId(mapper, 2);
8847 processSync(mapper);
8848
8849 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8850 fakePointerController->getSpots().find(DISPLAY_ID);
8851 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8852 ASSERT_EQ(size_t(2), iter->second.size());
8853
8854 // Two fingers down at second display.
8855 processPosition(mapper2, x1, y1);
8856 processId(mapper2, 1);
8857 processSlot(mapper2, 1);
8858 processPosition(mapper2, x2, y2);
8859 processId(mapper2, 2);
8860 processSync(mapper2);
8861
8862 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8863 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8864 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008865
8866 // Disable the show touches configuration and ensure the spots are cleared.
8867 mFakePolicy->setShowTouches(false);
8868 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8869 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
8870
8871 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008872}
8873
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008874TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008875 prepareAxes(POSITION);
8876 addConfigurationProperty("touch.deviceType", "touchScreen");
8877 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008878 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008879
8880 NotifyMotionArgs motionArgs;
8881 // Unrotated video frame
8882 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8883 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008884 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008885 processPosition(mapper, 100, 200);
8886 processSync(mapper);
8887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8888 ASSERT_EQ(frames, motionArgs.videoFrames);
8889
8890 // Subsequent touch events should not have any videoframes
8891 // This is implemented separately in FakeEventHub,
8892 // but that should match the behaviour of TouchVideoDevice.
8893 processPosition(mapper, 200, 200);
8894 processSync(mapper);
8895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8896 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8897}
8898
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008899TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008900 prepareAxes(POSITION);
8901 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008902 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008903 // Unrotated video frame
8904 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8905 NotifyMotionArgs motionArgs;
8906
8907 // Test all 4 orientations
8908 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008909 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8910 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8911 clearViewports();
8912 prepareDisplay(orientation);
8913 std::vector<TouchVideoFrame> frames{frame};
8914 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8915 processPosition(mapper, 100, 200);
8916 processSync(mapper);
8917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8918 ASSERT_EQ(frames, motionArgs.videoFrames);
8919 }
8920}
8921
8922TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8923 prepareAxes(POSITION);
8924 addConfigurationProperty("touch.deviceType", "touchScreen");
8925 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8926 // orientation-aware are affected by display rotation.
8927 addConfigurationProperty("touch.orientationAware", "0");
8928 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8929 // Unrotated video frame
8930 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8931 NotifyMotionArgs motionArgs;
8932
8933 // Test all 4 orientations
8934 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008935 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8936 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8937 clearViewports();
8938 prepareDisplay(orientation);
8939 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008940 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008941 processPosition(mapper, 100, 200);
8942 processSync(mapper);
8943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008944 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8945 // compared to the display. This is so that when the window transform (which contains the
8946 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8947 // window's coordinate space.
8948 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008949 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08008950
8951 // Release finger.
8952 processSync(mapper);
8953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008954 }
8955}
8956
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008957TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008958 prepareAxes(POSITION);
8959 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008960 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008961 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8962 // so mix these.
8963 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8964 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8965 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8966 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8967 NotifyMotionArgs motionArgs;
8968
8969 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008970 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008971 processPosition(mapper, 100, 200);
8972 processSync(mapper);
8973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008974 ASSERT_EQ(frames, motionArgs.videoFrames);
8975}
8976
8977TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8978 prepareAxes(POSITION);
8979 addConfigurationProperty("touch.deviceType", "touchScreen");
8980 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8981 // orientation-aware are affected by display rotation.
8982 addConfigurationProperty("touch.orientationAware", "0");
8983 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8984 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8985 // so mix these.
8986 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8987 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8988 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8989 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8990 NotifyMotionArgs motionArgs;
8991
8992 prepareDisplay(DISPLAY_ORIENTATION_90);
8993 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8994 processPosition(mapper, 100, 200);
8995 processSync(mapper);
8996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8997 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8998 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8999 // compared to the display. This is so that when the window transform (which contains the
9000 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9001 // window's coordinate space.
9002 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
9003 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009004 ASSERT_EQ(frames, motionArgs.videoFrames);
9005}
9006
Arthur Hung9da14732019-09-02 16:16:58 +08009007/**
9008 * If we had defined port associations, but the viewport is not ready, the touch device would be
9009 * expected to be disabled, and it should be enabled after the viewport has found.
9010 */
9011TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009012 constexpr uint8_t hdmi2 = 1;
9013 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009014 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009015
9016 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9017
9018 addConfigurationProperty("touch.deviceType", "touchScreen");
9019 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009020 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009021
9022 ASSERT_EQ(mDevice->isEnabled(), false);
9023
9024 // Add display on hdmi2, the device should be enabled and can receive touch event.
9025 prepareSecondaryDisplay(type, hdmi2);
9026 ASSERT_EQ(mDevice->isEnabled(), true);
9027
9028 // Send a touch event.
9029 processPosition(mapper, 100, 100);
9030 processSync(mapper);
9031
9032 NotifyMotionArgs args;
9033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9034 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9035}
9036
Arthur Hung421eb1c2020-01-16 00:09:42 +08009037TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009038 addConfigurationProperty("touch.deviceType", "touchScreen");
9039 prepareDisplay(DISPLAY_ORIENTATION_0);
9040 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009041 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009042
9043 NotifyMotionArgs motionArgs;
9044
9045 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9046 // finger down
9047 processId(mapper, 1);
9048 processPosition(mapper, x1, y1);
9049 processSync(mapper);
9050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9051 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9052 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9053
9054 // finger move
9055 processId(mapper, 1);
9056 processPosition(mapper, x2, y2);
9057 processSync(mapper);
9058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9060 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9061
9062 // finger up.
9063 processId(mapper, -1);
9064 processSync(mapper);
9065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9066 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9067 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9068
9069 // new finger down
9070 processId(mapper, 1);
9071 processPosition(mapper, x3, y3);
9072 processSync(mapper);
9073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9074 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9075 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9076}
9077
9078/**
arthurhungcc7f9802020-04-30 17:55:40 +08009079 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9080 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009081 */
arthurhungcc7f9802020-04-30 17:55:40 +08009082TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009083 addConfigurationProperty("touch.deviceType", "touchScreen");
9084 prepareDisplay(DISPLAY_ORIENTATION_0);
9085 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009086 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009087
9088 NotifyMotionArgs motionArgs;
9089
9090 // default tool type is finger
9091 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009092 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009093 processPosition(mapper, x1, y1);
9094 processSync(mapper);
9095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9096 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9097 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9098
9099 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9100 processToolType(mapper, MT_TOOL_PALM);
9101 processSync(mapper);
9102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9103 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9104
9105 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009106 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009107 processPosition(mapper, x2, y2);
9108 processSync(mapper);
9109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9110
9111 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009112 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009113 processSync(mapper);
9114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9115
9116 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009117 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009118 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009119 processPosition(mapper, x3, y3);
9120 processSync(mapper);
9121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9122 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9123 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9124}
9125
arthurhungbf89a482020-04-17 17:37:55 +08009126/**
arthurhungcc7f9802020-04-30 17:55:40 +08009127 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9128 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009129 */
arthurhungcc7f9802020-04-30 17:55:40 +08009130TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009131 addConfigurationProperty("touch.deviceType", "touchScreen");
9132 prepareDisplay(DISPLAY_ORIENTATION_0);
9133 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9134 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9135
9136 NotifyMotionArgs motionArgs;
9137
9138 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009139 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9140 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009141 processPosition(mapper, x1, y1);
9142 processSync(mapper);
9143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9144 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9145 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9146
9147 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009148 processSlot(mapper, SECOND_SLOT);
9149 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009150 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009151 processSync(mapper);
9152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009153 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009154 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9155
9156 // If the tool type of the first finger changes to MT_TOOL_PALM,
9157 // we expect to receive ACTION_POINTER_UP with cancel flag.
9158 processSlot(mapper, FIRST_SLOT);
9159 processId(mapper, FIRST_TRACKING_ID);
9160 processToolType(mapper, MT_TOOL_PALM);
9161 processSync(mapper);
9162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009163 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009164 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9165
9166 // The following MOVE events of second finger should be processed.
9167 processSlot(mapper, SECOND_SLOT);
9168 processId(mapper, SECOND_TRACKING_ID);
9169 processPosition(mapper, x2 + 1, y2 + 1);
9170 processSync(mapper);
9171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9172 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9173 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9174
9175 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9176 // it. Second finger receive move.
9177 processSlot(mapper, FIRST_SLOT);
9178 processId(mapper, INVALID_TRACKING_ID);
9179 processSync(mapper);
9180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9181 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9182 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9183
9184 // Second finger keeps moving.
9185 processSlot(mapper, SECOND_SLOT);
9186 processId(mapper, SECOND_TRACKING_ID);
9187 processPosition(mapper, x2 + 2, y2 + 2);
9188 processSync(mapper);
9189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9190 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9191 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9192
9193 // Second finger up.
9194 processId(mapper, INVALID_TRACKING_ID);
9195 processSync(mapper);
9196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9197 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9198 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9199}
9200
9201/**
9202 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9203 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9204 */
9205TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9206 addConfigurationProperty("touch.deviceType", "touchScreen");
9207 prepareDisplay(DISPLAY_ORIENTATION_0);
9208 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9209 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9210
9211 NotifyMotionArgs motionArgs;
9212
9213 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9214 // First finger down.
9215 processId(mapper, FIRST_TRACKING_ID);
9216 processPosition(mapper, x1, y1);
9217 processSync(mapper);
9218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9219 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9220 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9221
9222 // Second finger down.
9223 processSlot(mapper, SECOND_SLOT);
9224 processId(mapper, SECOND_TRACKING_ID);
9225 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009226 processSync(mapper);
9227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009228 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009229 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9230
arthurhungcc7f9802020-04-30 17:55:40 +08009231 // If the tool type of the first finger changes to MT_TOOL_PALM,
9232 // we expect to receive ACTION_POINTER_UP with cancel flag.
9233 processSlot(mapper, FIRST_SLOT);
9234 processId(mapper, FIRST_TRACKING_ID);
9235 processToolType(mapper, MT_TOOL_PALM);
9236 processSync(mapper);
9237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009238 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009239 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9240
9241 // Second finger keeps moving.
9242 processSlot(mapper, SECOND_SLOT);
9243 processId(mapper, SECOND_TRACKING_ID);
9244 processPosition(mapper, x2 + 1, y2 + 1);
9245 processSync(mapper);
9246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9247 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9248
9249 // second finger becomes palm, receive cancel due to only 1 finger is active.
9250 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009251 processToolType(mapper, MT_TOOL_PALM);
9252 processSync(mapper);
9253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9254 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9255
arthurhungcc7f9802020-04-30 17:55:40 +08009256 // third finger down.
9257 processSlot(mapper, THIRD_SLOT);
9258 processId(mapper, THIRD_TRACKING_ID);
9259 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009260 processPosition(mapper, x3, y3);
9261 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9263 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9264 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009265 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9266
9267 // third finger move
9268 processId(mapper, THIRD_TRACKING_ID);
9269 processPosition(mapper, x3 + 1, y3 + 1);
9270 processSync(mapper);
9271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9272 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9273
9274 // first finger up, third finger receive move.
9275 processSlot(mapper, FIRST_SLOT);
9276 processId(mapper, INVALID_TRACKING_ID);
9277 processSync(mapper);
9278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9279 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9280 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9281
9282 // second finger up, third finger receive move.
9283 processSlot(mapper, SECOND_SLOT);
9284 processId(mapper, INVALID_TRACKING_ID);
9285 processSync(mapper);
9286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9287 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9288 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9289
9290 // third finger up.
9291 processSlot(mapper, THIRD_SLOT);
9292 processId(mapper, INVALID_TRACKING_ID);
9293 processSync(mapper);
9294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9295 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9296 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9297}
9298
9299/**
9300 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9301 * and the active finger could still be allowed to receive the events
9302 */
9303TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9304 addConfigurationProperty("touch.deviceType", "touchScreen");
9305 prepareDisplay(DISPLAY_ORIENTATION_0);
9306 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9307 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9308
9309 NotifyMotionArgs motionArgs;
9310
9311 // default tool type is finger
9312 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9313 processId(mapper, FIRST_TRACKING_ID);
9314 processPosition(mapper, x1, y1);
9315 processSync(mapper);
9316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9317 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9318 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9319
9320 // Second finger down.
9321 processSlot(mapper, SECOND_SLOT);
9322 processId(mapper, SECOND_TRACKING_ID);
9323 processPosition(mapper, x2, y2);
9324 processSync(mapper);
9325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009326 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009327 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9328
9329 // If the tool type of the second finger changes to MT_TOOL_PALM,
9330 // we expect to receive ACTION_POINTER_UP with cancel flag.
9331 processId(mapper, SECOND_TRACKING_ID);
9332 processToolType(mapper, MT_TOOL_PALM);
9333 processSync(mapper);
9334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009335 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009336 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9337
9338 // The following MOVE event should be processed.
9339 processSlot(mapper, FIRST_SLOT);
9340 processId(mapper, FIRST_TRACKING_ID);
9341 processPosition(mapper, x1 + 1, y1 + 1);
9342 processSync(mapper);
9343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9344 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9345 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9346
9347 // second finger up.
9348 processSlot(mapper, SECOND_SLOT);
9349 processId(mapper, INVALID_TRACKING_ID);
9350 processSync(mapper);
9351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9352 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9353
9354 // first finger keep moving
9355 processSlot(mapper, FIRST_SLOT);
9356 processId(mapper, FIRST_TRACKING_ID);
9357 processPosition(mapper, x1 + 2, y1 + 2);
9358 processSync(mapper);
9359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9360 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9361
9362 // first finger up.
9363 processId(mapper, INVALID_TRACKING_ID);
9364 processSync(mapper);
9365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9366 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9367 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009368}
9369
Arthur Hung9ad18942021-06-19 02:04:46 +00009370/**
9371 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9372 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9373 * cause slot be valid again.
9374 */
9375TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9376 addConfigurationProperty("touch.deviceType", "touchScreen");
9377 prepareDisplay(DISPLAY_ORIENTATION_0);
9378 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9379 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9380
9381 NotifyMotionArgs motionArgs;
9382
9383 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9384 // First finger down.
9385 processId(mapper, FIRST_TRACKING_ID);
9386 processPosition(mapper, x1, y1);
9387 processPressure(mapper, RAW_PRESSURE_MAX);
9388 processSync(mapper);
9389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9390 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9391 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9392
9393 // First finger move.
9394 processId(mapper, FIRST_TRACKING_ID);
9395 processPosition(mapper, x1 + 1, y1 + 1);
9396 processPressure(mapper, RAW_PRESSURE_MAX);
9397 processSync(mapper);
9398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9399 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9400 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9401
9402 // Second finger down.
9403 processSlot(mapper, SECOND_SLOT);
9404 processId(mapper, SECOND_TRACKING_ID);
9405 processPosition(mapper, x2, y2);
9406 processPressure(mapper, RAW_PRESSURE_MAX);
9407 processSync(mapper);
9408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009409 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009410 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9411
9412 // second finger up with some unexpected data.
9413 processSlot(mapper, SECOND_SLOT);
9414 processId(mapper, INVALID_TRACKING_ID);
9415 processPosition(mapper, x2, y2);
9416 processSync(mapper);
9417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009418 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009419 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9420
9421 // first finger up with some unexpected data.
9422 processSlot(mapper, FIRST_SLOT);
9423 processId(mapper, INVALID_TRACKING_ID);
9424 processPosition(mapper, x2, y2);
9425 processPressure(mapper, RAW_PRESSURE_MAX);
9426 processSync(mapper);
9427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9428 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9429 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9430}
9431
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009432// --- MultiTouchInputMapperTest_ExternalDevice ---
9433
9434class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9435protected:
Chris Yea52ade12020-08-27 16:49:20 -07009436 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009437};
9438
9439/**
9440 * Expect fallback to internal viewport if device is external and external viewport is not present.
9441 */
9442TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9443 prepareAxes(POSITION);
9444 addConfigurationProperty("touch.deviceType", "touchScreen");
9445 prepareDisplay(DISPLAY_ORIENTATION_0);
9446 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9447
9448 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9449
9450 NotifyMotionArgs motionArgs;
9451
9452 // Expect the event to be sent to the internal viewport,
9453 // because an external viewport is not present.
9454 processPosition(mapper, 100, 100);
9455 processSync(mapper);
9456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9457 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9458
9459 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009460 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009461 processPosition(mapper, 100, 100);
9462 processSync(mapper);
9463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9464 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9465}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009466
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009467TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9468 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9469 std::shared_ptr<FakePointerController> fakePointerController =
9470 std::make_shared<FakePointerController>();
9471 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9472 fakePointerController->setPosition(0, 0);
9473 fakePointerController->setButtonState(0);
9474
9475 // prepare device and capture
9476 prepareDisplay(DISPLAY_ORIENTATION_0);
9477 prepareAxes(POSITION | ID | SLOT);
9478 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9479 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9480 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009481 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009482 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9483
9484 // captured touchpad should be a touchpad source
9485 NotifyDeviceResetArgs resetArgs;
9486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9487 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9488
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009489 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009490
9491 const InputDeviceInfo::MotionRange* relRangeX =
9492 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9493 ASSERT_NE(relRangeX, nullptr);
9494 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9495 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9496 const InputDeviceInfo::MotionRange* relRangeY =
9497 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9498 ASSERT_NE(relRangeY, nullptr);
9499 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9500 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9501
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009502 // run captured pointer tests - note that this is unscaled, so input listener events should be
9503 // identical to what the hardware sends (accounting for any
9504 // calibration).
9505 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009506 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009507 processId(mapper, 1);
9508 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9509 processKey(mapper, BTN_TOUCH, 1);
9510 processSync(mapper);
9511
9512 // expect coord[0] to contain initial location of touch 0
9513 NotifyMotionArgs args;
9514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9515 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9516 ASSERT_EQ(1U, args.pointerCount);
9517 ASSERT_EQ(0, args.pointerProperties[0].id);
9518 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9519 ASSERT_NO_FATAL_FAILURE(
9520 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9521
9522 // FINGER 1 DOWN
9523 processSlot(mapper, 1);
9524 processId(mapper, 2);
9525 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9526 processSync(mapper);
9527
9528 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009530 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009531 ASSERT_EQ(2U, args.pointerCount);
9532 ASSERT_EQ(0, args.pointerProperties[0].id);
9533 ASSERT_EQ(1, args.pointerProperties[1].id);
9534 ASSERT_NO_FATAL_FAILURE(
9535 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9536 ASSERT_NO_FATAL_FAILURE(
9537 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9538
9539 // FINGER 1 MOVE
9540 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9541 processSync(mapper);
9542
9543 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9544 // from move
9545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9546 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9547 ASSERT_NO_FATAL_FAILURE(
9548 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9549 ASSERT_NO_FATAL_FAILURE(
9550 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9551
9552 // FINGER 0 MOVE
9553 processSlot(mapper, 0);
9554 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9555 processSync(mapper);
9556
9557 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9559 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9560 ASSERT_NO_FATAL_FAILURE(
9561 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9562 ASSERT_NO_FATAL_FAILURE(
9563 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9564
9565 // BUTTON DOWN
9566 processKey(mapper, BTN_LEFT, 1);
9567 processSync(mapper);
9568
9569 // touchinputmapper design sends a move before button press
9570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9571 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9573 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9574
9575 // BUTTON UP
9576 processKey(mapper, BTN_LEFT, 0);
9577 processSync(mapper);
9578
9579 // touchinputmapper design sends a move after button release
9580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9581 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9583 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9584
9585 // FINGER 0 UP
9586 processId(mapper, -1);
9587 processSync(mapper);
9588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9589 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9590
9591 // FINGER 1 MOVE
9592 processSlot(mapper, 1);
9593 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9594 processSync(mapper);
9595
9596 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9598 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9599 ASSERT_EQ(1U, args.pointerCount);
9600 ASSERT_EQ(1, args.pointerProperties[0].id);
9601 ASSERT_NO_FATAL_FAILURE(
9602 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9603
9604 // FINGER 1 UP
9605 processId(mapper, -1);
9606 processKey(mapper, BTN_TOUCH, 0);
9607 processSync(mapper);
9608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9609 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9610
9611 // non captured touchpad should be a mouse source
9612 mFakePolicy->setPointerCapture(false);
9613 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9615 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9616}
9617
9618TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9619 std::shared_ptr<FakePointerController> fakePointerController =
9620 std::make_shared<FakePointerController>();
9621 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9622 fakePointerController->setPosition(0, 0);
9623 fakePointerController->setButtonState(0);
9624
9625 // prepare device and capture
9626 prepareDisplay(DISPLAY_ORIENTATION_0);
9627 prepareAxes(POSITION | ID | SLOT);
9628 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9629 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009630 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009631 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9632 // run uncaptured pointer tests - pushes out generic events
9633 // FINGER 0 DOWN
9634 processId(mapper, 3);
9635 processPosition(mapper, 100, 100);
9636 processKey(mapper, BTN_TOUCH, 1);
9637 processSync(mapper);
9638
9639 // start at (100,100), cursor should be at (0,0) * scale
9640 NotifyMotionArgs args;
9641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9642 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9643 ASSERT_NO_FATAL_FAILURE(
9644 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9645
9646 // FINGER 0 MOVE
9647 processPosition(mapper, 200, 200);
9648 processSync(mapper);
9649
9650 // compute scaling to help with touch position checking
9651 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9652 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9653 float scale =
9654 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9655
9656 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9658 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9660 0, 0, 0, 0, 0, 0, 0));
9661}
9662
9663TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9664 std::shared_ptr<FakePointerController> fakePointerController =
9665 std::make_shared<FakePointerController>();
9666
9667 prepareDisplay(DISPLAY_ORIENTATION_0);
9668 prepareAxes(POSITION | ID | SLOT);
9669 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009670 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009671 mFakePolicy->setPointerCapture(false);
9672 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9673
9674 // uncaptured touchpad should be a pointer device
9675 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9676
9677 // captured touchpad should be a touchpad device
9678 mFakePolicy->setPointerCapture(true);
9679 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9680 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9681}
9682
HQ Liue6983c72022-04-19 22:14:56 +00009683class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9684protected:
9685 float mPointerMovementScale;
9686 float mPointerXZoomScale;
9687 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9688 addConfigurationProperty("touch.deviceType", "pointer");
9689 std::shared_ptr<FakePointerController> fakePointerController =
9690 std::make_shared<FakePointerController>();
9691 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9692 fakePointerController->setPosition(0, 0);
9693 fakePointerController->setButtonState(0);
9694 prepareDisplay(DISPLAY_ORIENTATION_0);
9695
9696 prepareAxes(POSITION);
9697 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9698 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9699 // needs to be disabled, and the pointer gesture needs to be enabled.
9700 mFakePolicy->setPointerCapture(false);
9701 mFakePolicy->setPointerGestureEnabled(true);
9702 mFakePolicy->setPointerController(fakePointerController);
9703
9704 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9705 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9706 mPointerMovementScale =
9707 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9708 mPointerXZoomScale =
9709 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9710 }
9711
9712 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9713 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9714 /*flat*/ 0,
9715 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9716 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9717 /*flat*/ 0,
9718 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9719 }
9720};
9721
9722/**
9723 * Two fingers down on a pointer mode touch pad. The width
9724 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9725 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9726 * be greater than the both value to be freeform gesture, so that after two
9727 * fingers start to move downwards, the gesture should be swipe.
9728 */
9729TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9730 // The min freeform gesture width is 25units/mm x 30mm = 750
9731 // which is greater than fraction of the diagnal length of the touchpad (349).
9732 // Thus, MaxSwipWidth is 750.
9733 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9734 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9735 NotifyMotionArgs motionArgs;
9736
9737 // Two fingers down at once.
9738 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9739 // Pointer's initial position is used the [0,0] coordinate.
9740 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9741
9742 processId(mapper, FIRST_TRACKING_ID);
9743 processPosition(mapper, x1, y1);
9744 processMTSync(mapper);
9745 processId(mapper, SECOND_TRACKING_ID);
9746 processPosition(mapper, x2, y2);
9747 processMTSync(mapper);
9748 processSync(mapper);
9749
9750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9751 ASSERT_EQ(1U, motionArgs.pointerCount);
9752 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9753 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009754 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009755 ASSERT_NO_FATAL_FAILURE(
9756 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9757
9758 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9759 // that there should be 1 pointer.
9760 int32_t movingDistance = 200;
9761 y1 += movingDistance;
9762 y2 += movingDistance;
9763
9764 processId(mapper, FIRST_TRACKING_ID);
9765 processPosition(mapper, x1, y1);
9766 processMTSync(mapper);
9767 processId(mapper, SECOND_TRACKING_ID);
9768 processPosition(mapper, x2, y2);
9769 processMTSync(mapper);
9770 processSync(mapper);
9771
9772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9773 ASSERT_EQ(1U, motionArgs.pointerCount);
9774 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9775 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009776 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9778 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9779 0, 0, 0, 0));
9780}
9781
9782/**
9783 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9784 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9785 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9786 * value to be freeform gesture, so that after two fingers start to move downwards,
9787 * the gesture should be swipe.
9788 */
9789TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9790 // The min freeform gesture width is 5units/mm x 30mm = 150
9791 // which is greater than fraction of the diagnal length of the touchpad (349).
9792 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
9793 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
9794 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9795 NotifyMotionArgs motionArgs;
9796
9797 // Two fingers down at once.
9798 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9799 // Pointer's initial position is used the [0,0] coordinate.
9800 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9801
9802 processId(mapper, FIRST_TRACKING_ID);
9803 processPosition(mapper, x1, y1);
9804 processMTSync(mapper);
9805 processId(mapper, SECOND_TRACKING_ID);
9806 processPosition(mapper, x2, y2);
9807 processMTSync(mapper);
9808 processSync(mapper);
9809
9810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9811 ASSERT_EQ(1U, motionArgs.pointerCount);
9812 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9813 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009814 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009815 ASSERT_NO_FATAL_FAILURE(
9816 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9817
9818 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9819 // and there should be 1 pointer.
9820 int32_t movingDistance = 200;
9821 y1 += movingDistance;
9822 y2 += movingDistance;
9823
9824 processId(mapper, FIRST_TRACKING_ID);
9825 processPosition(mapper, x1, y1);
9826 processMTSync(mapper);
9827 processId(mapper, SECOND_TRACKING_ID);
9828 processPosition(mapper, x2, y2);
9829 processMTSync(mapper);
9830 processSync(mapper);
9831
9832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9833 ASSERT_EQ(1U, motionArgs.pointerCount);
9834 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9835 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009836 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009837 // New coordinate is the scaled relative coordinate from the initial coordinate.
9838 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9839 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9840 0, 0, 0, 0));
9841}
9842
9843/**
9844 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
9845 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
9846 * freeform gestures after two fingers start to move downwards.
9847 */
9848TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
9849 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9850 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9851
9852 NotifyMotionArgs motionArgs;
9853
9854 // Two fingers down at once. Wider than the max swipe width.
9855 // The gesture is expected to be PRESS, then transformed to FREEFORM
9856 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
9857
9858 processId(mapper, FIRST_TRACKING_ID);
9859 processPosition(mapper, x1, y1);
9860 processMTSync(mapper);
9861 processId(mapper, SECOND_TRACKING_ID);
9862 processPosition(mapper, x2, y2);
9863 processMTSync(mapper);
9864 processSync(mapper);
9865
9866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9867 ASSERT_EQ(1U, motionArgs.pointerCount);
9868 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9869 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009870 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009871 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
9872 ASSERT_NO_FATAL_FAILURE(
9873 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9874
9875 int32_t movingDistance = 200;
9876
9877 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
9878 // then two down events for two pointers.
9879 y1 += movingDistance;
9880 y2 += movingDistance;
9881
9882 processId(mapper, FIRST_TRACKING_ID);
9883 processPosition(mapper, x1, y1);
9884 processMTSync(mapper);
9885 processId(mapper, SECOND_TRACKING_ID);
9886 processPosition(mapper, x2, y2);
9887 processMTSync(mapper);
9888 processSync(mapper);
9889
9890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9891 // The previous PRESS gesture is cancelled, because it is transformed to freeform
9892 ASSERT_EQ(1U, motionArgs.pointerCount);
9893 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9895 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9896 ASSERT_EQ(1U, motionArgs.pointerCount);
9897 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9899 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009900 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009901 ASSERT_EQ(2U, motionArgs.pointerCount);
9902 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
9903 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009904 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009905 // Two pointers' scaled relative coordinates from their initial centroid.
9906 // Initial y coordinates are 0 as y1 and y2 have the same value.
9907 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
9908 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
9909 // When pointers move, the new coordinates equal to the initial coordinates plus
9910 // scaled moving distance.
9911 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9912 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9913 0, 0, 0, 0));
9914 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9915 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9916 0, 0, 0, 0));
9917
9918 // Move two fingers down again, expect one MOVE motion event.
9919 y1 += movingDistance;
9920 y2 += movingDistance;
9921
9922 processId(mapper, FIRST_TRACKING_ID);
9923 processPosition(mapper, x1, y1);
9924 processMTSync(mapper);
9925 processId(mapper, SECOND_TRACKING_ID);
9926 processPosition(mapper, x2, y2);
9927 processMTSync(mapper);
9928 processSync(mapper);
9929
9930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9931 ASSERT_EQ(2U, motionArgs.pointerCount);
9932 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9933 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009934 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009935 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9936 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9937 0, 0, 0, 0, 0));
9938 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9939 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9940 0, 0, 0, 0, 0));
9941}
9942
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009943// --- JoystickInputMapperTest ---
9944
9945class JoystickInputMapperTest : public InputMapperTest {
9946protected:
9947 static const int32_t RAW_X_MIN;
9948 static const int32_t RAW_X_MAX;
9949 static const int32_t RAW_Y_MIN;
9950 static const int32_t RAW_Y_MAX;
9951
9952 void SetUp() override {
9953 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9954 }
9955 void prepareAxes() {
9956 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9957 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9958 }
9959
9960 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9961 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9962 }
9963
9964 void processSync(JoystickInputMapper& mapper) {
9965 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9966 }
9967
9968 void prepareVirtualDisplay(int32_t orientation) {
9969 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9970 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9971 NO_PORT, ViewportType::VIRTUAL);
9972 }
9973};
9974
9975const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9976const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9977const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9978const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9979
9980TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9981 prepareAxes();
9982 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9983
9984 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9985
9986 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9987
9988 // Send an axis event
9989 processAxis(mapper, ABS_X, 100);
9990 processSync(mapper);
9991
9992 NotifyMotionArgs args;
9993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9994 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9995
9996 // Send another axis event
9997 processAxis(mapper, ABS_Y, 100);
9998 processSync(mapper);
9999
10000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10001 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10002}
10003
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010004// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010005
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010006class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010007protected:
10008 static const char* DEVICE_NAME;
10009 static const char* DEVICE_LOCATION;
10010 static const int32_t DEVICE_ID;
10011 static const int32_t DEVICE_GENERATION;
10012 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010013 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010014 static const int32_t EVENTHUB_ID;
10015
10016 std::shared_ptr<FakeEventHub> mFakeEventHub;
10017 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010018 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010019 std::unique_ptr<InstrumentedInputReader> mReader;
10020 std::shared_ptr<InputDevice> mDevice;
10021
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010022 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010023 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010024 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010025 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010026 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010027 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010028 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10029 }
10030
10031 void SetUp() override { SetUp(DEVICE_CLASSES); }
10032
10033 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010034 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010035 mFakePolicy.clear();
10036 }
10037
10038 void configureDevice(uint32_t changes) {
10039 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10040 mReader->requestRefreshConfiguration(changes);
10041 mReader->loopOnce();
10042 }
10043 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
10044 }
10045
10046 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10047 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010048 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010049 InputDeviceIdentifier identifier;
10050 identifier.name = name;
10051 identifier.location = location;
10052 std::shared_ptr<InputDevice> device =
10053 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10054 identifier);
10055 mReader->pushNextDevice(device);
10056 mFakeEventHub->addDevice(eventHubId, name, classes);
10057 mReader->loopOnce();
10058 return device;
10059 }
10060
10061 template <class T, typename... Args>
10062 T& addControllerAndConfigure(Args... args) {
10063 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10064
10065 return controller;
10066 }
10067};
10068
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010069const char* PeripheralControllerTest::DEVICE_NAME = "device";
10070const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10071const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10072const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10073const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010074const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10075 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010076const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010077
10078// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010079class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010080protected:
10081 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010082 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010083 }
10084};
10085
10086TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010087 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010088
10089 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
10090 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
10091}
10092
10093TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010094 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010095
10096 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
10097 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
10098}
10099
10100// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010101class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010102protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010103 void SetUp() override {
10104 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10105 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010106};
10107
Chris Ye85758332021-05-16 23:05:17 -070010108TEST_F(LightControllerTest, MonoLight) {
10109 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010110 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010111 .maxBrightness = 255,
10112 .flags = InputLightClass::BRIGHTNESS,
10113 .path = ""};
10114 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010115
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010116 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010117 InputDeviceInfo info;
10118 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010119 std::vector<InputDeviceLightInfo> lights = info.getLights();
10120 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010121 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10122 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10123
10124 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10125 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10126}
10127
10128TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10129 RawLightInfo infoMono = {.id = 1,
10130 .name = "mono_keyboard_backlight",
10131 .maxBrightness = 255,
10132 .flags = InputLightClass::BRIGHTNESS |
10133 InputLightClass::KEYBOARD_BACKLIGHT,
10134 .path = ""};
10135 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10136
10137 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10138 InputDeviceInfo info;
10139 controller.populateDeviceInfo(&info);
10140 std::vector<InputDeviceLightInfo> lights = info.getLights();
10141 ASSERT_EQ(1U, lights.size());
10142 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10143 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010144
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010145 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10146 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010147}
10148
10149TEST_F(LightControllerTest, RGBLight) {
10150 RawLightInfo infoRed = {.id = 1,
10151 .name = "red",
10152 .maxBrightness = 255,
10153 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10154 .path = ""};
10155 RawLightInfo infoGreen = {.id = 2,
10156 .name = "green",
10157 .maxBrightness = 255,
10158 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10159 .path = ""};
10160 RawLightInfo infoBlue = {.id = 3,
10161 .name = "blue",
10162 .maxBrightness = 255,
10163 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10164 .path = ""};
10165 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10166 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10167 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10168
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010169 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010170 InputDeviceInfo info;
10171 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010172 std::vector<InputDeviceLightInfo> lights = info.getLights();
10173 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010174 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10175 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10176 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10177
10178 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10179 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10180}
10181
10182TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10183 RawLightInfo infoRed = {.id = 1,
10184 .name = "red_keyboard_backlight",
10185 .maxBrightness = 255,
10186 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10187 InputLightClass::KEYBOARD_BACKLIGHT,
10188 .path = ""};
10189 RawLightInfo infoGreen = {.id = 2,
10190 .name = "green_keyboard_backlight",
10191 .maxBrightness = 255,
10192 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10193 InputLightClass::KEYBOARD_BACKLIGHT,
10194 .path = ""};
10195 RawLightInfo infoBlue = {.id = 3,
10196 .name = "blue_keyboard_backlight",
10197 .maxBrightness = 255,
10198 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10199 InputLightClass::KEYBOARD_BACKLIGHT,
10200 .path = ""};
10201 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10202 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10203 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10204
10205 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10206 InputDeviceInfo info;
10207 controller.populateDeviceInfo(&info);
10208 std::vector<InputDeviceLightInfo> lights = info.getLights();
10209 ASSERT_EQ(1U, lights.size());
10210 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10211 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10212 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10213
10214 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10215 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10216}
10217
10218TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10219 RawLightInfo infoRed = {.id = 1,
10220 .name = "red",
10221 .maxBrightness = 255,
10222 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10223 .path = ""};
10224 RawLightInfo infoGreen = {.id = 2,
10225 .name = "green",
10226 .maxBrightness = 255,
10227 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10228 .path = ""};
10229 RawLightInfo infoBlue = {.id = 3,
10230 .name = "blue",
10231 .maxBrightness = 255,
10232 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10233 .path = ""};
10234 RawLightInfo infoGlobal = {.id = 3,
10235 .name = "global_keyboard_backlight",
10236 .maxBrightness = 255,
10237 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10238 InputLightClass::KEYBOARD_BACKLIGHT,
10239 .path = ""};
10240 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10241 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10242 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10243 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10244
10245 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10246 InputDeviceInfo info;
10247 controller.populateDeviceInfo(&info);
10248 std::vector<InputDeviceLightInfo> lights = info.getLights();
10249 ASSERT_EQ(1U, lights.size());
10250 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10251 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10252 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010253
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010254 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10255 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010256}
10257
10258TEST_F(LightControllerTest, MultiColorRGBLight) {
10259 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010260 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010261 .maxBrightness = 255,
10262 .flags = InputLightClass::BRIGHTNESS |
10263 InputLightClass::MULTI_INTENSITY |
10264 InputLightClass::MULTI_INDEX,
10265 .path = ""};
10266
10267 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10268
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010269 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010270 InputDeviceInfo info;
10271 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010272 std::vector<InputDeviceLightInfo> lights = info.getLights();
10273 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010274 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10275 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10276 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10277
10278 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10279 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10280}
10281
10282TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10283 RawLightInfo infoColor = {.id = 1,
10284 .name = "multi_color_keyboard_backlight",
10285 .maxBrightness = 255,
10286 .flags = InputLightClass::BRIGHTNESS |
10287 InputLightClass::MULTI_INTENSITY |
10288 InputLightClass::MULTI_INDEX |
10289 InputLightClass::KEYBOARD_BACKLIGHT,
10290 .path = ""};
10291
10292 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10293
10294 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10295 InputDeviceInfo info;
10296 controller.populateDeviceInfo(&info);
10297 std::vector<InputDeviceLightInfo> lights = info.getLights();
10298 ASSERT_EQ(1U, lights.size());
10299 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10300 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10301 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010302
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010303 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10304 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010305}
10306
10307TEST_F(LightControllerTest, PlayerIdLight) {
10308 RawLightInfo info1 = {.id = 1,
10309 .name = "player1",
10310 .maxBrightness = 255,
10311 .flags = InputLightClass::BRIGHTNESS,
10312 .path = ""};
10313 RawLightInfo info2 = {.id = 2,
10314 .name = "player2",
10315 .maxBrightness = 255,
10316 .flags = InputLightClass::BRIGHTNESS,
10317 .path = ""};
10318 RawLightInfo info3 = {.id = 3,
10319 .name = "player3",
10320 .maxBrightness = 255,
10321 .flags = InputLightClass::BRIGHTNESS,
10322 .path = ""};
10323 RawLightInfo info4 = {.id = 4,
10324 .name = "player4",
10325 .maxBrightness = 255,
10326 .flags = InputLightClass::BRIGHTNESS,
10327 .path = ""};
10328 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10329 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10330 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10331 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10332
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010333 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010334 InputDeviceInfo info;
10335 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010336 std::vector<InputDeviceLightInfo> lights = info.getLights();
10337 ASSERT_EQ(1U, lights.size());
10338 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010339 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10340 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010341
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010342 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10343 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10344 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010345}
10346
Michael Wrightd02c5b62014-02-10 15:10:22 -080010347} // namespace android