blob: 78ea6920853863a66c5ae003b3383c697963a57a [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
19
Prabir Pradhan2770d242019-09-02 18:07:11 -070020#include <CursorInputMapper.h>
21#include <InputDevice.h>
22#include <InputMapper.h>
23#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080024#include <InputReaderBase.h>
25#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000026#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070027#include <KeyboardInputMapper.h>
28#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070029#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070030#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070031#include <SingleTouchInputMapper.h>
32#include <SwitchInputMapper.h>
33#include <TestInputListener.h>
Prabir Pradhan739dca42022-09-09 20:12:01 +000034#include <TestInputListenerMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070035#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080036#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000037#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070038#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080039#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050040#include <gui/constants.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080041
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000042#include "android/hardware/input/InputDeviceCountryCode.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000043#include "input/DisplayViewport.h"
44#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010045
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000046using android::hardware::input::InputDeviceCountryCode;
47
Michael Wrightd02c5b62014-02-10 15:10:22 -080048namespace android {
49
Dominik Laskowski2f01d772022-03-23 16:01:29 -070050using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000051using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070052using std::chrono_literals::operator""ms;
53
54// Timeout for waiting for an expected event
55static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
56
Michael Wrightd02c5b62014-02-10 15:10:22 -080057// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000058static constexpr nsecs_t ARBITRARY_TIME = 1234;
59static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080060
61// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080062static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000063static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080064static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000065static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080066static constexpr int32_t DISPLAY_WIDTH = 480;
67static constexpr int32_t DISPLAY_HEIGHT = 800;
68static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
69static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
70static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070071static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070072static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080073
arthurhungcc7f9802020-04-30 17:55:40 +080074static constexpr int32_t FIRST_SLOT = 0;
75static constexpr int32_t SECOND_SLOT = 1;
76static constexpr int32_t THIRD_SLOT = 2;
77static constexpr int32_t INVALID_TRACKING_ID = -1;
78static constexpr int32_t FIRST_TRACKING_ID = 0;
79static constexpr int32_t SECOND_TRACKING_ID = 1;
80static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080081static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080082static constexpr int32_t BATTERY_STATUS = 4;
83static constexpr int32_t BATTERY_CAPACITY = 66;
Prabir Pradhane287ecd2022-09-07 21:18:05 +000084static const std::string BATTERY_DEVPATH = "/sys/devices/mydevice/power_supply/mybattery";
Chris Ye3fdbfef2021-01-06 18:45:18 -080085static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
86static constexpr int32_t LIGHT_COLOR = 0x7F448866;
87static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080088
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080089static constexpr int32_t ACTION_POINTER_0_DOWN =
90 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
91static constexpr int32_t ACTION_POINTER_0_UP =
92 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
93static constexpr int32_t ACTION_POINTER_1_DOWN =
94 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
95static constexpr int32_t ACTION_POINTER_1_UP =
96 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
97
Michael Wrightd02c5b62014-02-10 15:10:22 -080098// Error tolerance for floating point assertions.
99static const float EPSILON = 0.001f;
100
101template<typename T>
102static inline T min(T a, T b) {
103 return a < b ? a : b;
104}
105
106static inline float avg(float x, float y) {
107 return (x + y) / 2;
108}
109
Chris Ye3fdbfef2021-01-06 18:45:18 -0800110// Mapping for light color name and the light color
111const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
112 {"green", LightColor::GREEN},
113 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800114
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700115static int32_t getInverseRotation(int32_t orientation) {
116 switch (orientation) {
117 case DISPLAY_ORIENTATION_90:
118 return DISPLAY_ORIENTATION_270;
119 case DISPLAY_ORIENTATION_270:
120 return DISPLAY_ORIENTATION_90;
121 default:
122 return orientation;
123 }
124}
125
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800126static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
127 InputDeviceInfo info;
128 mapper.populateDeviceInfo(&info);
129
130 const InputDeviceInfo::MotionRange* motionRange =
131 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
132 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
133}
134
135static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
136 InputDeviceInfo info;
137 mapper.populateDeviceInfo(&info);
138
139 const InputDeviceInfo::MotionRange* motionRange =
140 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
141 ASSERT_EQ(nullptr, motionRange);
142}
143
Michael Wrightd02c5b62014-02-10 15:10:22 -0800144// --- FakePointerController ---
145
146class FakePointerController : public PointerControllerInterface {
147 bool mHaveBounds;
148 float mMinX, mMinY, mMaxX, mMaxY;
149 float mX, mY;
150 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800151 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152
Michael Wrightd02c5b62014-02-10 15:10:22 -0800153public:
154 FakePointerController() :
155 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800156 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800157 }
158
Michael Wright17db18e2020-06-26 20:51:44 +0100159 virtual ~FakePointerController() {}
160
Michael Wrightd02c5b62014-02-10 15:10:22 -0800161 void setBounds(float minX, float minY, float maxX, float maxY) {
162 mHaveBounds = true;
163 mMinX = minX;
164 mMinY = minY;
165 mMaxX = maxX;
166 mMaxY = maxY;
167 }
168
Chris Yea52ade12020-08-27 16:49:20 -0700169 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170 mX = x;
171 mY = y;
172 }
173
Chris Yea52ade12020-08-27 16:49:20 -0700174 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800175
Chris Yea52ade12020-08-27 16:49:20 -0700176 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800177
Chris Yea52ade12020-08-27 16:49:20 -0700178 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800179 *outX = mX;
180 *outY = mY;
181 }
182
Chris Yea52ade12020-08-27 16:49:20 -0700183 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800184
Chris Yea52ade12020-08-27 16:49:20 -0700185 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800186 mDisplayId = viewport.displayId;
187 }
188
Arthur Hung7c645402019-01-25 17:45:42 +0800189 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
190 return mSpotsByDisplay;
191 }
192
Michael Wrightd02c5b62014-02-10 15:10:22 -0800193private:
Chris Yea52ade12020-08-27 16:49:20 -0700194 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800195 *outMinX = mMinX;
196 *outMinY = mMinY;
197 *outMaxX = mMaxX;
198 *outMaxY = mMaxY;
199 return mHaveBounds;
200 }
201
Chris Yea52ade12020-08-27 16:49:20 -0700202 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800203 mX += deltaX;
204 if (mX < mMinX) mX = mMinX;
205 if (mX > mMaxX) mX = mMaxX;
206 mY += deltaY;
207 if (mY < mMinY) mY = mMinY;
208 if (mY > mMaxY) mY = mMaxY;
209 }
210
Chris Yea52ade12020-08-27 16:49:20 -0700211 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800212
Chris Yea52ade12020-08-27 16:49:20 -0700213 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800214
Chris Yea52ade12020-08-27 16:49:20 -0700215 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800216
Chris Yea52ade12020-08-27 16:49:20 -0700217 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
218 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800219 std::vector<int32_t> newSpots;
220 // Add spots for fingers that are down.
221 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
222 uint32_t id = idBits.clearFirstMarkedBit();
223 newSpots.push_back(id);
224 }
225
226 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800227 }
228
Prabir Pradhan197e0862022-07-01 14:28:00 +0000229 void clearSpots() override { mSpotsByDisplay.clear(); }
Arthur Hung7c645402019-01-25 17:45:42 +0800230
231 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800232};
233
234
235// --- FakeInputReaderPolicy ---
236
237class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700238 std::mutex mLock;
239 std::condition_variable mDevicesChangedCondition;
240
Michael Wrightd02c5b62014-02-10 15:10:22 -0800241 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000242 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700243 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
244 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100245 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700246 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800247
248protected:
Chris Yea52ade12020-08-27 16:49:20 -0700249 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800250
251public:
252 FakeInputReaderPolicy() {
253 }
254
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700255 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800256 waitForInputDevices([](bool devicesChanged) {
257 if (!devicesChanged) {
258 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
259 }
260 });
261 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700262
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800263 void assertInputDevicesNotChanged() {
264 waitForInputDevices([](bool devicesChanged) {
265 if (devicesChanged) {
266 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
267 }
268 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700269 }
270
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700271 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100272 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100273 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700274 }
275
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700276 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
277 return mConfig.getDisplayViewportByUniqueId(uniqueId);
278 }
279 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
280 return mConfig.getDisplayViewportByType(type);
281 }
282
283 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
284 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700285 }
286
Prabir Pradhan5632d622021-09-06 07:57:20 -0700287 void addDisplayViewport(DisplayViewport viewport) {
288 mViewports.push_back(std::move(viewport));
289 mConfig.setDisplayViewports(mViewports);
290 }
291
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700292 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000293 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700294 std::optional<uint8_t> physicalPort, ViewportType type) {
295 const bool isRotated =
296 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
297 DisplayViewport v;
298 v.displayId = displayId;
299 v.orientation = orientation;
300 v.logicalLeft = 0;
301 v.logicalTop = 0;
302 v.logicalRight = isRotated ? height : width;
303 v.logicalBottom = isRotated ? width : height;
304 v.physicalLeft = 0;
305 v.physicalTop = 0;
306 v.physicalRight = isRotated ? height : width;
307 v.physicalBottom = isRotated ? width : height;
308 v.deviceWidth = isRotated ? height : width;
309 v.deviceHeight = isRotated ? width : height;
310 v.isActive = isActive;
311 v.uniqueId = uniqueId;
312 v.physicalPort = physicalPort;
313 v.type = type;
314
315 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800316 }
317
Arthur Hung6cd19a42019-08-30 19:04:12 +0800318 bool updateViewport(const DisplayViewport& viewport) {
319 size_t count = mViewports.size();
320 for (size_t i = 0; i < count; i++) {
321 const DisplayViewport& currentViewport = mViewports[i];
322 if (currentViewport.displayId == viewport.displayId) {
323 mViewports[i] = viewport;
324 mConfig.setDisplayViewports(mViewports);
325 return true;
326 }
327 }
328 // no viewport found.
329 return false;
330 }
331
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100332 void addExcludedDeviceName(const std::string& deviceName) {
333 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800334 }
335
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700336 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
337 mConfig.portAssociations.insert({inputPort, displayPort});
338 }
339
Christine Franks1ba71cc2021-04-07 14:37:42 -0700340 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
341 const std::string& displayUniqueId) {
342 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
343 }
344
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000345 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700346
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000347 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700348
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000349 void setPointerController(std::shared_ptr<FakePointerController> controller) {
350 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351 }
352
353 const InputReaderConfiguration* getReaderConfiguration() const {
354 return &mConfig;
355 }
356
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800357 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800358 return mInputDevices;
359 }
360
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100361 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700362 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700363 return transform;
364 }
365
366 void setTouchAffineTransformation(const TouchAffineTransformation t) {
367 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800368 }
369
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000370 PointerCaptureRequest setPointerCapture(bool enabled) {
371 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
372 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800373 }
374
Arthur Hung7c645402019-01-25 17:45:42 +0800375 void setShowTouches(bool enabled) {
376 mConfig.showTouches = enabled;
377 }
378
Garfield Tan888a6a42020-01-09 11:39:16 -0800379 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
380 mConfig.defaultPointerDisplayId = pointerDisplayId;
381 }
382
HQ Liue6983c72022-04-19 22:14:56 +0000383 void setPointerGestureEnabled(bool enabled) { mConfig.pointerGesturesEnabled = enabled; }
384
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800385 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
386
HQ Liue6983c72022-04-19 22:14:56 +0000387 float getPointerGestureZoomSpeedRatio() { return mConfig.pointerGestureZoomSpeedRatio; }
388
Prabir Pradhanf99d6e72022-04-21 15:28:35 +0000389 void setVelocityControlParams(const VelocityControlParameters& params) {
390 mConfig.pointerVelocityControlParameters = params;
391 mConfig.wheelVelocityControlParameters = params;
392 }
393
Michael Wrightd02c5b62014-02-10 15:10:22 -0800394private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000395 uint32_t mNextPointerCaptureSequenceNumber = 0;
396
Chris Yea52ade12020-08-27 16:49:20 -0700397 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800398 *outConfig = mConfig;
399 }
400
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000401 std::shared_ptr<PointerControllerInterface> obtainPointerController(
402 int32_t /*deviceId*/) override {
403 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800404 }
405
Chris Yea52ade12020-08-27 16:49:20 -0700406 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700407 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800408 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700409 mInputDevicesChanged = true;
410 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800411 }
412
Chris Yea52ade12020-08-27 16:49:20 -0700413 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
414 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700415 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800416 }
417
Chris Yea52ade12020-08-27 16:49:20 -0700418 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800419
420 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
421 std::unique_lock<std::mutex> lock(mLock);
422 base::ScopedLockAssertion assumeLocked(mLock);
423
424 const bool devicesChanged =
425 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
426 return mInputDevicesChanged;
427 });
428 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
429 mInputDevicesChanged = false;
430 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800431};
432
Michael Wrightd02c5b62014-02-10 15:10:22 -0800433// --- FakeEventHub ---
434
435class FakeEventHub : public EventHubInterface {
436 struct KeyInfo {
437 int32_t keyCode;
438 uint32_t flags;
439 };
440
Chris Yef59a2f42020-10-16 12:55:26 -0700441 struct SensorInfo {
442 InputDeviceSensorType sensorType;
443 int32_t sensorDataIndex;
444 };
445
Michael Wrightd02c5b62014-02-10 15:10:22 -0800446 struct Device {
447 InputDeviceIdentifier identifier;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700448 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800449 PropertyMap configuration;
450 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
451 KeyedVector<int, bool> relativeAxes;
452 KeyedVector<int32_t, int32_t> keyCodeStates;
453 KeyedVector<int32_t, int32_t> scanCodeStates;
454 KeyedVector<int32_t, int32_t> switchStates;
455 KeyedVector<int32_t, int32_t> absoluteAxisValue;
456 KeyedVector<int32_t, KeyInfo> keysByScanCode;
457 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
458 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100459 // fake mapping which would normally come from keyCharacterMap
460 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700461 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
462 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800463 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700464 bool enabled;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000465 InputDeviceCountryCode countryCode;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700466
467 status_t enable() {
468 enabled = true;
469 return OK;
470 }
471
472 status_t disable() {
473 enabled = false;
474 return OK;
475 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800476
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700477 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800478 };
479
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700480 std::mutex mLock;
481 std::condition_variable mEventsCondition;
482
Michael Wrightd02c5b62014-02-10 15:10:22 -0800483 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100484 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000485 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600486 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000487 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800488 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
489 // Simulates a device light brightness, from light id to light brightness.
490 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
491 // Simulates a device light intensities, from light id to light intensities map.
492 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
493 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800494
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700495public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800496 virtual ~FakeEventHub() {
497 for (size_t i = 0; i < mDevices.size(); i++) {
498 delete mDevices.valueAt(i);
499 }
500 }
501
Michael Wrightd02c5b62014-02-10 15:10:22 -0800502 FakeEventHub() { }
503
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700504 void addDevice(int32_t deviceId, const std::string& name,
505 ftl::Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800506 Device* device = new Device(classes);
507 device->identifier.name = name;
508 mDevices.add(deviceId, device);
509
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000510 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800511 }
512
513 void removeDevice(int32_t deviceId) {
514 delete mDevices.valueFor(deviceId);
515 mDevices.removeItem(deviceId);
516
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000517 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800518 }
519
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000520 bool isDeviceEnabled(int32_t deviceId) const override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700521 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700522 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700523 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
524 return false;
525 }
526 return device->enabled;
527 }
528
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000529 status_t enableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700530 status_t result;
531 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700532 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700533 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
534 return BAD_VALUE;
535 }
536 if (device->enabled) {
537 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
538 return OK;
539 }
540 result = device->enable();
541 return result;
542 }
543
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000544 status_t disableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700545 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700546 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700547 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
548 return BAD_VALUE;
549 }
550 if (!device->enabled) {
551 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
552 return OK;
553 }
554 return device->disable();
555 }
556
Michael Wrightd02c5b62014-02-10 15:10:22 -0800557 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000558 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800559 }
560
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700561 void addConfigurationProperty(int32_t deviceId, const char* key, const char* value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800562 Device* device = getDevice(deviceId);
563 device->configuration.addProperty(key, value);
564 }
565
566 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
567 Device* device = getDevice(deviceId);
568 device->configuration.addAll(configuration);
569 }
570
571 void addAbsoluteAxis(int32_t deviceId, int axis,
572 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
573 Device* device = getDevice(deviceId);
574
575 RawAbsoluteAxisInfo info;
576 info.valid = true;
577 info.minValue = minValue;
578 info.maxValue = maxValue;
579 info.flat = flat;
580 info.fuzz = fuzz;
581 info.resolution = resolution;
582 device->absoluteAxes.add(axis, info);
583 }
584
585 void addRelativeAxis(int32_t deviceId, int32_t axis) {
586 Device* device = getDevice(deviceId);
587 device->relativeAxes.add(axis, true);
588 }
589
590 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
591 Device* device = getDevice(deviceId);
592 device->keyCodeStates.replaceValueFor(keyCode, state);
593 }
594
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000595 void setCountryCode(int32_t deviceId, InputDeviceCountryCode countryCode) {
596 Device* device = getDevice(deviceId);
597 device->countryCode = countryCode;
598 }
599
Michael Wrightd02c5b62014-02-10 15:10:22 -0800600 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
601 Device* device = getDevice(deviceId);
602 device->scanCodeStates.replaceValueFor(scanCode, state);
603 }
604
605 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
606 Device* device = getDevice(deviceId);
607 device->switchStates.replaceValueFor(switchCode, state);
608 }
609
610 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
611 Device* device = getDevice(deviceId);
612 device->absoluteAxisValue.replaceValueFor(axis, value);
613 }
614
615 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
616 int32_t keyCode, uint32_t flags) {
617 Device* device = getDevice(deviceId);
618 KeyInfo info;
619 info.keyCode = keyCode;
620 info.flags = flags;
621 if (scanCode) {
622 device->keysByScanCode.add(scanCode, info);
623 }
624 if (usageCode) {
625 device->keysByUsageCode.add(usageCode, info);
626 }
627 }
628
Philip Junker4af3b3d2021-12-14 10:36:55 +0100629 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
630 Device* device = getDevice(deviceId);
631 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
632 }
633
Michael Wrightd02c5b62014-02-10 15:10:22 -0800634 void addLed(int32_t deviceId, int32_t led, bool initialState) {
635 Device* device = getDevice(deviceId);
636 device->leds.add(led, initialState);
637 }
638
Chris Yef59a2f42020-10-16 12:55:26 -0700639 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
640 int32_t sensorDataIndex) {
641 Device* device = getDevice(deviceId);
642 SensorInfo info;
643 info.sensorType = sensorType;
644 info.sensorDataIndex = sensorDataIndex;
645 device->sensorsByAbsCode.emplace(absCode, info);
646 }
647
648 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
649 Device* device = getDevice(deviceId);
650 typename BitArray<MSC_MAX>::Buffer buffer;
651 buffer[mscEvent / 32] = 1 << mscEvent % 32;
652 device->mscBitmask.loadFromBuffer(buffer);
653 }
654
Chris Ye3fdbfef2021-01-06 18:45:18 -0800655 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
656 mRawLightInfos.emplace(rawId, std::move(info));
657 }
658
659 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
660 mLightBrightness.emplace(rawId, brightness);
661 }
662
663 void fakeLightIntensities(int32_t rawId,
664 const std::unordered_map<LightColor, int32_t> intensities) {
665 mLightIntensities.emplace(rawId, std::move(intensities));
666 }
667
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668 bool getLedState(int32_t deviceId, int32_t led) {
669 Device* device = getDevice(deviceId);
670 return device->leds.valueFor(led);
671 }
672
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100673 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800674 return mExcludedDevices;
675 }
676
677 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
678 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800679 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800680 }
681
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000682 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
683 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700684 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800685 RawEvent event;
686 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000687 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800688 event.deviceId = deviceId;
689 event.type = type;
690 event.code = code;
691 event.value = value;
692 mEvents.push_back(event);
693
694 if (type == EV_ABS) {
695 setAbsoluteAxisValue(deviceId, code, value);
696 }
697 }
698
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600699 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
700 std::vector<TouchVideoFrame>> videoFrames) {
701 mVideoFrames = std::move(videoFrames);
702 }
703
Michael Wrightd02c5b62014-02-10 15:10:22 -0800704 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700705 std::unique_lock<std::mutex> lock(mLock);
706 base::ScopedLockAssertion assumeLocked(mLock);
707 const bool queueIsEmpty =
708 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
709 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
710 if (!queueIsEmpty) {
711 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
712 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800713 }
714
715private:
716 Device* getDevice(int32_t deviceId) const {
717 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100718 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800719 }
720
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700721 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800722 Device* device = getDevice(deviceId);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700723 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800724 }
725
Chris Yea52ade12020-08-27 16:49:20 -0700726 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800727 Device* device = getDevice(deviceId);
728 return device ? device->identifier : InputDeviceIdentifier();
729 }
730
Chris Yea52ade12020-08-27 16:49:20 -0700731 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800732
Chris Yea52ade12020-08-27 16:49:20 -0700733 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800734 Device* device = getDevice(deviceId);
735 if (device) {
736 *outConfiguration = device->configuration;
737 }
738 }
739
Chris Yea52ade12020-08-27 16:49:20 -0700740 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
741 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800742 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800743 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800744 ssize_t index = device->absoluteAxes.indexOfKey(axis);
745 if (index >= 0) {
746 *outAxisInfo = device->absoluteAxes.valueAt(index);
747 return OK;
748 }
749 }
750 outAxisInfo->clear();
751 return -1;
752 }
753
Chris Yea52ade12020-08-27 16:49:20 -0700754 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800755 Device* device = getDevice(deviceId);
756 if (device) {
757 return device->relativeAxes.indexOfKey(axis) >= 0;
758 }
759 return false;
760 }
761
Chris Yea52ade12020-08-27 16:49:20 -0700762 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800763
Chris Yef59a2f42020-10-16 12:55:26 -0700764 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
765 Device* device = getDevice(deviceId);
766 if (device) {
767 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
768 }
769 return false;
770 }
771
Chris Yea52ade12020-08-27 16:49:20 -0700772 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
773 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800774 Device* device = getDevice(deviceId);
775 if (device) {
776 const KeyInfo* key = getKey(device, scanCode, usageCode);
777 if (key) {
778 if (outKeycode) {
779 *outKeycode = key->keyCode;
780 }
781 if (outFlags) {
782 *outFlags = key->flags;
783 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700784 if (outMetaState) {
785 *outMetaState = metaState;
786 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800787 return OK;
788 }
789 }
790 return NAME_NOT_FOUND;
791 }
792
793 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
794 if (usageCode) {
795 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
796 if (index >= 0) {
797 return &device->keysByUsageCode.valueAt(index);
798 }
799 }
800 if (scanCode) {
801 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
802 if (index >= 0) {
803 return &device->keysByScanCode.valueAt(index);
804 }
805 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700806 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800807 }
808
Chris Yea52ade12020-08-27 16:49:20 -0700809 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000811 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
812 int32_t deviceId, int32_t absCode) const override {
Chris Yef59a2f42020-10-16 12:55:26 -0700813 Device* device = getDevice(deviceId);
814 if (!device) {
815 return Errorf("Sensor device not found.");
816 }
817 auto it = device->sensorsByAbsCode.find(absCode);
818 if (it == device->sensorsByAbsCode.end()) {
819 return Errorf("Sensor map not found.");
820 }
821 const SensorInfo& info = it->second;
822 return std::make_pair(info.sensorType, info.sensorDataIndex);
823 }
824
Chris Yea52ade12020-08-27 16:49:20 -0700825 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800826 mExcludedDevices = devices;
827 }
828
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700829 std::vector<RawEvent> getEvents(int) override {
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000830 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800831
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700832 std::vector<RawEvent> buffer;
833 std::swap(buffer, mEvents);
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000834
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700835 mEventsCondition.notify_all();
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700836 return buffer;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800837 }
838
Chris Yea52ade12020-08-27 16:49:20 -0700839 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600840 auto it = mVideoFrames.find(deviceId);
841 if (it != mVideoFrames.end()) {
842 std::vector<TouchVideoFrame> frames = std::move(it->second);
843 mVideoFrames.erase(deviceId);
844 return frames;
845 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800846 return {};
847 }
848
Chris Yea52ade12020-08-27 16:49:20 -0700849 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800850 Device* device = getDevice(deviceId);
851 if (device) {
852 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
853 if (index >= 0) {
854 return device->scanCodeStates.valueAt(index);
855 }
856 }
857 return AKEY_STATE_UNKNOWN;
858 }
859
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000860 InputDeviceCountryCode getCountryCode(int32_t deviceId) const override {
861 Device* device = getDevice(deviceId);
862 if (device) {
863 return device->countryCode;
864 }
865 return InputDeviceCountryCode::INVALID;
866 }
867
Chris Yea52ade12020-08-27 16:49:20 -0700868 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800869 Device* device = getDevice(deviceId);
870 if (device) {
871 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
872 if (index >= 0) {
873 return device->keyCodeStates.valueAt(index);
874 }
875 }
876 return AKEY_STATE_UNKNOWN;
877 }
878
Chris Yea52ade12020-08-27 16:49:20 -0700879 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800880 Device* device = getDevice(deviceId);
881 if (device) {
882 ssize_t index = device->switchStates.indexOfKey(sw);
883 if (index >= 0) {
884 return device->switchStates.valueAt(index);
885 }
886 }
887 return AKEY_STATE_UNKNOWN;
888 }
889
Chris Yea52ade12020-08-27 16:49:20 -0700890 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
891 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800892 Device* device = getDevice(deviceId);
893 if (device) {
894 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
895 if (index >= 0) {
896 *outValue = device->absoluteAxisValue.valueAt(index);
897 return OK;
898 }
899 }
900 *outValue = 0;
901 return -1;
902 }
903
Philip Junker4af3b3d2021-12-14 10:36:55 +0100904 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
905 Device* device = getDevice(deviceId);
906 if (!device) {
907 return AKEYCODE_UNKNOWN;
908 }
909 auto it = device->keyCodeMapping.find(locationKeyCode);
910 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
911 }
912
Chris Yea52ade12020-08-27 16:49:20 -0700913 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700914 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700915 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800916 bool result = false;
917 Device* device = getDevice(deviceId);
918 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700919 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700920 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800921 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
922 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
923 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800924 }
925 }
926 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
927 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
928 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929 }
930 }
931 }
932 }
933 return result;
934 }
935
Chris Yea52ade12020-08-27 16:49:20 -0700936 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800937 Device* device = getDevice(deviceId);
938 if (device) {
939 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
940 return index >= 0;
941 }
942 return false;
943 }
944
Arthur Hungcb40a002021-08-03 14:31:01 +0000945 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
946 Device* device = getDevice(deviceId);
947 if (!device) {
948 return false;
949 }
950 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
951 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
952 return true;
953 }
954 }
955 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
956 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
957 return true;
958 }
959 }
960 return false;
961 }
962
Chris Yea52ade12020-08-27 16:49:20 -0700963 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800964 Device* device = getDevice(deviceId);
965 return device && device->leds.indexOfKey(led) >= 0;
966 }
967
Chris Yea52ade12020-08-27 16:49:20 -0700968 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800969 Device* device = getDevice(deviceId);
970 if (device) {
971 ssize_t index = device->leds.indexOfKey(led);
972 if (index >= 0) {
973 device->leds.replaceValueAt(led, on);
974 } else {
975 ADD_FAILURE()
976 << "Attempted to set the state of an LED that the EventHub declared "
977 "was not present. led=" << led;
978 }
979 }
980 }
981
Chris Yea52ade12020-08-27 16:49:20 -0700982 void getVirtualKeyDefinitions(
983 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800984 outVirtualKeys.clear();
985
986 Device* device = getDevice(deviceId);
987 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800988 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800989 }
990 }
991
Chris Yea52ade12020-08-27 16:49:20 -0700992 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700993 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800994 }
995
Chris Yea52ade12020-08-27 16:49:20 -0700996 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800997 return false;
998 }
999
Chris Yea52ade12020-08-27 16:49:20 -07001000 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001001
Chris Yea52ade12020-08-27 16:49:20 -07001002 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001003
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001004 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override { return mVibrators; };
Chris Ye87143712020-11-10 05:05:58 +00001005
Chris Yee2b1e5c2021-03-10 22:45:12 -08001006 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
1007 return BATTERY_CAPACITY;
1008 }
Kim Low03ea0352020-11-06 12:45:07 -08001009
Chris Yee2b1e5c2021-03-10 22:45:12 -08001010 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
1011 return BATTERY_STATUS;
1012 }
1013
Andy Chenf9f1a022022-08-29 20:07:10 -04001014 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override {
1015 return {DEFAULT_BATTERY};
1016 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001017
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001018 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
1019 int32_t batteryId) const override {
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001020 if (batteryId != DEFAULT_BATTERY) return {};
1021 static const auto BATTERY_INFO = RawBatteryInfo{.id = DEFAULT_BATTERY,
1022 .name = "default battery",
1023 .flags = InputBatteryClass::CAPACITY,
1024 .path = BATTERY_DEVPATH};
1025 return BATTERY_INFO;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001026 }
Kim Low03ea0352020-11-06 12:45:07 -08001027
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001028 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001029 std::vector<int32_t> ids;
1030 for (const auto& [rawId, info] : mRawLightInfos) {
1031 ids.push_back(rawId);
1032 }
1033 return ids;
1034 }
1035
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001036 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001037 auto it = mRawLightInfos.find(lightId);
1038 if (it == mRawLightInfos.end()) {
1039 return std::nullopt;
1040 }
1041 return it->second;
1042 }
1043
1044 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1045 mLightBrightness.emplace(lightId, brightness);
1046 }
1047
1048 void setLightIntensities(int32_t deviceId, int32_t lightId,
1049 std::unordered_map<LightColor, int32_t> intensities) override {
1050 mLightIntensities.emplace(lightId, intensities);
1051 };
1052
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001053 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001054 auto lightIt = mLightBrightness.find(lightId);
1055 if (lightIt == mLightBrightness.end()) {
1056 return std::nullopt;
1057 }
1058 return lightIt->second;
1059 }
1060
1061 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001062 int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001063 auto lightIt = mLightIntensities.find(lightId);
1064 if (lightIt == mLightIntensities.end()) {
1065 return std::nullopt;
1066 }
1067 return lightIt->second;
1068 };
1069
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001070 void dump(std::string&) const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001071
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001072 void monitor() const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001073
Chris Yea52ade12020-08-27 16:49:20 -07001074 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001075
Chris Yea52ade12020-08-27 16:49:20 -07001076 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001077};
1078
Michael Wrightd02c5b62014-02-10 15:10:22 -08001079// --- FakeInputMapper ---
1080
1081class FakeInputMapper : public InputMapper {
1082 uint32_t mSources;
1083 int32_t mKeyboardType;
1084 int32_t mMetaState;
1085 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1086 KeyedVector<int32_t, int32_t> mScanCodeStates;
1087 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001088 // fake mapping which would normally come from keyCharacterMap
1089 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001090 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001091
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001092 std::mutex mLock;
1093 std::condition_variable mStateChangedCondition;
1094 bool mConfigureWasCalled GUARDED_BY(mLock);
1095 bool mResetWasCalled GUARDED_BY(mLock);
1096 bool mProcessWasCalled GUARDED_BY(mLock);
1097 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001098
Arthur Hungc23540e2018-11-29 20:42:11 +08001099 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001100public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001101 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1102 : InputMapper(deviceContext),
1103 mSources(sources),
1104 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001105 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001106 mConfigureWasCalled(false),
1107 mResetWasCalled(false),
1108 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001109
Chris Yea52ade12020-08-27 16:49:20 -07001110 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001111
1112 void setKeyboardType(int32_t keyboardType) {
1113 mKeyboardType = keyboardType;
1114 }
1115
1116 void setMetaState(int32_t metaState) {
1117 mMetaState = metaState;
1118 }
1119
1120 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001121 std::unique_lock<std::mutex> lock(mLock);
1122 base::ScopedLockAssertion assumeLocked(mLock);
1123 const bool configureCalled =
1124 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1125 return mConfigureWasCalled;
1126 });
1127 if (!configureCalled) {
1128 FAIL() << "Expected configure() to have been called.";
1129 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001130 mConfigureWasCalled = false;
1131 }
1132
1133 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001134 std::unique_lock<std::mutex> lock(mLock);
1135 base::ScopedLockAssertion assumeLocked(mLock);
1136 const bool resetCalled =
1137 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1138 return mResetWasCalled;
1139 });
1140 if (!resetCalled) {
1141 FAIL() << "Expected reset() to have been called.";
1142 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001143 mResetWasCalled = false;
1144 }
1145
Yi Kong9b14ac62018-07-17 13:48:38 -07001146 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001147 std::unique_lock<std::mutex> lock(mLock);
1148 base::ScopedLockAssertion assumeLocked(mLock);
1149 const bool processCalled =
1150 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1151 return mProcessWasCalled;
1152 });
1153 if (!processCalled) {
1154 FAIL() << "Expected process() to have been called.";
1155 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001156 if (outLastEvent) {
1157 *outLastEvent = mLastEvent;
1158 }
1159 mProcessWasCalled = false;
1160 }
1161
1162 void setKeyCodeState(int32_t keyCode, int32_t state) {
1163 mKeyCodeStates.replaceValueFor(keyCode, state);
1164 }
1165
1166 void setScanCodeState(int32_t scanCode, int32_t state) {
1167 mScanCodeStates.replaceValueFor(scanCode, state);
1168 }
1169
1170 void setSwitchState(int32_t switchCode, int32_t state) {
1171 mSwitchStates.replaceValueFor(switchCode, state);
1172 }
1173
1174 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001175 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001176 }
1177
Philip Junker4af3b3d2021-12-14 10:36:55 +01001178 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1179 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1180 }
1181
Michael Wrightd02c5b62014-02-10 15:10:22 -08001182private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001183 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001184
Chris Yea52ade12020-08-27 16:49:20 -07001185 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001186 InputMapper::populateDeviceInfo(deviceInfo);
1187
1188 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1189 deviceInfo->setKeyboardType(mKeyboardType);
1190 }
1191 }
1192
Chris Yea52ade12020-08-27 16:49:20 -07001193 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001194 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001195 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001196
1197 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001198 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001199 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1200 mViewport = config->getDisplayViewportByPort(*displayPort);
1201 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001202
1203 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001204 }
1205
Chris Yea52ade12020-08-27 16:49:20 -07001206 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001207 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001208 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001209 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001210 }
1211
Chris Yea52ade12020-08-27 16:49:20 -07001212 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001213 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001214 mLastEvent = *rawEvent;
1215 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001216 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001217 }
1218
Chris Yea52ade12020-08-27 16:49:20 -07001219 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001220 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1221 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1222 }
1223
Philip Junker4af3b3d2021-12-14 10:36:55 +01001224 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1225 auto it = mKeyCodeMapping.find(locationKeyCode);
1226 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1227 }
1228
Chris Yea52ade12020-08-27 16:49:20 -07001229 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001230 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1231 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1232 }
1233
Chris Yea52ade12020-08-27 16:49:20 -07001234 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001235 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1236 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1237 }
1238
Chris Yea52ade12020-08-27 16:49:20 -07001239 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001240 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -07001241 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001242 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001243 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1244 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1245 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001246 }
1247 }
1248 }
Chris Yea52ade12020-08-27 16:49:20 -07001249 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001250 return result;
1251 }
1252
1253 virtual int32_t getMetaState() {
1254 return mMetaState;
1255 }
1256
1257 virtual void fadePointer() {
1258 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001259
1260 virtual std::optional<int32_t> getAssociatedDisplay() {
1261 if (mViewport) {
1262 return std::make_optional(mViewport->displayId);
1263 }
1264 return std::nullopt;
1265 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001266};
1267
1268
1269// --- InstrumentedInputReader ---
1270
1271class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001272 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001273
1274public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001275 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1276 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001277 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001278 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001279
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001280 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001281
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001282 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001283
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001284 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001285 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001286 InputDeviceIdentifier identifier;
1287 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001288 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001289 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001290 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001291 }
1292
Prabir Pradhan28efc192019-11-05 01:10:04 +00001293 // Make the protected loopOnce method accessible to tests.
1294 using InputReader::loopOnce;
1295
Michael Wrightd02c5b62014-02-10 15:10:22 -08001296protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001297 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1298 const InputDeviceIdentifier& identifier)
1299 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001300 if (!mNextDevices.empty()) {
1301 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1302 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001303 return device;
1304 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001305 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001306 }
1307
arthurhungdcef2dc2020-08-11 14:47:50 +08001308 // --- FakeInputReaderContext ---
1309 class FakeInputReaderContext : public ContextImpl {
1310 int32_t mGlobalMetaState;
1311 bool mUpdateGlobalMetaStateWasCalled;
1312 int32_t mGeneration;
1313
1314 public:
1315 FakeInputReaderContext(InputReader* reader)
1316 : ContextImpl(reader),
1317 mGlobalMetaState(0),
1318 mUpdateGlobalMetaStateWasCalled(false),
1319 mGeneration(1) {}
1320
1321 virtual ~FakeInputReaderContext() {}
1322
1323 void assertUpdateGlobalMetaStateWasCalled() {
1324 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1325 << "Expected updateGlobalMetaState() to have been called.";
1326 mUpdateGlobalMetaStateWasCalled = false;
1327 }
1328
1329 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1330
1331 uint32_t getGeneration() { return mGeneration; }
1332
1333 void updateGlobalMetaState() override {
1334 mUpdateGlobalMetaStateWasCalled = true;
1335 ContextImpl::updateGlobalMetaState();
1336 }
1337
1338 int32_t getGlobalMetaState() override {
1339 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1340 }
1341
1342 int32_t bumpGeneration() override {
1343 mGeneration = ContextImpl::bumpGeneration();
1344 return mGeneration;
1345 }
1346 } mFakeContext;
1347
Michael Wrightd02c5b62014-02-10 15:10:22 -08001348 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001349
1350public:
1351 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001352};
1353
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001354// --- InputReaderPolicyTest ---
1355class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001356protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001357 sp<FakeInputReaderPolicy> mFakePolicy;
1358
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001359 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -07001360 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001361};
1362
1363/**
1364 * Check that empty set of viewports is an acceptable configuration.
1365 * Also try to get internal viewport two different ways - by type and by uniqueId.
1366 *
1367 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1368 * Such configuration is not currently allowed.
1369 */
1370TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001371 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001372
1373 // We didn't add any viewports yet, so there shouldn't be any.
1374 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001375 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001376 ASSERT_FALSE(internalViewport);
1377
1378 // Add an internal viewport, then clear it
1379 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001380 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001381 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001382
1383 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001384 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001385 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001386 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001387
1388 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001389 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001390 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001391 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001392
1393 mFakePolicy->clearViewports();
1394 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001395 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001396 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001397 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001398 ASSERT_FALSE(internalViewport);
1399}
1400
1401TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1402 const std::string internalUniqueId = "local:0";
1403 const std::string externalUniqueId = "local:1";
1404 const std::string virtualUniqueId1 = "virtual:2";
1405 const std::string virtualUniqueId2 = "virtual:3";
1406 constexpr int32_t virtualDisplayId1 = 2;
1407 constexpr int32_t virtualDisplayId2 = 3;
1408
1409 // Add an internal viewport
1410 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001411 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1412 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001413 // Add an external viewport
1414 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001415 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1416 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001417 // Add an virtual viewport
1418 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001419 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1420 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001421 // Add another virtual viewport
1422 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001423 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1424 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001425
1426 // Check matching by type for internal
1427 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001428 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001429 ASSERT_TRUE(internalViewport);
1430 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1431
1432 // Check matching by type for external
1433 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001434 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001435 ASSERT_TRUE(externalViewport);
1436 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1437
1438 // Check matching by uniqueId for virtual viewport #1
1439 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001440 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001441 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001442 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001443 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1444 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1445
1446 // Check matching by uniqueId for virtual viewport #2
1447 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001448 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001449 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001450 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001451 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1452 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1453}
1454
1455
1456/**
1457 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1458 * that lookup works by checking display id.
1459 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1460 */
1461TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1462 const std::string uniqueId1 = "uniqueId1";
1463 const std::string uniqueId2 = "uniqueId2";
1464 constexpr int32_t displayId1 = 2;
1465 constexpr int32_t displayId2 = 3;
1466
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001467 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1468 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001469 for (const ViewportType& type : types) {
1470 mFakePolicy->clearViewports();
1471 // Add a viewport
1472 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001473 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1474 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001475 // Add another viewport
1476 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001477 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1478 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001479
1480 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001481 std::optional<DisplayViewport> viewport1 =
1482 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001483 ASSERT_TRUE(viewport1);
1484 ASSERT_EQ(displayId1, viewport1->displayId);
1485 ASSERT_EQ(type, viewport1->type);
1486
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001487 std::optional<DisplayViewport> viewport2 =
1488 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001489 ASSERT_TRUE(viewport2);
1490 ASSERT_EQ(displayId2, viewport2->displayId);
1491 ASSERT_EQ(type, viewport2->type);
1492
1493 // When there are multiple viewports of the same kind, and uniqueId is not specified
1494 // in the call to getDisplayViewport, then that situation is not supported.
1495 // The viewports can be stored in any order, so we cannot rely on the order, since that
1496 // is just implementation detail.
1497 // However, we can check that it still returns *a* viewport, we just cannot assert
1498 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001499 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001500 ASSERT_TRUE(someViewport);
1501 }
1502}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001503
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001504/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001505 * When we have multiple internal displays make sure we always return the default display when
1506 * querying by type.
1507 */
1508TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1509 const std::string uniqueId1 = "uniqueId1";
1510 const std::string uniqueId2 = "uniqueId2";
1511 constexpr int32_t nonDefaultDisplayId = 2;
1512 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1513 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1514
1515 // Add the default display first and ensure it gets returned.
1516 mFakePolicy->clearViewports();
1517 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001518 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001519 ViewportType::INTERNAL);
1520 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001521 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001522 ViewportType::INTERNAL);
1523
1524 std::optional<DisplayViewport> viewport =
1525 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1526 ASSERT_TRUE(viewport);
1527 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1528 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1529
1530 // Add the default display second to make sure order doesn't matter.
1531 mFakePolicy->clearViewports();
1532 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001533 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001534 ViewportType::INTERNAL);
1535 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001536 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001537 ViewportType::INTERNAL);
1538
1539 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1540 ASSERT_TRUE(viewport);
1541 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1542 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1543}
1544
1545/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001546 * Check getDisplayViewportByPort
1547 */
1548TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001549 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001550 const std::string uniqueId1 = "uniqueId1";
1551 const std::string uniqueId2 = "uniqueId2";
1552 constexpr int32_t displayId1 = 1;
1553 constexpr int32_t displayId2 = 2;
1554 const uint8_t hdmi1 = 0;
1555 const uint8_t hdmi2 = 1;
1556 const uint8_t hdmi3 = 2;
1557
1558 mFakePolicy->clearViewports();
1559 // Add a viewport that's associated with some display port that's not of interest.
1560 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001561 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1562 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001563 // Add another viewport, connected to HDMI1 port
1564 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001565 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1566 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001567
1568 // Check that correct display viewport was returned by comparing the display ports.
1569 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1570 ASSERT_TRUE(hdmi1Viewport);
1571 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1572 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1573
1574 // Check that we can still get the same viewport using the uniqueId
1575 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1576 ASSERT_TRUE(hdmi1Viewport);
1577 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1578 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1579 ASSERT_EQ(type, hdmi1Viewport->type);
1580
1581 // Check that we cannot find a port with "HDMI2", because we never added one
1582 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1583 ASSERT_FALSE(hdmi2Viewport);
1584}
1585
Michael Wrightd02c5b62014-02-10 15:10:22 -08001586// --- InputReaderTest ---
1587
1588class InputReaderTest : public testing::Test {
1589protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001590 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001591 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001592 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001593 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001594
Chris Yea52ade12020-08-27 16:49:20 -07001595 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001596 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001597 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001598 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001599
Prabir Pradhan28efc192019-11-05 01:10:04 +00001600 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001601 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001602 }
1603
Chris Yea52ade12020-08-27 16:49:20 -07001604 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001605 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001606 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001607 }
1608
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001609 void addDevice(int32_t eventHubId, const std::string& name,
1610 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001611 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001612
1613 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001614 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001615 }
1616 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001617 mReader->loopOnce();
1618 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001619 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1620 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001621 }
1622
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001623 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001624 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001625 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001626 }
1627
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001628 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001629 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001630 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001631 }
1632
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001633 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001634 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001635 ftl::Flags<InputDeviceClass> classes,
1636 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001637 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001638 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1639 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001640 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001641 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001642 return mapper;
1643 }
1644};
1645
Chris Ye98d3f532020-10-01 21:48:59 -07001646TEST_F(InputReaderTest, PolicyGetInputDevices) {
1647 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001648 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001649 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001650
1651 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001652 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001653 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001654 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001655 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001656 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1657 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001658 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001659}
1660
Chris Yee7310032020-09-22 15:36:28 -07001661TEST_F(InputReaderTest, GetMergedInputDevices) {
1662 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1663 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1664 // Add two subdevices to device
1665 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1666 // Must add at least one mapper or the device will be ignored!
1667 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1668 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1669
1670 // Push same device instance for next device to be added, so they'll have same identifier.
1671 mReader->pushNextDevice(device);
1672 mReader->pushNextDevice(device);
1673 ASSERT_NO_FATAL_FAILURE(
1674 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1675 ASSERT_NO_FATAL_FAILURE(
1676 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1677
1678 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001679 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001680}
1681
Chris Yee14523a2020-12-19 13:46:00 -08001682TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1683 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1684 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1685 // Add two subdevices to device
1686 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1687 // Must add at least one mapper or the device will be ignored!
1688 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1689 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1690
1691 // Push same device instance for next device to be added, so they'll have same identifier.
1692 mReader->pushNextDevice(device);
1693 mReader->pushNextDevice(device);
1694 // Sensor device is initially disabled
1695 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1696 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1697 nullptr));
1698 // Device is disabled because the only sub device is a sensor device and disabled initially.
1699 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1700 ASSERT_FALSE(device->isEnabled());
1701 ASSERT_NO_FATAL_FAILURE(
1702 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1703 // The merged device is enabled if any sub device is enabled
1704 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1705 ASSERT_TRUE(device->isEnabled());
1706}
1707
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001708TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001709 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001710 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001711 constexpr int32_t eventHubId = 1;
1712 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001713 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001714 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001715 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001716 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001717
Yi Kong9b14ac62018-07-17 13:48:38 -07001718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001719
1720 NotifyDeviceResetArgs resetArgs;
1721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001722 ASSERT_EQ(deviceId, resetArgs.deviceId);
1723
1724 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001725 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001726 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001727
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001729 ASSERT_EQ(deviceId, resetArgs.deviceId);
1730 ASSERT_EQ(device->isEnabled(), false);
1731
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001732 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001733 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001736 ASSERT_EQ(device->isEnabled(), false);
1737
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001738 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001739 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001741 ASSERT_EQ(deviceId, resetArgs.deviceId);
1742 ASSERT_EQ(device->isEnabled(), true);
1743}
1744
Michael Wrightd02c5b62014-02-10 15:10:22 -08001745TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001746 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001747 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001748 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001749 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001750 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001751 AINPUT_SOURCE_KEYBOARD, nullptr);
1752 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001753
1754 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1755 AINPUT_SOURCE_ANY, AKEYCODE_A))
1756 << "Should return unknown when the device id is >= 0 but unknown.";
1757
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001758 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1759 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1760 << "Should return unknown when the device id is valid but the sources are not "
1761 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001762
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001763 ASSERT_EQ(AKEY_STATE_DOWN,
1764 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1765 AKEYCODE_A))
1766 << "Should return value provided by mapper when device id is valid and the device "
1767 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001768
1769 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1770 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1771 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1772
1773 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1774 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1775 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1776}
1777
Philip Junker4af3b3d2021-12-14 10:36:55 +01001778TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1779 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1780 constexpr int32_t eventHubId = 1;
1781 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1782 InputDeviceClass::KEYBOARD,
1783 AINPUT_SOURCE_KEYBOARD, nullptr);
1784 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1785
1786 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1787 << "Should return unknown when the device with the specified id is not found.";
1788
1789 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1790 << "Should return correct mapping when device id is valid and mapping exists.";
1791
1792 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1793 << "Should return the location key code when device id is valid and there's no "
1794 "mapping.";
1795}
1796
1797TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1798 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1799 constexpr int32_t eventHubId = 1;
1800 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1801 InputDeviceClass::JOYSTICK,
1802 AINPUT_SOURCE_GAMEPAD, nullptr);
1803 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1804
1805 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1806 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1807}
1808
Michael Wrightd02c5b62014-02-10 15:10:22 -08001809TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001810 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001811 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001812 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001813 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001814 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001815 AINPUT_SOURCE_KEYBOARD, nullptr);
1816 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001817
1818 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1819 AINPUT_SOURCE_ANY, KEY_A))
1820 << "Should return unknown when the device id is >= 0 but unknown.";
1821
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001822 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1823 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1824 << "Should return unknown when the device id is valid but the sources are not "
1825 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001826
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001827 ASSERT_EQ(AKEY_STATE_DOWN,
1828 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1829 KEY_A))
1830 << "Should return value provided by mapper when device id is valid and the device "
1831 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001832
1833 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1834 AINPUT_SOURCE_TRACKBALL, KEY_A))
1835 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1836
1837 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1838 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1839 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1840}
1841
1842TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001843 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001844 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001845 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001846 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001847 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001848 AINPUT_SOURCE_KEYBOARD, nullptr);
1849 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001850
1851 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1852 AINPUT_SOURCE_ANY, SW_LID))
1853 << "Should return unknown when the device id is >= 0 but unknown.";
1854
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001855 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1856 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1857 << "Should return unknown when the device id is valid but the sources are not "
1858 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001859
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001860 ASSERT_EQ(AKEY_STATE_DOWN,
1861 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1862 SW_LID))
1863 << "Should return value provided by mapper when device id is valid and the device "
1864 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001865
1866 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1867 AINPUT_SOURCE_TRACKBALL, SW_LID))
1868 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1869
1870 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1871 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1872 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1873}
1874
1875TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001876 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001877 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001878 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001879 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001880 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001881 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001882
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001883 mapper.addSupportedKeyCode(AKEYCODE_A);
1884 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001885
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001886 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001887 uint8_t flags[4] = { 0, 0, 0, 1 };
1888
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001889 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08001890 << "Should return false when device id is >= 0 but unknown.";
1891 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1892
1893 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001894 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001895 << "Should return false when device id is valid but the sources are not supported by "
1896 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001897 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1898
1899 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001900 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001901 keyCodes, flags))
1902 << "Should return value provided by mapper when device id is valid and the device "
1903 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001904 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1905
1906 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001907 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1908 << "Should return false when the device id is < 0 but the sources are not supported by "
1909 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001910 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1911
1912 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001913 ASSERT_TRUE(
1914 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1915 << "Should return value provided by mapper when device id is < 0 and one of the "
1916 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001917 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1918}
1919
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001920TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001921 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001922 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001923
1924 NotifyConfigurationChangedArgs args;
1925
1926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1927 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1928}
1929
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001930TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001931 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001932 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001933 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001934 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001935 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001936 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001937 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001938 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001939
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001940 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001941 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001942 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1943
1944 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001945 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001946 ASSERT_EQ(when, event.when);
1947 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001948 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001949 ASSERT_EQ(EV_KEY, event.type);
1950 ASSERT_EQ(KEY_A, event.code);
1951 ASSERT_EQ(1, event.value);
1952}
1953
Garfield Tan1c7bc862020-01-28 13:24:04 -08001954TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001955 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001956 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001957 constexpr int32_t eventHubId = 1;
1958 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001959 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001960 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001961 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001962 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001963
1964 NotifyDeviceResetArgs resetArgs;
1965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001966 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001967
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001968 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001969 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001971 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001972 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001973
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001974 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001975 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001977 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001978 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001979
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001980 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001981 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001983 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001984 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001985}
1986
Garfield Tan1c7bc862020-01-28 13:24:04 -08001987TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1988 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001989 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001990 constexpr int32_t eventHubId = 1;
1991 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1992 // Must add at least one mapper or the device will be ignored!
1993 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001994 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001995 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1996
1997 NotifyDeviceResetArgs resetArgs;
1998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1999 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
2000}
2001
Arthur Hungc23540e2018-11-29 20:42:11 +08002002TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002003 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002004 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002005 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08002006 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002007 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2008 FakeInputMapper& mapper =
2009 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002010 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08002011
2012 const uint8_t hdmi1 = 1;
2013
2014 // Associated touch screen with second display.
2015 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2016
2017 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00002018 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08002019 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002020 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002021 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002022 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002023 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002024 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002025 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002026 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002027
2028 // Add the device, and make sure all of the callbacks are triggered.
2029 // The device is added after the input port associations are processed since
2030 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002031 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002034 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002035
Arthur Hung2c9a3342019-07-23 14:18:59 +08002036 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002037 ASSERT_EQ(deviceId, device->getId());
2038 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2039 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002040
2041 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002042 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002043 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002044 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002045}
2046
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002047TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2048 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002049 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002050 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2051 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2052 // Must add at least one mapper or the device will be ignored!
2053 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2054 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2055 mReader->pushNextDevice(device);
2056 mReader->pushNextDevice(device);
2057 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2058 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2059
2060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2061
2062 NotifyDeviceResetArgs resetArgs;
2063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2064 ASSERT_EQ(deviceId, resetArgs.deviceId);
2065 ASSERT_TRUE(device->isEnabled());
2066 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2067 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2068
2069 disableDevice(deviceId);
2070 mReader->loopOnce();
2071
2072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2073 ASSERT_EQ(deviceId, resetArgs.deviceId);
2074 ASSERT_FALSE(device->isEnabled());
2075 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2076 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2077
2078 enableDevice(deviceId);
2079 mReader->loopOnce();
2080
2081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2082 ASSERT_EQ(deviceId, resetArgs.deviceId);
2083 ASSERT_TRUE(device->isEnabled());
2084 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2085 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2086}
2087
2088TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2089 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002090 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002091 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2092 // Add two subdevices to device
2093 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2094 FakeInputMapper& mapperDevice1 =
2095 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2096 FakeInputMapper& mapperDevice2 =
2097 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2098 mReader->pushNextDevice(device);
2099 mReader->pushNextDevice(device);
2100 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2101 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2102
2103 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2104 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2105
2106 ASSERT_EQ(AKEY_STATE_DOWN,
2107 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2108 ASSERT_EQ(AKEY_STATE_DOWN,
2109 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2110 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2111 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2112}
2113
Prabir Pradhan7e186182020-11-10 13:56:45 -08002114TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2115 NotifyPointerCaptureChangedArgs args;
2116
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002117 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002118 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2119 mReader->loopOnce();
2120 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002121 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2122 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002123
2124 mFakePolicy->setPointerCapture(false);
2125 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2126 mReader->loopOnce();
2127 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002128 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002129
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002130 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002131 // does not change.
2132 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2133 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002134 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002135}
2136
Chris Ye87143712020-11-10 05:05:58 +00002137class FakeVibratorInputMapper : public FakeInputMapper {
2138public:
2139 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2140 : FakeInputMapper(deviceContext, sources) {}
2141
2142 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2143};
2144
2145TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2146 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002147 ftl::Flags<InputDeviceClass> deviceClass =
2148 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002149 constexpr int32_t eventHubId = 1;
2150 const char* DEVICE_LOCATION = "BLUETOOTH";
2151 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2152 FakeVibratorInputMapper& mapper =
2153 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2154 mReader->pushNextDevice(device);
2155
2156 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2157 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2158
2159 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2160 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2161}
2162
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002163// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002164
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002165class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002166public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002167 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002168
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002169 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002170
Andy Chenf9f1a022022-08-29 20:07:10 -04002171 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
2172
Chris Yee2b1e5c2021-03-10 22:45:12 -08002173 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2174
2175 void dump(std::string& dump) override {}
2176
2177 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2178 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002179 }
2180
Chris Yee2b1e5c2021-03-10 22:45:12 -08002181 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2182 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002183 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002184
2185 bool setLightColor(int32_t lightId, int32_t color) override {
2186 getDeviceContext().setLightBrightness(lightId, color >> 24);
2187 return true;
2188 }
2189
2190 std::optional<int32_t> getLightColor(int32_t lightId) override {
2191 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2192 if (!result.has_value()) {
2193 return std::nullopt;
2194 }
2195 return result.value() << 24;
2196 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002197
2198 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2199
2200 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2201
2202private:
2203 InputDeviceContext& mDeviceContext;
2204 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2205 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04002206 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002207};
2208
Chris Yee2b1e5c2021-03-10 22:45:12 -08002209TEST_F(InputReaderTest, BatteryGetCapacity) {
2210 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002211 ftl::Flags<InputDeviceClass> deviceClass =
2212 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002213 constexpr int32_t eventHubId = 1;
2214 const char* DEVICE_LOCATION = "BLUETOOTH";
2215 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002216 FakePeripheralController& controller =
2217 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002218 mReader->pushNextDevice(device);
2219
2220 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2221
2222 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2223 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2224}
2225
2226TEST_F(InputReaderTest, BatteryGetStatus) {
2227 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002228 ftl::Flags<InputDeviceClass> deviceClass =
2229 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002230 constexpr int32_t eventHubId = 1;
2231 const char* DEVICE_LOCATION = "BLUETOOTH";
2232 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002233 FakePeripheralController& controller =
2234 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002235 mReader->pushNextDevice(device);
2236
2237 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2238
2239 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2240 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2241}
2242
Prabir Pradhane287ecd2022-09-07 21:18:05 +00002243TEST_F(InputReaderTest, BatteryGetDevicePath) {
2244 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2245 ftl::Flags<InputDeviceClass> deviceClass =
2246 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2247 constexpr int32_t eventHubId = 1;
2248 const char* DEVICE_LOCATION = "BLUETOOTH";
2249 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2250 device->addController<FakePeripheralController>(eventHubId);
2251 mReader->pushNextDevice(device);
2252
2253 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2254
2255 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), BATTERY_DEVPATH);
2256}
2257
Chris Ye3fdbfef2021-01-06 18:45:18 -08002258TEST_F(InputReaderTest, LightGetColor) {
2259 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002260 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002261 constexpr int32_t eventHubId = 1;
2262 const char* DEVICE_LOCATION = "BLUETOOTH";
2263 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002264 FakePeripheralController& controller =
2265 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002266 mReader->pushNextDevice(device);
2267 RawLightInfo info = {.id = 1,
2268 .name = "Mono",
2269 .maxBrightness = 255,
2270 .flags = InputLightClass::BRIGHTNESS,
2271 .path = ""};
2272 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2273 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2274
2275 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002276
Chris Yee2b1e5c2021-03-10 22:45:12 -08002277 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2278 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002279 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2280 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2281}
2282
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002283// --- InputReaderIntegrationTest ---
2284
2285// These tests create and interact with the InputReader only through its interface.
2286// The InputReader is started during SetUp(), which starts its processing in its own
2287// thread. The tests use linux uinput to emulate input devices.
2288// NOTE: Interacting with the physical device while these tests are running may cause
2289// the tests to fail.
2290class InputReaderIntegrationTest : public testing::Test {
2291protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002292 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002293 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002294 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002295
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002296 std::shared_ptr<FakePointerController> mFakePointerController;
2297
Chris Yea52ade12020-08-27 16:49:20 -07002298 void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002299 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002300 mFakePointerController = std::make_shared<FakePointerController>();
2301 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002302 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2303 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002304
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002305 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2306 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002307 ASSERT_EQ(mReader->start(), OK);
2308
2309 // Since this test is run on a real device, all the input devices connected
2310 // to the test device will show up in mReader. We wait for those input devices to
2311 // show up before beginning the tests.
2312 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2313 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2314 }
2315
Chris Yea52ade12020-08-27 16:49:20 -07002316 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002317 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002318 mReader.reset();
2319 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002320 mFakePolicy.clear();
2321 }
2322};
2323
2324TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2325 // An invalid input device that is only used for this test.
2326 class InvalidUinputDevice : public UinputDevice {
2327 public:
2328 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2329
2330 private:
2331 void configureDevice(int fd, uinput_user_dev* device) override {}
2332 };
2333
2334 const size_t numDevices = mFakePolicy->getInputDevices().size();
2335
2336 // UinputDevice does not set any event or key bits, so InputReader should not
2337 // consider it as a valid device.
2338 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2339 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2340 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2341 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2342
2343 invalidDevice.reset();
2344 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2345 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2346 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2347}
2348
2349TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2350 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2351
2352 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2353 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2354 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2355 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2356
2357 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002358 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002359 const auto& it =
2360 std::find_if(inputDevices.begin(), inputDevices.end(),
2361 [&keyboard](const InputDeviceInfo& info) {
2362 return info.getIdentifier().name == keyboard->getName();
2363 });
2364
2365 ASSERT_NE(it, inputDevices.end());
2366 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2367 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2368 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002369
2370 keyboard.reset();
2371 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2372 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2373 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2374}
2375
2376TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2377 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2378 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2379
2380 NotifyConfigurationChangedArgs configChangedArgs;
2381 ASSERT_NO_FATAL_FAILURE(
2382 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002383 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002384 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2385
2386 NotifyKeyArgs keyArgs;
2387 keyboard->pressAndReleaseHomeKey();
2388 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2389 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002390 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002391 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002392 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002393 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002394 prevTimestamp = keyArgs.eventTime;
2395
2396 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2397 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002398 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002399 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002400 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002401}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002402
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002403/**
2404 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2405 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2406 * are passed to the listener.
2407 */
2408static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2409TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2410 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2411 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2412 NotifyKeyArgs keyArgs;
2413
2414 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2415 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2416 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2417 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2418
2419 controller->pressAndReleaseKey(BTN_GEAR_UP);
2420 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2421 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2422 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2423}
2424
Arthur Hungaab25622020-01-16 11:22:11 +08002425// --- TouchProcessTest ---
2426class TouchIntegrationTest : public InputReaderIntegrationTest {
2427protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002428 const std::string UNIQUE_ID = "local:0";
2429
Chris Yea52ade12020-08-27 16:49:20 -07002430 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002431 InputReaderIntegrationTest::SetUp();
2432 // At least add an internal display.
2433 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2434 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002435 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002436
2437 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2438 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2439 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2440 }
2441
2442 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2443 int32_t orientation, const std::string& uniqueId,
2444 std::optional<uint8_t> physicalPort,
2445 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002446 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2447 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002448 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2449 }
2450
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002451 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2452 NotifyMotionArgs args;
2453 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2454 EXPECT_EQ(action, args.action);
2455 ASSERT_EQ(points.size(), args.pointerCount);
2456 for (size_t i = 0; i < args.pointerCount; i++) {
2457 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2458 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2459 }
2460 }
2461
Arthur Hungaab25622020-01-16 11:22:11 +08002462 std::unique_ptr<UinputTouchScreen> mDevice;
2463};
2464
2465TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2466 NotifyMotionArgs args;
2467 const Point centerPoint = mDevice->getCenterPoint();
2468
2469 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002470 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002471 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002472 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002473 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2474 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2475
2476 // ACTION_MOVE
2477 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002478 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002479 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2481
2482 // ACTION_UP
2483 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002484 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002485 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2486 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2487}
2488
2489TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2490 NotifyMotionArgs args;
2491 const Point centerPoint = mDevice->getCenterPoint();
2492
2493 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002494 mDevice->sendSlot(FIRST_SLOT);
2495 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002496 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002497 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002498 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2499 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2500
2501 // ACTION_POINTER_DOWN (Second slot)
2502 const Point secondPoint = centerPoint + Point(100, 100);
2503 mDevice->sendSlot(SECOND_SLOT);
2504 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002505 mDevice->sendDown(secondPoint);
2506 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002507 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002508 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002509
2510 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002511 mDevice->sendMove(secondPoint + Point(1, 1));
2512 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002513 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2514 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2515
2516 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002517 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002518 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002519 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002520 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002521
2522 // ACTION_UP
2523 mDevice->sendSlot(FIRST_SLOT);
2524 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002525 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002526 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2527 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2528}
2529
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002530/**
2531 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2532 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2533 * data?
2534 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2535 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2536 * for Pointer 0 only is generated after.
2537 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2538 * events, we will not miss any information.
2539 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2540 * event generated afterwards that contains the newest movement of pointer 0.
2541 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2542 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2543 * losing information about non-palm pointers.
2544 */
2545TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2546 NotifyMotionArgs args;
2547 const Point centerPoint = mDevice->getCenterPoint();
2548
2549 // ACTION_DOWN
2550 mDevice->sendSlot(FIRST_SLOT);
2551 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2552 mDevice->sendDown(centerPoint);
2553 mDevice->sendSync();
2554 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2555
2556 // ACTION_POINTER_DOWN (Second slot)
2557 const Point secondPoint = centerPoint + Point(100, 100);
2558 mDevice->sendSlot(SECOND_SLOT);
2559 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2560 mDevice->sendDown(secondPoint);
2561 mDevice->sendSync();
2562 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2563
2564 // ACTION_MOVE (First slot)
2565 mDevice->sendSlot(FIRST_SLOT);
2566 mDevice->sendMove(centerPoint + Point(5, 5));
2567 // ACTION_POINTER_UP (Second slot)
2568 mDevice->sendSlot(SECOND_SLOT);
2569 mDevice->sendPointerUp();
2570 // Send a single sync for the above 2 pointer updates
2571 mDevice->sendSync();
2572
2573 // First, we should get POINTER_UP for the second pointer
2574 assertReceivedMotion(ACTION_POINTER_1_UP,
2575 {/*first pointer */ centerPoint + Point(5, 5),
2576 /*second pointer*/ secondPoint});
2577
2578 // Next, the MOVE event for the first pointer
2579 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2580}
2581
2582/**
2583 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2584 * move, and then it will go up, all in the same frame.
2585 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2586 * gets sent to the listener.
2587 */
2588TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2589 NotifyMotionArgs args;
2590 const Point centerPoint = mDevice->getCenterPoint();
2591
2592 // ACTION_DOWN
2593 mDevice->sendSlot(FIRST_SLOT);
2594 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2595 mDevice->sendDown(centerPoint);
2596 mDevice->sendSync();
2597 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2598
2599 // ACTION_POINTER_DOWN (Second slot)
2600 const Point secondPoint = centerPoint + Point(100, 100);
2601 mDevice->sendSlot(SECOND_SLOT);
2602 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2603 mDevice->sendDown(secondPoint);
2604 mDevice->sendSync();
2605 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2606
2607 // ACTION_MOVE (First slot)
2608 mDevice->sendSlot(FIRST_SLOT);
2609 mDevice->sendMove(centerPoint + Point(5, 5));
2610 // ACTION_POINTER_UP (Second slot)
2611 mDevice->sendSlot(SECOND_SLOT);
2612 mDevice->sendMove(secondPoint + Point(6, 6));
2613 mDevice->sendPointerUp();
2614 // Send a single sync for the above 2 pointer updates
2615 mDevice->sendSync();
2616
2617 // First, we should get POINTER_UP for the second pointer
2618 // The movement of the second pointer during the liftoff frame is ignored.
2619 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2620 assertReceivedMotion(ACTION_POINTER_1_UP,
2621 {/*first pointer */ centerPoint + Point(5, 5),
2622 /*second pointer*/ secondPoint});
2623
2624 // Next, the MOVE event for the first pointer
2625 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2626}
2627
Arthur Hungaab25622020-01-16 11:22:11 +08002628TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2629 NotifyMotionArgs args;
2630 const Point centerPoint = mDevice->getCenterPoint();
2631
2632 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002633 mDevice->sendSlot(FIRST_SLOT);
2634 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002635 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002636 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002637 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2638 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2639
arthurhungcc7f9802020-04-30 17:55:40 +08002640 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002641 const Point secondPoint = centerPoint + Point(100, 100);
2642 mDevice->sendSlot(SECOND_SLOT);
2643 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2644 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002645 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002646 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002647 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002648
arthurhungcc7f9802020-04-30 17:55:40 +08002649 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002650 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002651 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002652 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2653 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2654
arthurhungcc7f9802020-04-30 17:55:40 +08002655 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2656 // a palm event.
2657 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002658 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002659 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002660 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002661 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002662 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002663
arthurhungcc7f9802020-04-30 17:55:40 +08002664 // Send up to second slot, expect first slot send moving.
2665 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002666 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002667 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2668 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002669
arthurhungcc7f9802020-04-30 17:55:40 +08002670 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002671 mDevice->sendSlot(FIRST_SLOT);
2672 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002673 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002674
arthurhungcc7f9802020-04-30 17:55:40 +08002675 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2676 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002677}
2678
Michael Wrightd02c5b62014-02-10 15:10:22 -08002679// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002680class InputDeviceTest : public testing::Test {
2681protected:
2682 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002683 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002684 static const int32_t DEVICE_ID;
2685 static const int32_t DEVICE_GENERATION;
2686 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002687 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002688 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002689
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002690 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002691 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002692 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002693 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002694 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002695
Chris Yea52ade12020-08-27 16:49:20 -07002696 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002697 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002698 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002699 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002700 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002701 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002702 InputDeviceIdentifier identifier;
2703 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002704 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002705 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002706 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002707 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002708 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002709 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002710 }
2711
Chris Yea52ade12020-08-27 16:49:20 -07002712 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002713 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002714 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002715 }
2716};
2717
2718const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002719const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002720const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002721const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2722const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002723const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002724 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002725const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002726
2727TEST_F(InputDeviceTest, ImmutableProperties) {
2728 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002729 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002730 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002731}
2732
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002733TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
2734 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
2735
2736 // Configuration
2737 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2738 InputReaderConfiguration config;
2739 mDevice->configure(ARBITRARY_TIME, &config, 0);
2740
2741 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
2742}
2743
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002744TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2745 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002746}
2747
Michael Wrightd02c5b62014-02-10 15:10:22 -08002748TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2749 // Configuration.
2750 InputReaderConfiguration config;
2751 mDevice->configure(ARBITRARY_TIME, &config, 0);
2752
2753 // Reset.
2754 mDevice->reset(ARBITRARY_TIME);
2755
2756 NotifyDeviceResetArgs resetArgs;
2757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2758 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2759 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2760
2761 // Metadata.
2762 ASSERT_TRUE(mDevice->isIgnored());
2763 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2764
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002765 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002766 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002767 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002768 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2769 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2770
2771 // State queries.
2772 ASSERT_EQ(0, mDevice->getMetaState());
2773
2774 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2775 << "Ignored device should return unknown key code state.";
2776 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2777 << "Ignored device should return unknown scan code state.";
2778 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2779 << "Ignored device should return unknown switch state.";
2780
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002781 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002782 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002783 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002784 << "Ignored device should never mark any key codes.";
2785 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2786 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2787}
2788
2789TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2790 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002791 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08002792
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002793 FakeInputMapper& mapper1 =
2794 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002795 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2796 mapper1.setMetaState(AMETA_ALT_ON);
2797 mapper1.addSupportedKeyCode(AKEYCODE_A);
2798 mapper1.addSupportedKeyCode(AKEYCODE_B);
2799 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2800 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2801 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2802 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2803 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002804
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002805 FakeInputMapper& mapper2 =
2806 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002807 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002808
2809 InputReaderConfiguration config;
2810 mDevice->configure(ARBITRARY_TIME, &config, 0);
2811
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002812 std::string propertyValue;
2813 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002814 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002815 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002816
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002817 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2818 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002819
2820 // Reset
2821 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002822 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2823 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002824
2825 NotifyDeviceResetArgs resetArgs;
2826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2827 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2828 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2829
2830 // Metadata.
2831 ASSERT_FALSE(mDevice->isIgnored());
2832 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2833
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002834 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002835 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002836 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002837 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2838 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2839
2840 // State queries.
2841 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2842 << "Should query mappers and combine meta states.";
2843
2844 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2845 << "Should return unknown key code state when source not supported.";
2846 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2847 << "Should return unknown scan code state when source not supported.";
2848 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2849 << "Should return unknown switch state when source not supported.";
2850
2851 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2852 << "Should query mapper when source is supported.";
2853 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2854 << "Should query mapper when source is supported.";
2855 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2856 << "Should query mapper when source is supported.";
2857
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002858 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002859 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002860 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002861 << "Should do nothing when source is unsupported.";
2862 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2863 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2864 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2865 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2866
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002867 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002868 << "Should query mapper when source is supported.";
2869 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2870 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2871 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2872 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2873
2874 // Event handling.
2875 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002876 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002877 mDevice->process(&event, 1);
2878
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002879 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2880 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002881}
2882
Arthur Hung2c9a3342019-07-23 14:18:59 +08002883// A single input device is associated with a specific display. Check that:
2884// 1. Device is disabled if the viewport corresponding to the associated display is not found
2885// 2. Device is disabled when setEnabled API is called
2886TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002887 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002888
2889 // First Configuration.
2890 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2891
2892 // Device should be enabled by default.
2893 ASSERT_TRUE(mDevice->isEnabled());
2894
2895 // Prepare associated info.
2896 constexpr uint8_t hdmi = 1;
2897 const std::string UNIQUE_ID = "local:1";
2898
2899 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2900 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2901 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2902 // Device should be disabled because it is associated with a specific display via
2903 // input port <-> display port association, but the corresponding display is not found
2904 ASSERT_FALSE(mDevice->isEnabled());
2905
2906 // Prepare displays.
2907 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002908 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2909 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002910 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2911 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2912 ASSERT_TRUE(mDevice->isEnabled());
2913
2914 // Device should be disabled after set disable.
2915 mFakePolicy->addDisabledDevice(mDevice->getId());
2916 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2917 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2918 ASSERT_FALSE(mDevice->isEnabled());
2919
2920 // Device should still be disabled even found the associated display.
2921 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2922 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2923 ASSERT_FALSE(mDevice->isEnabled());
2924}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002925
Christine Franks1ba71cc2021-04-07 14:37:42 -07002926TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2927 // Device should be enabled by default.
2928 mFakePolicy->clearViewports();
2929 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2930 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2931 ASSERT_TRUE(mDevice->isEnabled());
2932
2933 // Device should be disabled because it is associated with a specific display, but the
2934 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002935 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002936 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2937 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2938 ASSERT_FALSE(mDevice->isEnabled());
2939
2940 // Device should be enabled when a display is found.
2941 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2942 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2943 NO_PORT, ViewportType::INTERNAL);
2944 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2945 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2946 ASSERT_TRUE(mDevice->isEnabled());
2947
2948 // Device should be disabled after set disable.
2949 mFakePolicy->addDisabledDevice(mDevice->getId());
2950 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2951 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2952 ASSERT_FALSE(mDevice->isEnabled());
2953
2954 // Device should still be disabled even found the associated display.
2955 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2956 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2957 ASSERT_FALSE(mDevice->isEnabled());
2958}
2959
Christine Franks2a2293c2022-01-18 11:51:16 -08002960TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2961 mFakePolicy->clearViewports();
2962 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2963 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2964
Christine Franks2a2293c2022-01-18 11:51:16 -08002965 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2966 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2967 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2968 NO_PORT, ViewportType::INTERNAL);
2969 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2970 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2971 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2972}
2973
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002974/**
2975 * This test reproduces a crash caused by a dangling reference that remains after device is added
2976 * and removed. The reference is accessed in InputDevice::dump(..);
2977 */
2978TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2979 constexpr int32_t TEST_EVENTHUB_ID = 10;
2980 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2981
2982 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
2983 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
2984 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2985 std::string dumpStr, eventHubDevStr;
2986 device.dump(dumpStr, eventHubDevStr);
2987}
2988
Michael Wrightd02c5b62014-02-10 15:10:22 -08002989// --- InputMapperTest ---
2990
2991class InputMapperTest : public testing::Test {
2992protected:
2993 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002994 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002995 static const int32_t DEVICE_ID;
2996 static const int32_t DEVICE_GENERATION;
2997 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002998 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002999 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003000
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003001 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003002 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003003 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08003004 std::unique_ptr<InstrumentedInputReader> mReader;
3005 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003006
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003007 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003008 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003009 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003010 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08003011 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003012 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08003013 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003014 // Consume the device reset notification generated when adding a new device.
3015 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003016 }
3017
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003018 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003019 SetUp(DEVICE_CLASSES);
3020 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003021
Chris Yea52ade12020-08-27 16:49:20 -07003022 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003023 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003024 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003025 }
3026
3027 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003028 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003029 }
3030
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003031 void configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00003032 if (!changes ||
3033 (changes &
3034 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
3035 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003036 mReader->requestRefreshConfiguration(changes);
3037 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08003038 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003039 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003040 // Loop the reader to flush the input listener queue.
3041 mReader->loopOnce();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003042 }
3043
arthurhungdcef2dc2020-08-11 14:47:50 +08003044 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
3045 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003046 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003047 InputDeviceIdentifier identifier;
3048 identifier.name = name;
3049 identifier.location = location;
3050 std::shared_ptr<InputDevice> device =
3051 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
3052 identifier);
3053 mReader->pushNextDevice(device);
3054 mFakeEventHub->addDevice(eventHubId, name, classes);
3055 mReader->loopOnce();
3056 return device;
3057 }
3058
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003059 template <class T, typename... Args>
3060 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003061 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003062 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003063 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07003064 mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003065 // Loop the reader to flush the input listener queue.
3066 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003067 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003068 }
3069
3070 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003071 int32_t orientation, const std::string& uniqueId,
3072 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003073 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3074 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003075 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3076 }
3077
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003078 void clearViewports() {
3079 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003080 }
3081
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003082 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
3083 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003084 RawEvent event;
3085 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003086 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003087 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003088 event.type = type;
3089 event.code = code;
3090 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003091 mapper.process(&event);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003092 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003093 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003094 }
3095
3096 static void assertMotionRange(const InputDeviceInfo& info,
3097 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3098 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003099 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3101 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3102 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3103 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3104 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3105 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3106 }
3107
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003108 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3109 float size, float touchMajor, float touchMinor, float toolMajor,
3110 float toolMinor, float orientation, float distance,
3111 float scaledAxisEpsilon = 1.f) {
3112 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3113 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003114 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3115 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003116 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3117 scaledAxisEpsilon);
3118 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3119 scaledAxisEpsilon);
3120 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3121 scaledAxisEpsilon);
3122 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3123 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003124 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3125 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3126 }
3127
Michael Wright17db18e2020-06-26 20:51:44 +01003128 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003129 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003130 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003131 ASSERT_NEAR(x, actualX, 1);
3132 ASSERT_NEAR(y, actualY, 1);
3133 }
3134};
3135
3136const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003137const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003138const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003139const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3140const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003141const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3142 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003143const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003144
3145// --- SwitchInputMapperTest ---
3146
3147class SwitchInputMapperTest : public InputMapperTest {
3148protected:
3149};
3150
3151TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003152 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003153
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003154 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003155}
3156
3157TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003158 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003159
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003160 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003161 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003162
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003163 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003164 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003165}
3166
3167TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003168 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003169
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003170 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3171 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3172 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3173 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003174
3175 NotifySwitchArgs args;
3176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3177 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003178 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3179 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003180 args.switchMask);
3181 ASSERT_EQ(uint32_t(0), args.policyFlags);
3182}
3183
Chris Ye87143712020-11-10 05:05:58 +00003184// --- VibratorInputMapperTest ---
3185class VibratorInputMapperTest : public InputMapperTest {
3186protected:
3187 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3188};
3189
3190TEST_F(VibratorInputMapperTest, GetSources) {
3191 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3192
3193 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3194}
3195
3196TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3197 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3198
3199 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3200}
3201
3202TEST_F(VibratorInputMapperTest, Vibrate) {
3203 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003204 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003205 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3206
3207 VibrationElement pattern(2);
3208 VibrationSequence sequence(2);
3209 pattern.duration = std::chrono::milliseconds(200);
3210 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3211 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3212 sequence.addElement(pattern);
3213 pattern.duration = std::chrono::milliseconds(500);
3214 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3215 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3216 sequence.addElement(pattern);
3217
3218 std::vector<int64_t> timings = {0, 1};
3219 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3220
3221 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003222 // Start vibrating
3223 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003224 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003225 // Verify vibrator state listener was notified.
3226 mReader->loopOnce();
3227 NotifyVibratorStateArgs args;
3228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3229 ASSERT_EQ(DEVICE_ID, args.deviceId);
3230 ASSERT_TRUE(args.isOn);
3231 // Stop vibrating
3232 mapper.cancelVibrate(VIBRATION_TOKEN);
3233 ASSERT_FALSE(mapper.isVibrating());
3234 // Verify vibrator state listener was notified.
3235 mReader->loopOnce();
3236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3237 ASSERT_EQ(DEVICE_ID, args.deviceId);
3238 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003239}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003240
Chris Yef59a2f42020-10-16 12:55:26 -07003241// --- SensorInputMapperTest ---
3242
3243class SensorInputMapperTest : public InputMapperTest {
3244protected:
3245 static const int32_t ACCEL_RAW_MIN;
3246 static const int32_t ACCEL_RAW_MAX;
3247 static const int32_t ACCEL_RAW_FUZZ;
3248 static const int32_t ACCEL_RAW_FLAT;
3249 static const int32_t ACCEL_RAW_RESOLUTION;
3250
3251 static const int32_t GYRO_RAW_MIN;
3252 static const int32_t GYRO_RAW_MAX;
3253 static const int32_t GYRO_RAW_FUZZ;
3254 static const int32_t GYRO_RAW_FLAT;
3255 static const int32_t GYRO_RAW_RESOLUTION;
3256
3257 static const float GRAVITY_MS2_UNIT;
3258 static const float DEGREE_RADIAN_UNIT;
3259
3260 void prepareAccelAxes();
3261 void prepareGyroAxes();
3262 void setAccelProperties();
3263 void setGyroProperties();
3264 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3265};
3266
3267const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3268const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3269const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3270const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3271const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3272
3273const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3274const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3275const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3276const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3277const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3278
3279const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3280const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3281
3282void SensorInputMapperTest::prepareAccelAxes() {
3283 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3284 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3285 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3286 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3287 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3288 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3289}
3290
3291void SensorInputMapperTest::prepareGyroAxes() {
3292 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3293 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3294 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3295 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3296 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3297 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3298}
3299
3300void SensorInputMapperTest::setAccelProperties() {
3301 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3302 /* sensorDataIndex */ 0);
3303 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3304 /* sensorDataIndex */ 1);
3305 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3306 /* sensorDataIndex */ 2);
3307 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3308 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3309 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3310 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3311 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3312}
3313
3314void SensorInputMapperTest::setGyroProperties() {
3315 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3316 /* sensorDataIndex */ 0);
3317 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3318 /* sensorDataIndex */ 1);
3319 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3320 /* sensorDataIndex */ 2);
3321 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3322 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3323 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3324 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3325 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3326}
3327
3328TEST_F(SensorInputMapperTest, GetSources) {
3329 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3330
3331 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3332}
3333
3334TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3335 setAccelProperties();
3336 prepareAccelAxes();
3337 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3338
3339 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3340 std::chrono::microseconds(10000),
3341 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003342 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003343 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3344 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3345 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3346 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3347 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003348
3349 NotifySensorArgs args;
3350 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3351 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3352 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3353
3354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3355 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3356 ASSERT_EQ(args.deviceId, DEVICE_ID);
3357 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3358 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3359 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3360 ASSERT_EQ(args.values, values);
3361 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3362}
3363
3364TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3365 setGyroProperties();
3366 prepareGyroAxes();
3367 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3368
3369 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3370 std::chrono::microseconds(10000),
3371 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003372 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003373 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3374 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3375 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3377 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003378
3379 NotifySensorArgs args;
3380 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3381 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3382 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3383
3384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3385 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3386 ASSERT_EQ(args.deviceId, DEVICE_ID);
3387 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3388 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3389 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3390 ASSERT_EQ(args.values, values);
3391 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3392}
3393
Michael Wrightd02c5b62014-02-10 15:10:22 -08003394// --- KeyboardInputMapperTest ---
3395
3396class KeyboardInputMapperTest : public InputMapperTest {
3397protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003398 const std::string UNIQUE_ID = "local:0";
3399
3400 void prepareDisplay(int32_t orientation);
3401
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003402 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003403 int32_t originalKeyCode, int32_t rotatedKeyCode,
3404 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003405};
3406
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003407/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3408 * orientation.
3409 */
3410void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003411 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3412 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003413}
3414
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003415void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003416 int32_t originalScanCode, int32_t originalKeyCode,
3417 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003418 NotifyKeyArgs args;
3419
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003420 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3422 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3423 ASSERT_EQ(originalScanCode, args.scanCode);
3424 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003425 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003426
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003427 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3429 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3430 ASSERT_EQ(originalScanCode, args.scanCode);
3431 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003432 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003433}
3434
Michael Wrightd02c5b62014-02-10 15:10:22 -08003435TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003436 KeyboardInputMapper& mapper =
3437 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3438 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003439
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003440 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003441}
3442
3443TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3444 const int32_t USAGE_A = 0x070004;
3445 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003446 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3447 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003448 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3449 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3450 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003451
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003452 KeyboardInputMapper& mapper =
3453 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3454 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003455 // Initial metastate is AMETA_NONE.
3456 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003457
3458 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003459 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003460 NotifyKeyArgs args;
3461 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, args.eventTime);
3465 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, 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 up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003474 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3476 ASSERT_EQ(DEVICE_ID, args.deviceId);
3477 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3478 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3479 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3480 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3481 ASSERT_EQ(KEY_HOME, args.scanCode);
3482 ASSERT_EQ(AMETA_NONE, args.metaState);
3483 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3484 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3485 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3486
3487 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003488 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3489 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3491 ASSERT_EQ(DEVICE_ID, args.deviceId);
3492 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3493 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3494 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3495 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3496 ASSERT_EQ(0, args.scanCode);
3497 ASSERT_EQ(AMETA_NONE, args.metaState);
3498 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3499 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3500 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3501
3502 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003503 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3504 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3506 ASSERT_EQ(DEVICE_ID, args.deviceId);
3507 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3508 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3509 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3510 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3511 ASSERT_EQ(0, args.scanCode);
3512 ASSERT_EQ(AMETA_NONE, args.metaState);
3513 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3514 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3515 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3516
3517 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3521 ASSERT_EQ(DEVICE_ID, args.deviceId);
3522 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3523 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3524 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3525 ASSERT_EQ(0, args.keyCode);
3526 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3527 ASSERT_EQ(AMETA_NONE, args.metaState);
3528 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3529 ASSERT_EQ(0U, args.policyFlags);
3530 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3531
3532 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003533 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3534 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3536 ASSERT_EQ(DEVICE_ID, args.deviceId);
3537 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3538 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3539 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3540 ASSERT_EQ(0, args.keyCode);
3541 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3542 ASSERT_EQ(AMETA_NONE, args.metaState);
3543 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3544 ASSERT_EQ(0U, args.policyFlags);
3545 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3546}
3547
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003548/**
3549 * Ensure that the readTime is set to the time when the EV_KEY is received.
3550 */
3551TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3552 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3553
3554 KeyboardInputMapper& mapper =
3555 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3556 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3557 NotifyKeyArgs args;
3558
3559 // Key down
3560 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3562 ASSERT_EQ(12, args.readTime);
3563
3564 // Key up
3565 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3567 ASSERT_EQ(15, args.readTime);
3568}
3569
Michael Wrightd02c5b62014-02-10 15:10:22 -08003570TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003571 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3572 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003573 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3574 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3575 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003576
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003577 KeyboardInputMapper& mapper =
3578 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3579 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003580
Arthur Hung95f68612022-04-07 14:08:22 +08003581 // Initial metastate is AMETA_NONE.
3582 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003583
3584 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003585 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003586 NotifyKeyArgs args;
3587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3588 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003589 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003590 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003591
3592 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003593 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3595 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003596 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003597
3598 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003599 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3601 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003602 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003603
3604 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003605 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3607 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003608 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003609 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003610}
3611
3612TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003613 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3614 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3615 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3616 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003617
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003618 KeyboardInputMapper& mapper =
3619 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3620 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003621
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003622 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003623 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3624 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3625 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3626 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3627 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3628 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3629 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3630 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3631}
3632
3633TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003634 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3635 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3636 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3637 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003638
Michael Wrightd02c5b62014-02-10 15:10:22 -08003639 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003640 KeyboardInputMapper& mapper =
3641 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3642 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003643
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003644 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003645 ASSERT_NO_FATAL_FAILURE(
3646 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3647 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3648 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3649 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3650 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3651 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3652 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003653
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003654 clearViewports();
3655 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003656 ASSERT_NO_FATAL_FAILURE(
3657 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3658 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3659 AKEYCODE_DPAD_UP, DISPLAY_ID));
3660 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3661 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3662 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3663 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003664
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003665 clearViewports();
3666 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003667 ASSERT_NO_FATAL_FAILURE(
3668 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3669 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3670 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3671 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3672 AKEYCODE_DPAD_UP, DISPLAY_ID));
3673 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3674 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003675
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003676 clearViewports();
3677 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003678 ASSERT_NO_FATAL_FAILURE(
3679 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3680 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3681 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3682 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3683 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3684 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3685 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003686
3687 // Special case: if orientation changes while key is down, we still emit the same keycode
3688 // in the key up as we did in the key down.
3689 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003690 clearViewports();
3691 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003692 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3694 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3695 ASSERT_EQ(KEY_UP, args.scanCode);
3696 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3697
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003698 clearViewports();
3699 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003700 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3702 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3703 ASSERT_EQ(KEY_UP, args.scanCode);
3704 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3705}
3706
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003707TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3708 // If the keyboard is not orientation aware,
3709 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003710 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003711
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003712 KeyboardInputMapper& mapper =
3713 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3714 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003715 NotifyKeyArgs args;
3716
3717 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003718 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003720 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3722 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3723
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003724 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003725 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003727 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3729 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3730}
3731
3732TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3733 // If the keyboard is orientation aware,
3734 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003735 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003736
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003737 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003738 KeyboardInputMapper& mapper =
3739 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3740 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003741 NotifyKeyArgs args;
3742
3743 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3744 // ^--- already checked by the previous test
3745
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003746 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003747 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003748 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003750 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3752 ASSERT_EQ(DISPLAY_ID, args.displayId);
3753
3754 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003755 clearViewports();
3756 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003757 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003758 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003760 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3762 ASSERT_EQ(newDisplayId, args.displayId);
3763}
3764
Michael Wrightd02c5b62014-02-10 15:10:22 -08003765TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003766 KeyboardInputMapper& mapper =
3767 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3768 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003769
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003770 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003771 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003772
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003773 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003774 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003775}
3776
Philip Junker4af3b3d2021-12-14 10:36:55 +01003777TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3778 KeyboardInputMapper& mapper =
3779 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3780 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3781
3782 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3783 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3784 << "If a mapping is available, the result is equal to the mapping";
3785
3786 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3787 << "If no mapping is available, the result is the key location";
3788}
3789
Michael Wrightd02c5b62014-02-10 15:10:22 -08003790TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003791 KeyboardInputMapper& mapper =
3792 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3793 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003794
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003795 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003796 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003797
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003798 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003799 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003800}
3801
3802TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003803 KeyboardInputMapper& mapper =
3804 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3805 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003806
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003807 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003808
Michael Wrightd02c5b62014-02-10 15:10:22 -08003809 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003810 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003811 ASSERT_TRUE(flags[0]);
3812 ASSERT_FALSE(flags[1]);
3813}
3814
3815TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003816 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3817 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3818 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3819 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3820 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3821 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003822
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003823 KeyboardInputMapper& mapper =
3824 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3825 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003826 // Initial metastate is AMETA_NONE.
3827 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003828
3829 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003830 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3831 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3832 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003833
3834 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003835 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3836 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003837 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3838 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3839 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003840 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003841
3842 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003843 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3844 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003845 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3846 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3847 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003848 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003849
3850 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003851 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3852 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003853 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3854 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3855 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003856 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003857
3858 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003859 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003861 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3862 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3863 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003864 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003865
3866 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003867 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3868 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003869 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3870 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3871 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003872 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003873
3874 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003875 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3876 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003877 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3878 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3879 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003880 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003881}
3882
Chris Yea52ade12020-08-27 16:49:20 -07003883TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3884 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3885 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3886 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3887 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3888
3889 KeyboardInputMapper& mapper =
3890 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3891 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3892
Chris Yea52ade12020-08-27 16:49:20 -07003893 // Meta state should be AMETA_NONE after reset
3894 mapper.reset(ARBITRARY_TIME);
3895 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3896 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3897 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3898 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3899
3900 NotifyKeyArgs args;
3901 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003902 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3904 ASSERT_EQ(AMETA_NONE, args.metaState);
3905 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3906 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3907 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3908
3909 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003910 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3912 ASSERT_EQ(AMETA_NONE, args.metaState);
3913 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3914 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3915 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3916}
3917
Arthur Hung2c9a3342019-07-23 14:18:59 +08003918TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3919 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003920 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3921 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3922 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3923 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003924
3925 // keyboard 2.
3926 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003927 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003928 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003929 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003930 std::shared_ptr<InputDevice> device2 =
3931 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003932 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003933
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003934 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3935 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3936 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3937 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003938
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003939 KeyboardInputMapper& mapper =
3940 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3941 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003942
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003943 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003944 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003945 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003946 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3947 device2->reset(ARBITRARY_TIME);
3948
3949 // Prepared displays and associated info.
3950 constexpr uint8_t hdmi1 = 0;
3951 constexpr uint8_t hdmi2 = 1;
3952 const std::string SECONDARY_UNIQUE_ID = "local:1";
3953
3954 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3955 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3956
3957 // No associated display viewport found, should disable the device.
3958 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3959 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3960 ASSERT_FALSE(device2->isEnabled());
3961
3962 // Prepare second display.
3963 constexpr int32_t newDisplayId = 2;
3964 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003965 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003966 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003967 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003968 // Default device will reconfigure above, need additional reconfiguration for another device.
3969 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3970 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3971
3972 // Device should be enabled after the associated display is found.
3973 ASSERT_TRUE(mDevice->isEnabled());
3974 ASSERT_TRUE(device2->isEnabled());
3975
3976 // Test pad key events
3977 ASSERT_NO_FATAL_FAILURE(
3978 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3979 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3980 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3981 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3982 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3983 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3984 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3985
3986 ASSERT_NO_FATAL_FAILURE(
3987 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3988 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3989 AKEYCODE_DPAD_RIGHT, newDisplayId));
3990 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3991 AKEYCODE_DPAD_DOWN, newDisplayId));
3992 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3993 AKEYCODE_DPAD_LEFT, newDisplayId));
3994}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003995
arthurhungc903df12020-08-11 15:08:42 +08003996TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3997 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3998 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3999 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4000 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4001 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4002 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4003
4004 KeyboardInputMapper& mapper =
4005 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4006 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004007 // Initial metastate is AMETA_NONE.
4008 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004009
4010 // Initialization should have turned all of the lights off.
4011 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4012 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4013 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4014
4015 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004016 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4017 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004018 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4019 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4020
4021 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004022 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4023 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004024 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4025 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
4026
4027 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004028 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4029 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004030 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4031 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
4032
4033 mFakeEventHub->removeDevice(EVENTHUB_ID);
4034 mReader->loopOnce();
4035
4036 // keyboard 2 should default toggle keys.
4037 const std::string USB2 = "USB2";
4038 const std::string DEVICE_NAME2 = "KEYBOARD2";
4039 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4040 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4041 std::shared_ptr<InputDevice> device2 =
4042 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004043 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08004044 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4045 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4046 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4047 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4048 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4049 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4050
arthurhung6fe95782020-10-05 22:41:16 +08004051 KeyboardInputMapper& mapper2 =
4052 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4053 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08004054 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4055 device2->reset(ARBITRARY_TIME);
4056
4057 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4058 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4059 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004060 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4061 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004062}
4063
Arthur Hungcb40a002021-08-03 14:31:01 +00004064TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4065 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4066 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4067 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4068
4069 // Suppose we have two mappers. (DPAD + KEYBOARD)
4070 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4071 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4072 KeyboardInputMapper& mapper =
4073 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4074 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004075 // Initial metastate is AMETA_NONE.
4076 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004077
4078 mReader->toggleCapsLockState(DEVICE_ID);
4079 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4080}
4081
Arthur Hungfb3cc112022-04-13 07:39:50 +00004082TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4083 // keyboard 1.
4084 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4085 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4086 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4087 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4088 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4089 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4090
4091 KeyboardInputMapper& mapper1 =
4092 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4093 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4094
4095 // keyboard 2.
4096 const std::string USB2 = "USB2";
4097 const std::string DEVICE_NAME2 = "KEYBOARD2";
4098 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4099 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4100 std::shared_ptr<InputDevice> device2 =
4101 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4102 ftl::Flags<InputDeviceClass>(0));
4103 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4104 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4105 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4106 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4107 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4108 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4109
4110 KeyboardInputMapper& mapper2 =
4111 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4112 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4113 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4114 device2->reset(ARBITRARY_TIME);
4115
Arthur Hung95f68612022-04-07 14:08:22 +08004116 // Initial metastate is AMETA_NONE.
4117 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4118 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4119
4120 // Toggle num lock on and off.
4121 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4122 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004123 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4124 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4125 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4126
4127 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4128 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4129 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4130 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4131 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4132
4133 // Toggle caps lock on and off.
4134 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4135 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4136 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4137 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4138 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4139
4140 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4141 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4142 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4143 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4144 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4145
4146 // Toggle scroll lock on and off.
4147 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4148 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4149 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4150 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4151 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4152
4153 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4154 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4155 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4156 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4157 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4158}
4159
Arthur Hung2141d542022-08-23 07:45:21 +00004160TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4161 const int32_t USAGE_A = 0x070004;
4162 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4163 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4164
4165 KeyboardInputMapper& mapper =
4166 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4167 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4168 // Key down by scan code.
4169 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4170 NotifyKeyArgs args;
4171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4172 ASSERT_EQ(DEVICE_ID, args.deviceId);
4173 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4174 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4175 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4176 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4177 ASSERT_EQ(KEY_HOME, args.scanCode);
4178 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4179
4180 // Disable device, it should synthesize cancellation events for down events.
4181 mFakePolicy->addDisabledDevice(DEVICE_ID);
4182 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
4183
4184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4185 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4186 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4187 ASSERT_EQ(KEY_HOME, args.scanCode);
4188 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4189}
4190
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004191// --- KeyboardInputMapperTest_ExternalDevice ---
4192
4193class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4194protected:
Chris Yea52ade12020-08-27 16:49:20 -07004195 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004196};
4197
4198TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004199 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4200 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004201
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004202 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4203 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4204 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4205 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004206
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004207 KeyboardInputMapper& mapper =
4208 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4209 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004210
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004211 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004212 NotifyKeyArgs args;
4213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4214 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4215
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004216 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4218 ASSERT_EQ(uint32_t(0), args.policyFlags);
4219
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004220 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4222 ASSERT_EQ(uint32_t(0), args.policyFlags);
4223
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004224 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4226 ASSERT_EQ(uint32_t(0), args.policyFlags);
4227
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004228 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4230 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4231
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004232 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4234 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4235}
4236
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004237TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004238 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004239
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004240 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4241 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4242 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004243
Powei Fengd041c5d2019-05-03 17:11:33 -07004244 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004245 KeyboardInputMapper& mapper =
4246 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4247 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004248
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004249 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004250 NotifyKeyArgs args;
4251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4252 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4253
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004254 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4256 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4257
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004258 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4260 ASSERT_EQ(uint32_t(0), args.policyFlags);
4261
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004262 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4264 ASSERT_EQ(uint32_t(0), args.policyFlags);
4265
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004266 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4268 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4269
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004270 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4272 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4273}
4274
Michael Wrightd02c5b62014-02-10 15:10:22 -08004275// --- CursorInputMapperTest ---
4276
4277class CursorInputMapperTest : public InputMapperTest {
4278protected:
4279 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4280
Michael Wright17db18e2020-06-26 20:51:44 +01004281 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004282
Chris Yea52ade12020-08-27 16:49:20 -07004283 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004284 InputMapperTest::SetUp();
4285
Michael Wright17db18e2020-06-26 20:51:44 +01004286 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004287 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004288 }
4289
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004290 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4291 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004292
4293 void prepareDisplay(int32_t orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004294 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4295 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4296 }
4297
4298 void prepareSecondaryDisplay() {
4299 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4300 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
4301 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004302 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004303
4304 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4305 float pressure) {
4306 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4307 0.0f, 0.0f, 0.0f, EPSILON));
4308 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004309};
4310
4311const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4312
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004313void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4314 int32_t originalY, int32_t rotatedX,
4315 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004316 NotifyMotionArgs args;
4317
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004318 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4319 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4320 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4322 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004323 ASSERT_NO_FATAL_FAILURE(
4324 assertCursorPointerCoords(args.pointerCoords[0],
4325 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4326 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004327}
4328
4329TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
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
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004333 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004334}
4335
4336TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004337 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004338 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004339
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004340 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004341}
4342
4343TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004344 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004345 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004346
4347 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004348 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004349
4350 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004351 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4352 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004353 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4354 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4355
4356 // When the bounds are set, then there should be a valid motion range.
4357 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4358
4359 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004360 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004361
4362 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4363 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4364 1, 800 - 1, 0.0f, 0.0f));
4365 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4366 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4367 2, 480 - 1, 0.0f, 0.0f));
4368 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4369 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4370 0.0f, 1.0f, 0.0f, 0.0f));
4371}
4372
4373TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004374 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004375 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004376
4377 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004378 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379
4380 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4381 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4382 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4383 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4384 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4385 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4386 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4387 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4388 0.0f, 1.0f, 0.0f, 0.0f));
4389}
4390
4391TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004393 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004394
arthurhungdcef2dc2020-08-11 14:47:50 +08004395 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004396
4397 NotifyMotionArgs args;
4398
4399 // Button press.
4400 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004401 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4402 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4404 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4405 ASSERT_EQ(DEVICE_ID, args.deviceId);
4406 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4407 ASSERT_EQ(uint32_t(0), args.policyFlags);
4408 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4409 ASSERT_EQ(0, args.flags);
4410 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4411 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4412 ASSERT_EQ(0, args.edgeFlags);
4413 ASSERT_EQ(uint32_t(1), args.pointerCount);
4414 ASSERT_EQ(0, args.pointerProperties[0].id);
4415 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004416 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004417 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4418 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4419 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4420
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4422 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4423 ASSERT_EQ(DEVICE_ID, args.deviceId);
4424 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4425 ASSERT_EQ(uint32_t(0), args.policyFlags);
4426 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4427 ASSERT_EQ(0, args.flags);
4428 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4429 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4430 ASSERT_EQ(0, args.edgeFlags);
4431 ASSERT_EQ(uint32_t(1), args.pointerCount);
4432 ASSERT_EQ(0, args.pointerProperties[0].id);
4433 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004434 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004435 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4436 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4437 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4438
Michael Wrightd02c5b62014-02-10 15:10:22 -08004439 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004440 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4441 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4443 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4444 ASSERT_EQ(DEVICE_ID, args.deviceId);
4445 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4446 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004447 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4448 ASSERT_EQ(0, args.flags);
4449 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4450 ASSERT_EQ(0, args.buttonState);
4451 ASSERT_EQ(0, args.edgeFlags);
4452 ASSERT_EQ(uint32_t(1), args.pointerCount);
4453 ASSERT_EQ(0, args.pointerProperties[0].id);
4454 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004455 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004456 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4457 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4458 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4459
4460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4461 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4462 ASSERT_EQ(DEVICE_ID, args.deviceId);
4463 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4464 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004465 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4466 ASSERT_EQ(0, args.flags);
4467 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4468 ASSERT_EQ(0, args.buttonState);
4469 ASSERT_EQ(0, args.edgeFlags);
4470 ASSERT_EQ(uint32_t(1), args.pointerCount);
4471 ASSERT_EQ(0, args.pointerProperties[0].id);
4472 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004473 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004474 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4475 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4476 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4477}
4478
4479TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004480 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004481 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004482
4483 NotifyMotionArgs args;
4484
4485 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004486 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4487 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4489 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004490 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4491 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4492 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004493
4494 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004495 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4496 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4498 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004499 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4500 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004501}
4502
4503TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004504 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004505 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004506
4507 NotifyMotionArgs args;
4508
4509 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004510 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4511 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4513 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004514 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004515
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4517 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004518 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004519
Michael Wrightd02c5b62014-02-10 15:10:22 -08004520 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004521 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4522 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004524 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004525 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004526
4527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004528 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004529 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004530}
4531
4532TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004533 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004534 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004535
4536 NotifyMotionArgs args;
4537
4538 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004539 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4540 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4541 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4542 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4544 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004545 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4546 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4547 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004548
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4550 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004551 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4552 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4553 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004554
Michael Wrightd02c5b62014-02-10 15:10:22 -08004555 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004556 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4557 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4558 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4560 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004561 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4562 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4563 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004564
4565 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004566 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4567 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004569 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004570 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004571
4572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004573 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004574 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004575}
4576
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004577TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004578 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004579 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004580 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4581 // need to be rotated.
4582 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004583 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004584
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004585 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004586 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4587 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4588 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4589 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4590 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4591 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4592 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4593 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4594}
4595
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004596TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004597 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004598 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004599 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4600 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004601 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004602
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004603 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004604 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004605 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4606 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4607 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4608 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4609 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4610 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4611 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4612 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4613
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004614 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004615 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004616 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4617 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4618 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4619 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4620 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4621 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4622 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4623 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004624
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004625 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004626 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004627 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4628 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4629 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4630 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4631 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4632 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4633 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4634 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4635
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004636 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004637 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004638 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4639 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4640 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4641 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4642 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4643 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4644 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4645 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004646}
4647
4648TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004649 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004650 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004651
4652 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4653 mFakePointerController->setPosition(100, 200);
4654 mFakePointerController->setButtonState(0);
4655
4656 NotifyMotionArgs motionArgs;
4657 NotifyKeyArgs keyArgs;
4658
4659 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004660 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4661 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4663 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4664 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4665 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004666 ASSERT_NO_FATAL_FAILURE(
4667 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004668
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4670 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4671 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4672 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004673 ASSERT_NO_FATAL_FAILURE(
4674 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004675
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004676 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4677 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004679 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004686 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004687 ASSERT_EQ(0, motionArgs.buttonState);
4688 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004689 ASSERT_NO_FATAL_FAILURE(
4690 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004691
4692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004693 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004694 ASSERT_EQ(0, motionArgs.buttonState);
4695 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004696 ASSERT_NO_FATAL_FAILURE(
4697 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004698
4699 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004700 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4701 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4702 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4704 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4705 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4706 motionArgs.buttonState);
4707 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4708 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004709 ASSERT_NO_FATAL_FAILURE(
4710 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004711
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4713 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4714 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4715 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4716 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004717 ASSERT_NO_FATAL_FAILURE(
4718 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004719
4720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4721 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4722 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4723 motionArgs.buttonState);
4724 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4725 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004726 ASSERT_NO_FATAL_FAILURE(
4727 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004728
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004729 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4730 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004732 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004733 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4734 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004735 ASSERT_NO_FATAL_FAILURE(
4736 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004737
4738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004739 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004740 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4741 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004742 ASSERT_NO_FATAL_FAILURE(
4743 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004744
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004745 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4746 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004748 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4749 ASSERT_EQ(0, motionArgs.buttonState);
4750 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004751 ASSERT_NO_FATAL_FAILURE(
4752 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004753 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4754 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004755
4756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004757 ASSERT_EQ(0, motionArgs.buttonState);
4758 ASSERT_EQ(0, mFakePointerController->getButtonState());
4759 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004760 ASSERT_NO_FATAL_FAILURE(
4761 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004762
Michael Wrightd02c5b62014-02-10 15:10:22 -08004763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4764 ASSERT_EQ(0, motionArgs.buttonState);
4765 ASSERT_EQ(0, mFakePointerController->getButtonState());
4766 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004767 ASSERT_NO_FATAL_FAILURE(
4768 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004769
4770 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004771 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4772 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4774 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4775 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004776
Michael Wrightd02c5b62014-02-10 15:10:22 -08004777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004778 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004779 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4780 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004781 ASSERT_NO_FATAL_FAILURE(
4782 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004783
4784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4785 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4786 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4787 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004788 ASSERT_NO_FATAL_FAILURE(
4789 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004790
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004791 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4792 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004794 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004795 ASSERT_EQ(0, motionArgs.buttonState);
4796 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004797 ASSERT_NO_FATAL_FAILURE(
4798 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004799
4800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004801 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004802 ASSERT_EQ(0, motionArgs.buttonState);
4803 ASSERT_EQ(0, mFakePointerController->getButtonState());
4804
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004805 ASSERT_NO_FATAL_FAILURE(
4806 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4808 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4809 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4810
4811 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004812 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4813 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4815 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4816 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004817
Michael Wrightd02c5b62014-02-10 15:10:22 -08004818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004819 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004820 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4821 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004822 ASSERT_NO_FATAL_FAILURE(
4823 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004824
4825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4826 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4827 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4828 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004829 ASSERT_NO_FATAL_FAILURE(
4830 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4833 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004835 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004836 ASSERT_EQ(0, motionArgs.buttonState);
4837 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004838 ASSERT_NO_FATAL_FAILURE(
4839 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004840
4841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4842 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4843 ASSERT_EQ(0, motionArgs.buttonState);
4844 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004845 ASSERT_NO_FATAL_FAILURE(
4846 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004847
Michael Wrightd02c5b62014-02-10 15:10:22 -08004848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4849 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4850 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4851
4852 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004853 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4854 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4856 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4857 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004858
Michael Wrightd02c5b62014-02-10 15:10:22 -08004859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004860 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004861 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4862 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004863 ASSERT_NO_FATAL_FAILURE(
4864 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004865
4866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4867 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4868 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4869 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004870 ASSERT_NO_FATAL_FAILURE(
4871 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004872
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004873 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4874 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004876 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004877 ASSERT_EQ(0, motionArgs.buttonState);
4878 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004879 ASSERT_NO_FATAL_FAILURE(
4880 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004881
4882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4883 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4884 ASSERT_EQ(0, motionArgs.buttonState);
4885 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004886 ASSERT_NO_FATAL_FAILURE(
4887 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004888
Michael Wrightd02c5b62014-02-10 15:10:22 -08004889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4890 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4891 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4892
4893 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004894 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4895 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4897 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4898 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004899
Michael Wrightd02c5b62014-02-10 15:10:22 -08004900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004901 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004902 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4903 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004904 ASSERT_NO_FATAL_FAILURE(
4905 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004906
4907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4908 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4909 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4910 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004911 ASSERT_NO_FATAL_FAILURE(
4912 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004913
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004914 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4915 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004917 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004918 ASSERT_EQ(0, motionArgs.buttonState);
4919 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004920 ASSERT_NO_FATAL_FAILURE(
4921 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004922
4923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4924 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4925 ASSERT_EQ(0, motionArgs.buttonState);
4926 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004927 ASSERT_NO_FATAL_FAILURE(
4928 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004929
Michael Wrightd02c5b62014-02-10 15:10:22 -08004930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4931 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4932 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4933}
4934
4935TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004936 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004937 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004938
4939 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4940 mFakePointerController->setPosition(100, 200);
4941 mFakePointerController->setButtonState(0);
4942
4943 NotifyMotionArgs args;
4944
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004945 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4946 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4947 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004949 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4950 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4951 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4952 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 +01004953 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004954}
4955
4956TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004957 addConfigurationProperty("cursor.mode", "pointer");
4958 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004959 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004960
4961 NotifyDeviceResetArgs resetArgs;
4962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4963 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4964 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4965
4966 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4967 mFakePointerController->setPosition(100, 200);
4968 mFakePointerController->setButtonState(0);
4969
4970 NotifyMotionArgs args;
4971
4972 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004973 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4974 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4975 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4977 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4978 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4979 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4980 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 +01004981 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004982
4983 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004984 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4985 process(mapper, ARBITRARY_TIME, 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_DOWN, args.action);
4989 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4990 0.0f, 0.0f, 1.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_BUTTON_PRESS, args.action);
4994 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4995 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4996
4997 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004998 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4999 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5001 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5002 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
5003 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5004 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5006 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5007 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
5008 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5009 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5010
5011 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005012 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
5013 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
5014 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5016 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5017 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5018 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5019 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 +01005020 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005021
5022 // Disable pointer capture and check that the device generation got bumped
5023 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08005024 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005025 mFakePolicy->setPointerCapture(false);
5026 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08005027 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005028
5029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005030 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5031
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005032 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5033 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5034 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5036 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005037 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5038 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5039 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 +01005040 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005041}
5042
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00005043/**
5044 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
5045 * pointer acceleration or speed processing should not be applied.
5046 */
5047TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
5048 addConfigurationProperty("cursor.mode", "pointer");
5049 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
5050 100.f /*high threshold*/, 10.f /*acceleration*/);
5051 mFakePolicy->setVelocityControlParams(testParams);
5052 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5053
5054 NotifyDeviceResetArgs resetArgs;
5055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5056 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5057 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5058
5059 NotifyMotionArgs args;
5060
5061 // Move and verify scale is applied.
5062 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5063 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5064 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5066 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5067 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5068 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5069 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5070 ASSERT_GT(relX, 10);
5071 ASSERT_GT(relY, 20);
5072
5073 // Enable Pointer Capture
5074 mFakePolicy->setPointerCapture(true);
5075 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5076 NotifyPointerCaptureChangedArgs captureArgs;
5077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5078 ASSERT_TRUE(captureArgs.request.enable);
5079
5080 // Move and verify scale is not applied.
5081 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5082 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5083 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5085 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5086 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5087 ASSERT_EQ(10, args.pointerCoords[0].getX());
5088 ASSERT_EQ(20, args.pointerCoords[0].getY());
5089}
5090
Prabir Pradhan208360b2022-06-24 18:37:04 +00005091TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5092 addConfigurationProperty("cursor.mode", "pointer");
5093 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5094
5095 NotifyDeviceResetArgs resetArgs;
5096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5097 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5098 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5099
5100 // Ensure the display is rotated.
5101 prepareDisplay(DISPLAY_ORIENTATION_90);
5102
5103 NotifyMotionArgs args;
5104
5105 // Verify that the coordinates are rotated.
5106 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5107 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5108 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5110 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5111 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5112 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5113 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5114
5115 // Enable Pointer Capture.
5116 mFakePolicy->setPointerCapture(true);
5117 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5118 NotifyPointerCaptureChangedArgs captureArgs;
5119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5120 ASSERT_TRUE(captureArgs.request.enable);
5121
5122 // Move and verify rotation is not applied.
5123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5124 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5125 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5127 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5129 ASSERT_EQ(10, args.pointerCoords[0].getX());
5130 ASSERT_EQ(20, args.pointerCoords[0].getY());
5131}
5132
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005133TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005134 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005135
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005136 // Set up the default display.
5137 prepareDisplay(DISPLAY_ORIENTATION_90);
5138
5139 // Set up the secondary display as the display on which the pointer should be shown.
5140 // The InputDevice is not associated with any display.
5141 prepareSecondaryDisplay();
5142 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08005143 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5144
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005145 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005146 mFakePointerController->setPosition(100, 200);
5147 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005148
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005149 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005150 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5151 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5152 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005154 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5155 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5156 WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01005157 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005158}
5159
5160TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
5161 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5162
5163 // Set up the default display.
5164 prepareDisplay(DISPLAY_ORIENTATION_90);
5165
5166 // Set up the secondary display as the display on which the pointer should be shown,
5167 // and associate the InputDevice with the secondary display.
5168 prepareSecondaryDisplay();
5169 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5170 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5171 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5172
5173 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5174 mFakePointerController->setPosition(100, 200);
5175 mFakePointerController->setButtonState(0);
5176
5177 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5178 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5179 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005181 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5182 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5183 WithCoords(110.0f, 220.0f))));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005184 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
5185}
5186
5187TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
5188 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5189
5190 // Set up the default display as the display on which the pointer should be shown.
5191 prepareDisplay(DISPLAY_ORIENTATION_90);
5192 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5193
5194 // Associate the InputDevice with the secondary display.
5195 prepareSecondaryDisplay();
5196 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5197 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5198
5199 // The mapper should not generate any events because it is associated with a display that is
5200 // different from the pointer display.
5201 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5202 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5203 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005205}
5206
Michael Wrightd02c5b62014-02-10 15:10:22 -08005207// --- TouchInputMapperTest ---
5208
5209class TouchInputMapperTest : public InputMapperTest {
5210protected:
5211 static const int32_t RAW_X_MIN;
5212 static const int32_t RAW_X_MAX;
5213 static const int32_t RAW_Y_MIN;
5214 static const int32_t RAW_Y_MAX;
5215 static const int32_t RAW_TOUCH_MIN;
5216 static const int32_t RAW_TOUCH_MAX;
5217 static const int32_t RAW_TOOL_MIN;
5218 static const int32_t RAW_TOOL_MAX;
5219 static const int32_t RAW_PRESSURE_MIN;
5220 static const int32_t RAW_PRESSURE_MAX;
5221 static const int32_t RAW_ORIENTATION_MIN;
5222 static const int32_t RAW_ORIENTATION_MAX;
5223 static const int32_t RAW_DISTANCE_MIN;
5224 static const int32_t RAW_DISTANCE_MAX;
5225 static const int32_t RAW_TILT_MIN;
5226 static const int32_t RAW_TILT_MAX;
5227 static const int32_t RAW_ID_MIN;
5228 static const int32_t RAW_ID_MAX;
5229 static const int32_t RAW_SLOT_MIN;
5230 static const int32_t RAW_SLOT_MAX;
5231 static const float X_PRECISION;
5232 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005233 static const float X_PRECISION_VIRTUAL;
5234 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005235
5236 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005237 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005238
5239 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5240
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005241 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005242 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005243
Michael Wrightd02c5b62014-02-10 15:10:22 -08005244 enum Axes {
5245 POSITION = 1 << 0,
5246 TOUCH = 1 << 1,
5247 TOOL = 1 << 2,
5248 PRESSURE = 1 << 3,
5249 ORIENTATION = 1 << 4,
5250 MINOR = 1 << 5,
5251 ID = 1 << 6,
5252 DISTANCE = 1 << 7,
5253 TILT = 1 << 8,
5254 SLOT = 1 << 9,
5255 TOOL_TYPE = 1 << 10,
5256 };
5257
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005258 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5259 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005260 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005261 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005262 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005263 int32_t toRawX(float displayX);
5264 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005265 int32_t toRotatedRawX(float displayX);
5266 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005267 float toCookedX(float rawX, float rawY);
5268 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005269 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005270 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005271 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005272 float toDisplayY(int32_t rawY, int32_t displayHeight);
5273
Michael Wrightd02c5b62014-02-10 15:10:22 -08005274};
5275
5276const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5277const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5278const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5279const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5280const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5281const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5282const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5283const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005284const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5285const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005286const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5287const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5288const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5289const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5290const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5291const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5292const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5293const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5294const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5295const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5296const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5297const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005298const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5299 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5300const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5301 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005302const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5303 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005304
5305const float TouchInputMapperTest::GEOMETRIC_SCALE =
5306 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5307 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5308
5309const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5310 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5311 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5312};
5313
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005314void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005315 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5316 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005317}
5318
5319void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5320 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5321 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005322}
5323
Santos Cordonfa5cf462017-04-05 10:37:00 -07005324void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005325 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5326 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5327 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005328}
5329
Michael Wrightd02c5b62014-02-10 15:10:22 -08005330void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005331 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5332 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5333 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5334 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005335}
5336
Jason Gerecke489fda82012-09-07 17:19:40 -07005337void TouchInputMapperTest::prepareLocationCalibration() {
5338 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5339}
5340
Michael Wrightd02c5b62014-02-10 15:10:22 -08005341int32_t TouchInputMapperTest::toRawX(float displayX) {
5342 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5343}
5344
5345int32_t TouchInputMapperTest::toRawY(float displayY) {
5346 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5347}
5348
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005349int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5350 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5351}
5352
5353int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5354 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5355}
5356
Jason Gerecke489fda82012-09-07 17:19:40 -07005357float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5358 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5359 return rawX;
5360}
5361
5362float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5363 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5364 return rawY;
5365}
5366
Michael Wrightd02c5b62014-02-10 15:10:22 -08005367float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005368 return toDisplayX(rawX, DISPLAY_WIDTH);
5369}
5370
5371float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5372 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005373}
5374
5375float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005376 return toDisplayY(rawY, DISPLAY_HEIGHT);
5377}
5378
5379float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5380 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005381}
5382
5383
5384// --- SingleTouchInputMapperTest ---
5385
5386class SingleTouchInputMapperTest : public TouchInputMapperTest {
5387protected:
5388 void prepareButtons();
5389 void prepareAxes(int axes);
5390
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005391 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5392 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5393 void processUp(SingleTouchInputMapper& mappery);
5394 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5395 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5396 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5397 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5398 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5399 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005400};
5401
5402void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005403 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005404}
5405
5406void SingleTouchInputMapperTest::prepareAxes(int axes) {
5407 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005408 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5409 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005410 }
5411 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005412 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5413 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005414 }
5415 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005416 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5417 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005418 }
5419 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005420 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5421 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005422 }
5423 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005424 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5425 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005426 }
5427}
5428
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005429void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005430 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5431 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5432 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005433}
5434
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005435void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005436 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5437 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005438}
5439
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005440void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005441 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005442}
5443
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005444void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005445 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005446}
5447
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005448void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5449 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005451}
5452
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005453void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005454 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005455}
5456
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005457void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5458 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005459 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5460 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005461}
5462
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005463void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5464 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005465 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005466}
5467
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005468void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005469 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005470}
5471
Michael Wrightd02c5b62014-02-10 15:10:22 -08005472TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005473 prepareButtons();
5474 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005475 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005476
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005477 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005478}
5479
Michael Wrightd02c5b62014-02-10 15:10:22 -08005480TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005481 prepareButtons();
5482 prepareAxes(POSITION);
5483 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005484 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005485
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005486 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005487}
5488
5489TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005490 addConfigurationProperty("touch.deviceType", "touchScreen");
5491 prepareDisplay(DISPLAY_ORIENTATION_0);
5492 prepareButtons();
5493 prepareAxes(POSITION);
5494 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005495 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005496
5497 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005498 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005499
5500 // Virtual key is down.
5501 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5502 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5503 processDown(mapper, x, y);
5504 processSync(mapper);
5505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5506
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005507 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005508
5509 // Virtual key is up.
5510 processUp(mapper);
5511 processSync(mapper);
5512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5513
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005514 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005515}
5516
5517TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005518 addConfigurationProperty("touch.deviceType", "touchScreen");
5519 prepareDisplay(DISPLAY_ORIENTATION_0);
5520 prepareButtons();
5521 prepareAxes(POSITION);
5522 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005523 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005524
5525 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005526 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005527
5528 // Virtual key is down.
5529 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5530 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5531 processDown(mapper, x, y);
5532 processSync(mapper);
5533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5534
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005535 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005536
5537 // Virtual key is up.
5538 processUp(mapper);
5539 processSync(mapper);
5540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5541
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005542 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005543}
5544
5545TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005546 addConfigurationProperty("touch.deviceType", "touchScreen");
5547 prepareDisplay(DISPLAY_ORIENTATION_0);
5548 prepareButtons();
5549 prepareAxes(POSITION);
5550 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005551 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005552
Michael Wrightd02c5b62014-02-10 15:10:22 -08005553 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005554 ASSERT_TRUE(
5555 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005556 ASSERT_TRUE(flags[0]);
5557 ASSERT_FALSE(flags[1]);
5558}
5559
5560TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005561 addConfigurationProperty("touch.deviceType", "touchScreen");
5562 prepareDisplay(DISPLAY_ORIENTATION_0);
5563 prepareButtons();
5564 prepareAxes(POSITION);
5565 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005566 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005567
arthurhungdcef2dc2020-08-11 14:47:50 +08005568 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005569
5570 NotifyKeyArgs args;
5571
5572 // Press virtual key.
5573 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5574 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5575 processDown(mapper, x, y);
5576 processSync(mapper);
5577
5578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5579 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5580 ASSERT_EQ(DEVICE_ID, args.deviceId);
5581 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5582 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5583 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5584 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5585 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5586 ASSERT_EQ(KEY_HOME, args.scanCode);
5587 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5588 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5589
5590 // Release virtual key.
5591 processUp(mapper);
5592 processSync(mapper);
5593
5594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5595 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5596 ASSERT_EQ(DEVICE_ID, args.deviceId);
5597 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5598 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5599 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5600 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5601 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5602 ASSERT_EQ(KEY_HOME, args.scanCode);
5603 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5604 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5605
5606 // Should not have sent any motions.
5607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5608}
5609
5610TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005611 addConfigurationProperty("touch.deviceType", "touchScreen");
5612 prepareDisplay(DISPLAY_ORIENTATION_0);
5613 prepareButtons();
5614 prepareAxes(POSITION);
5615 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005616 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005617
arthurhungdcef2dc2020-08-11 14:47:50 +08005618 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005619
5620 NotifyKeyArgs keyArgs;
5621
5622 // Press virtual key.
5623 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5624 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5625 processDown(mapper, x, y);
5626 processSync(mapper);
5627
5628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5629 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5630 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5631 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5632 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5633 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5634 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5635 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5636 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5637 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5638 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5639
5640 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5641 // into the display area.
5642 y -= 100;
5643 processMove(mapper, x, y);
5644 processSync(mapper);
5645
5646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5647 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5648 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5649 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5650 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5651 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5652 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5653 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5654 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5655 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5656 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5657 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5658
5659 NotifyMotionArgs motionArgs;
5660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5661 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5662 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5663 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5664 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5665 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5666 ASSERT_EQ(0, motionArgs.flags);
5667 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5668 ASSERT_EQ(0, motionArgs.buttonState);
5669 ASSERT_EQ(0, motionArgs.edgeFlags);
5670 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5671 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5672 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5673 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5674 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5675 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5676 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5677 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5678
5679 // Keep moving out of bounds. Should generate a pointer move.
5680 y -= 50;
5681 processMove(mapper, x, y);
5682 processSync(mapper);
5683
5684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5685 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5686 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5687 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5688 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5689 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5690 ASSERT_EQ(0, motionArgs.flags);
5691 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5692 ASSERT_EQ(0, motionArgs.buttonState);
5693 ASSERT_EQ(0, motionArgs.edgeFlags);
5694 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5695 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5696 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5697 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5698 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5699 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5700 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5701 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5702
5703 // Release out of bounds. Should generate a pointer up.
5704 processUp(mapper);
5705 processSync(mapper);
5706
5707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5708 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5709 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5710 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5711 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5712 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5713 ASSERT_EQ(0, motionArgs.flags);
5714 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5715 ASSERT_EQ(0, motionArgs.buttonState);
5716 ASSERT_EQ(0, motionArgs.edgeFlags);
5717 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5718 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5719 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5720 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5721 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5722 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5723 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5724 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5725
5726 // Should not have sent any more keys or motions.
5727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5729}
5730
5731TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005732 addConfigurationProperty("touch.deviceType", "touchScreen");
5733 prepareDisplay(DISPLAY_ORIENTATION_0);
5734 prepareButtons();
5735 prepareAxes(POSITION);
5736 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005737 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005738
arthurhungdcef2dc2020-08-11 14:47:50 +08005739 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005740
5741 NotifyMotionArgs motionArgs;
5742
5743 // Initially go down out of bounds.
5744 int32_t x = -10;
5745 int32_t y = -10;
5746 processDown(mapper, x, y);
5747 processSync(mapper);
5748
5749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5750
5751 // Move into the display area. Should generate a pointer down.
5752 x = 50;
5753 y = 75;
5754 processMove(mapper, x, y);
5755 processSync(mapper);
5756
5757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5758 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5759 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5760 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5761 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5762 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5763 ASSERT_EQ(0, motionArgs.flags);
5764 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5765 ASSERT_EQ(0, motionArgs.buttonState);
5766 ASSERT_EQ(0, motionArgs.edgeFlags);
5767 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5768 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5769 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5770 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5771 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5772 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5773 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5774 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5775
5776 // Release. Should generate a pointer up.
5777 processUp(mapper);
5778 processSync(mapper);
5779
5780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5781 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5782 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5783 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5784 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5785 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5786 ASSERT_EQ(0, motionArgs.flags);
5787 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5788 ASSERT_EQ(0, motionArgs.buttonState);
5789 ASSERT_EQ(0, motionArgs.edgeFlags);
5790 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5791 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5792 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5793 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5794 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5795 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5796 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5797 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5798
5799 // Should not have sent any more keys or motions.
5800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5802}
5803
Santos Cordonfa5cf462017-04-05 10:37:00 -07005804TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005805 addConfigurationProperty("touch.deviceType", "touchScreen");
5806 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5807
5808 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5809 prepareButtons();
5810 prepareAxes(POSITION);
5811 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005812 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005813
arthurhungdcef2dc2020-08-11 14:47:50 +08005814 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005815
5816 NotifyMotionArgs motionArgs;
5817
5818 // Down.
5819 int32_t x = 100;
5820 int32_t y = 125;
5821 processDown(mapper, x, y);
5822 processSync(mapper);
5823
5824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5825 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5826 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5827 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5828 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5829 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5830 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5831 ASSERT_EQ(0, motionArgs.flags);
5832 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5833 ASSERT_EQ(0, motionArgs.buttonState);
5834 ASSERT_EQ(0, motionArgs.edgeFlags);
5835 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5836 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5837 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5838 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5839 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5840 1, 0, 0, 0, 0, 0, 0, 0));
5841 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5842 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5843 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5844
5845 // Move.
5846 x += 50;
5847 y += 75;
5848 processMove(mapper, x, y);
5849 processSync(mapper);
5850
5851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5852 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5853 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5854 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5855 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5856 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5857 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5858 ASSERT_EQ(0, motionArgs.flags);
5859 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5860 ASSERT_EQ(0, motionArgs.buttonState);
5861 ASSERT_EQ(0, motionArgs.edgeFlags);
5862 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5863 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5864 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5865 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5866 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5867 1, 0, 0, 0, 0, 0, 0, 0));
5868 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5869 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5870 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5871
5872 // Up.
5873 processUp(mapper);
5874 processSync(mapper);
5875
5876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5877 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5878 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5879 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5880 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5881 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5882 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5883 ASSERT_EQ(0, motionArgs.flags);
5884 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5885 ASSERT_EQ(0, motionArgs.buttonState);
5886 ASSERT_EQ(0, motionArgs.edgeFlags);
5887 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5888 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5889 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5890 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5891 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5892 1, 0, 0, 0, 0, 0, 0, 0));
5893 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5894 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5895 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5896
5897 // Should not have sent any more keys or motions.
5898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5900}
5901
Michael Wrightd02c5b62014-02-10 15:10:22 -08005902TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005903 addConfigurationProperty("touch.deviceType", "touchScreen");
5904 prepareDisplay(DISPLAY_ORIENTATION_0);
5905 prepareButtons();
5906 prepareAxes(POSITION);
5907 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005908 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005909
arthurhungdcef2dc2020-08-11 14:47:50 +08005910 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005911
5912 NotifyMotionArgs motionArgs;
5913
5914 // Down.
5915 int32_t x = 100;
5916 int32_t y = 125;
5917 processDown(mapper, x, y);
5918 processSync(mapper);
5919
5920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5921 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5922 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5923 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5924 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5925 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5926 ASSERT_EQ(0, motionArgs.flags);
5927 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5928 ASSERT_EQ(0, motionArgs.buttonState);
5929 ASSERT_EQ(0, motionArgs.edgeFlags);
5930 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5931 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5932 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5933 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5934 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5935 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5936 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5937 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5938
5939 // Move.
5940 x += 50;
5941 y += 75;
5942 processMove(mapper, x, y);
5943 processSync(mapper);
5944
5945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5946 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5947 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5948 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5949 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5950 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5951 ASSERT_EQ(0, motionArgs.flags);
5952 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5953 ASSERT_EQ(0, motionArgs.buttonState);
5954 ASSERT_EQ(0, motionArgs.edgeFlags);
5955 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5956 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5957 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5958 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5959 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5960 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5961 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5962 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5963
5964 // Up.
5965 processUp(mapper);
5966 processSync(mapper);
5967
5968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5969 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5970 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5971 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5972 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5973 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5974 ASSERT_EQ(0, motionArgs.flags);
5975 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5976 ASSERT_EQ(0, motionArgs.buttonState);
5977 ASSERT_EQ(0, motionArgs.edgeFlags);
5978 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5979 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5980 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5981 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5982 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5983 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5984 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5985 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5986
5987 // Should not have sent any more keys or motions.
5988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5990}
5991
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005992TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005993 addConfigurationProperty("touch.deviceType", "touchScreen");
5994 prepareButtons();
5995 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005996 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5997 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005998 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005999
6000 NotifyMotionArgs args;
6001
6002 // Rotation 90.
6003 prepareDisplay(DISPLAY_ORIENTATION_90);
6004 processDown(mapper, toRawX(50), toRawY(75));
6005 processSync(mapper);
6006
6007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6008 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6009 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6010
6011 processUp(mapper);
6012 processSync(mapper);
6013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6014}
6015
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006016TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006017 addConfigurationProperty("touch.deviceType", "touchScreen");
6018 prepareButtons();
6019 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006020 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6021 // orientation-aware are affected by display rotation.
6022 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006023 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006024
6025 NotifyMotionArgs args;
6026
6027 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006028 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006029 prepareDisplay(DISPLAY_ORIENTATION_0);
6030 processDown(mapper, toRawX(50), toRawY(75));
6031 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 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006042 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006043 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006044 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006045 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 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006056 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006057 prepareDisplay(DISPLAY_ORIENTATION_180);
6058 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6059 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 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006070 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006071 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006072 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006073 processSync(mapper);
6074
6075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6076 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6077 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6078
6079 processUp(mapper);
6080 processSync(mapper);
6081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6082}
6083
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006084TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6085 addConfigurationProperty("touch.deviceType", "touchScreen");
6086 prepareButtons();
6087 prepareAxes(POSITION);
6088 addConfigurationProperty("touch.orientationAware", "1");
6089 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6090 clearViewports();
6091 prepareDisplay(DISPLAY_ORIENTATION_0);
6092 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6093 NotifyMotionArgs args;
6094
6095 // Orientation 0.
6096 processDown(mapper, toRawX(50), toRawY(75));
6097 processSync(mapper);
6098
6099 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6100 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6101 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6102
6103 processUp(mapper);
6104 processSync(mapper);
6105 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6106}
6107
6108TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6109 addConfigurationProperty("touch.deviceType", "touchScreen");
6110 prepareButtons();
6111 prepareAxes(POSITION);
6112 addConfigurationProperty("touch.orientationAware", "1");
6113 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6114 clearViewports();
6115 prepareDisplay(DISPLAY_ORIENTATION_0);
6116 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6117 NotifyMotionArgs args;
6118
6119 // Orientation 90.
6120 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6121 processSync(mapper);
6122
6123 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6124 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6125 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6126
6127 processUp(mapper);
6128 processSync(mapper);
6129 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6130}
6131
6132TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6133 addConfigurationProperty("touch.deviceType", "touchScreen");
6134 prepareButtons();
6135 prepareAxes(POSITION);
6136 addConfigurationProperty("touch.orientationAware", "1");
6137 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6138 clearViewports();
6139 prepareDisplay(DISPLAY_ORIENTATION_0);
6140 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6141 NotifyMotionArgs args;
6142
6143 // Orientation 180.
6144 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6145 processSync(mapper);
6146
6147 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6148 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6149 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6150
6151 processUp(mapper);
6152 processSync(mapper);
6153 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6154}
6155
6156TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6157 addConfigurationProperty("touch.deviceType", "touchScreen");
6158 prepareButtons();
6159 prepareAxes(POSITION);
6160 addConfigurationProperty("touch.orientationAware", "1");
6161 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6162 clearViewports();
6163 prepareDisplay(DISPLAY_ORIENTATION_0);
6164 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6165 NotifyMotionArgs args;
6166
6167 // Orientation 270.
6168 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6169 processSync(mapper);
6170
6171 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6172 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6173 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6174
6175 processUp(mapper);
6176 processSync(mapper);
6177 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6178}
6179
6180TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6181 addConfigurationProperty("touch.deviceType", "touchScreen");
6182 prepareButtons();
6183 prepareAxes(POSITION);
6184 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6185 // orientation-aware are affected by display rotation.
6186 addConfigurationProperty("touch.orientationAware", "0");
6187 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6188 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6189
6190 NotifyMotionArgs args;
6191
6192 // Orientation 90, Rotation 0.
6193 clearViewports();
6194 prepareDisplay(DISPLAY_ORIENTATION_0);
6195 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
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 90.
6207 clearViewports();
6208 prepareDisplay(DISPLAY_ORIENTATION_90);
6209 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
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 180.
6221 clearViewports();
6222 prepareDisplay(DISPLAY_ORIENTATION_180);
6223 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6224 processSync(mapper);
6225
6226 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6227 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6228 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6229
6230 processUp(mapper);
6231 processSync(mapper);
6232 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6233
6234 // Orientation 90, Rotation 270.
6235 clearViewports();
6236 prepareDisplay(DISPLAY_ORIENTATION_270);
6237 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6238 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6239 processSync(mapper);
6240
6241 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6242 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6243 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6244
6245 processUp(mapper);
6246 processSync(mapper);
6247 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6248}
6249
Michael Wrightd02c5b62014-02-10 15:10:22 -08006250TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006251 addConfigurationProperty("touch.deviceType", "touchScreen");
6252 prepareDisplay(DISPLAY_ORIENTATION_0);
6253 prepareButtons();
6254 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006255 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006256
6257 // These calculations are based on the input device calibration documentation.
6258 int32_t rawX = 100;
6259 int32_t rawY = 200;
6260 int32_t rawPressure = 10;
6261 int32_t rawToolMajor = 12;
6262 int32_t rawDistance = 2;
6263 int32_t rawTiltX = 30;
6264 int32_t rawTiltY = 110;
6265
6266 float x = toDisplayX(rawX);
6267 float y = toDisplayY(rawY);
6268 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6269 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6270 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6271 float distance = float(rawDistance);
6272
6273 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6274 float tiltScale = M_PI / 180;
6275 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6276 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6277 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6278 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6279
6280 processDown(mapper, rawX, rawY);
6281 processPressure(mapper, rawPressure);
6282 processToolMajor(mapper, rawToolMajor);
6283 processDistance(mapper, rawDistance);
6284 processTilt(mapper, rawTiltX, rawTiltY);
6285 processSync(mapper);
6286
6287 NotifyMotionArgs args;
6288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6289 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6290 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6291 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6292}
6293
Jason Gerecke489fda82012-09-07 17:19:40 -07006294TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006295 addConfigurationProperty("touch.deviceType", "touchScreen");
6296 prepareDisplay(DISPLAY_ORIENTATION_0);
6297 prepareLocationCalibration();
6298 prepareButtons();
6299 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006300 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006301
6302 int32_t rawX = 100;
6303 int32_t rawY = 200;
6304
6305 float x = toDisplayX(toCookedX(rawX, rawY));
6306 float y = toDisplayY(toCookedY(rawX, rawY));
6307
6308 processDown(mapper, rawX, rawY);
6309 processSync(mapper);
6310
6311 NotifyMotionArgs args;
6312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6313 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6314 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6315}
6316
Michael Wrightd02c5b62014-02-10 15:10:22 -08006317TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006318 addConfigurationProperty("touch.deviceType", "touchScreen");
6319 prepareDisplay(DISPLAY_ORIENTATION_0);
6320 prepareButtons();
6321 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006322 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006323
6324 NotifyMotionArgs motionArgs;
6325 NotifyKeyArgs keyArgs;
6326
6327 processDown(mapper, 100, 200);
6328 processSync(mapper);
6329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6330 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6331 ASSERT_EQ(0, motionArgs.buttonState);
6332
6333 // press BTN_LEFT, release BTN_LEFT
6334 processKey(mapper, BTN_LEFT, 1);
6335 processSync(mapper);
6336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6337 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6338 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6339
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6341 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6342 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6343
Michael Wrightd02c5b62014-02-10 15:10:22 -08006344 processKey(mapper, BTN_LEFT, 0);
6345 processSync(mapper);
6346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006347 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006348 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006349
6350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006351 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006352 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006353
6354 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6355 processKey(mapper, BTN_RIGHT, 1);
6356 processKey(mapper, BTN_MIDDLE, 1);
6357 processSync(mapper);
6358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6359 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6360 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6361 motionArgs.buttonState);
6362
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6364 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6365 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6366
6367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6368 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6369 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6370 motionArgs.buttonState);
6371
Michael Wrightd02c5b62014-02-10 15:10:22 -08006372 processKey(mapper, BTN_RIGHT, 0);
6373 processSync(mapper);
6374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006375 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006376 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006377
6378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006379 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006380 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006381
6382 processKey(mapper, BTN_MIDDLE, 0);
6383 processSync(mapper);
6384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006385 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006386 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006387
6388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006390 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006391
6392 // press BTN_BACK, release BTN_BACK
6393 processKey(mapper, BTN_BACK, 1);
6394 processSync(mapper);
6395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6396 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6397 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006398
Michael Wrightd02c5b62014-02-10 15:10:22 -08006399 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(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6402
6403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6404 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6405 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006406
6407 processKey(mapper, BTN_BACK, 0);
6408 processSync(mapper);
6409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006410 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006411 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006412
6413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006414 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006415 ASSERT_EQ(0, motionArgs.buttonState);
6416
Michael Wrightd02c5b62014-02-10 15:10:22 -08006417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6418 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6419 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6420
6421 // press BTN_SIDE, release BTN_SIDE
6422 processKey(mapper, BTN_SIDE, 1);
6423 processSync(mapper);
6424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6425 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6426 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006427
Michael Wrightd02c5b62014-02-10 15:10:22 -08006428 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(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6431
6432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6433 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6434 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006435
6436 processKey(mapper, BTN_SIDE, 0);
6437 processSync(mapper);
6438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006439 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006440 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006441
6442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006443 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006444 ASSERT_EQ(0, motionArgs.buttonState);
6445
Michael Wrightd02c5b62014-02-10 15:10:22 -08006446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6447 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6448 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6449
6450 // press BTN_FORWARD, release BTN_FORWARD
6451 processKey(mapper, BTN_FORWARD, 1);
6452 processSync(mapper);
6453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6454 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6455 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006456
Michael Wrightd02c5b62014-02-10 15:10:22 -08006457 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(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6460
6461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6462 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6463 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006464
6465 processKey(mapper, BTN_FORWARD, 0);
6466 processSync(mapper);
6467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006468 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006469 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006470
6471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006472 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006473 ASSERT_EQ(0, motionArgs.buttonState);
6474
Michael Wrightd02c5b62014-02-10 15:10:22 -08006475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6476 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6477 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6478
6479 // press BTN_EXTRA, release BTN_EXTRA
6480 processKey(mapper, BTN_EXTRA, 1);
6481 processSync(mapper);
6482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6483 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6484 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006485
Michael Wrightd02c5b62014-02-10 15:10:22 -08006486 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(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6489
6490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6491 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6492 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006493
6494 processKey(mapper, BTN_EXTRA, 0);
6495 processSync(mapper);
6496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006497 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006498 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006499
6500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006501 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006502 ASSERT_EQ(0, motionArgs.buttonState);
6503
Michael Wrightd02c5b62014-02-10 15:10:22 -08006504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6505 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6506 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6507
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6509
Michael Wrightd02c5b62014-02-10 15:10:22 -08006510 // press BTN_STYLUS, release BTN_STYLUS
6511 processKey(mapper, BTN_STYLUS, 1);
6512 processSync(mapper);
6513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6514 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006515 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6516
6517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6518 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6519 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006520
6521 processKey(mapper, BTN_STYLUS, 0);
6522 processSync(mapper);
6523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006524 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006525 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006526
6527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006528 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006529 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006530
6531 // press BTN_STYLUS2, release BTN_STYLUS2
6532 processKey(mapper, BTN_STYLUS2, 1);
6533 processSync(mapper);
6534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6535 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006536 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6537
6538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6539 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6540 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006541
6542 processKey(mapper, BTN_STYLUS2, 0);
6543 processSync(mapper);
6544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006545 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006546 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006547
6548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006549 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006550 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006551
6552 // release touch
6553 processUp(mapper);
6554 processSync(mapper);
6555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6556 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6557 ASSERT_EQ(0, motionArgs.buttonState);
6558}
6559
6560TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006561 addConfigurationProperty("touch.deviceType", "touchScreen");
6562 prepareDisplay(DISPLAY_ORIENTATION_0);
6563 prepareButtons();
6564 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006565 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006566
6567 NotifyMotionArgs motionArgs;
6568
6569 // default tool type is finger
6570 processDown(mapper, 100, 200);
6571 processSync(mapper);
6572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6573 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6574 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6575
6576 // eraser
6577 processKey(mapper, BTN_TOOL_RUBBER, 1);
6578 processSync(mapper);
6579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6580 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6581 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6582
6583 // stylus
6584 processKey(mapper, BTN_TOOL_RUBBER, 0);
6585 processKey(mapper, BTN_TOOL_PEN, 1);
6586 processSync(mapper);
6587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6588 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6589 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6590
6591 // brush
6592 processKey(mapper, BTN_TOOL_PEN, 0);
6593 processKey(mapper, BTN_TOOL_BRUSH, 1);
6594 processSync(mapper);
6595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6596 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6597 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6598
6599 // pencil
6600 processKey(mapper, BTN_TOOL_BRUSH, 0);
6601 processKey(mapper, BTN_TOOL_PENCIL, 1);
6602 processSync(mapper);
6603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6604 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6605 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6606
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006607 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006608 processKey(mapper, BTN_TOOL_PENCIL, 0);
6609 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6610 processSync(mapper);
6611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6612 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6613 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6614
6615 // mouse
6616 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6617 processKey(mapper, BTN_TOOL_MOUSE, 1);
6618 processSync(mapper);
6619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6620 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6621 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6622
6623 // lens
6624 processKey(mapper, BTN_TOOL_MOUSE, 0);
6625 processKey(mapper, BTN_TOOL_LENS, 1);
6626 processSync(mapper);
6627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6628 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6629 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6630
6631 // double-tap
6632 processKey(mapper, BTN_TOOL_LENS, 0);
6633 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6634 processSync(mapper);
6635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6636 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6637 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6638
6639 // triple-tap
6640 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6641 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6642 processSync(mapper);
6643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6645 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6646
6647 // quad-tap
6648 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6649 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6650 processSync(mapper);
6651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6652 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6653 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6654
6655 // finger
6656 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6657 processKey(mapper, BTN_TOOL_FINGER, 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_FINGER, motionArgs.pointerProperties[0].toolType);
6662
6663 // stylus trumps finger
6664 processKey(mapper, BTN_TOOL_PEN, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
6669
6670 // eraser trumps stylus
6671 processKey(mapper, BTN_TOOL_RUBBER, 1);
6672 processSync(mapper);
6673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6674 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6675 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6676
6677 // mouse trumps eraser
6678 processKey(mapper, BTN_TOOL_MOUSE, 1);
6679 processSync(mapper);
6680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6681 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6682 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6683
6684 // back to default tool type
6685 processKey(mapper, BTN_TOOL_MOUSE, 0);
6686 processKey(mapper, BTN_TOOL_RUBBER, 0);
6687 processKey(mapper, BTN_TOOL_PEN, 0);
6688 processKey(mapper, BTN_TOOL_FINGER, 0);
6689 processSync(mapper);
6690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6691 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6692 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6693}
6694
6695TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006696 addConfigurationProperty("touch.deviceType", "touchScreen");
6697 prepareDisplay(DISPLAY_ORIENTATION_0);
6698 prepareButtons();
6699 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006700 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006701 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006702
6703 NotifyMotionArgs motionArgs;
6704
6705 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6706 processKey(mapper, BTN_TOOL_FINGER, 1);
6707 processMove(mapper, 100, 200);
6708 processSync(mapper);
6709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6710 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6711 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6712 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6713
6714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6715 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6716 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6717 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6718
6719 // move a little
6720 processMove(mapper, 150, 250);
6721 processSync(mapper);
6722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6723 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6724 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6725 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6726
6727 // down when BTN_TOUCH is pressed, pressure defaults to 1
6728 processKey(mapper, BTN_TOUCH, 1);
6729 processSync(mapper);
6730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6731 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6732 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6733 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6734
6735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6736 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6737 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6738 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6739
6740 // up when BTN_TOUCH is released, hover restored
6741 processKey(mapper, BTN_TOUCH, 0);
6742 processSync(mapper);
6743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6744 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6745 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6746 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6747
6748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6749 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6750 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6751 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6752
6753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6754 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6755 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6756 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6757
6758 // exit hover when pointer goes away
6759 processKey(mapper, BTN_TOOL_FINGER, 0);
6760 processSync(mapper);
6761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6762 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6763 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6764 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6765}
6766
6767TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006768 addConfigurationProperty("touch.deviceType", "touchScreen");
6769 prepareDisplay(DISPLAY_ORIENTATION_0);
6770 prepareButtons();
6771 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006772 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006773
6774 NotifyMotionArgs motionArgs;
6775
6776 // initially hovering because pressure is 0
6777 processDown(mapper, 100, 200);
6778 processPressure(mapper, 0);
6779 processSync(mapper);
6780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6781 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6782 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6783 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6784
6785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6786 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6787 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6788 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6789
6790 // move a little
6791 processMove(mapper, 150, 250);
6792 processSync(mapper);
6793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6794 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6795 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6796 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6797
6798 // down when pressure is non-zero
6799 processPressure(mapper, RAW_PRESSURE_MAX);
6800 processSync(mapper);
6801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6802 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6803 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6804 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6805
6806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6807 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6808 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6809 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6810
6811 // up when pressure becomes 0, hover restored
6812 processPressure(mapper, 0);
6813 processSync(mapper);
6814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6815 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6816 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6817 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6818
6819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6820 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6821 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6822 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6823
6824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6825 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6826 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6827 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6828
6829 // exit hover when pointer goes away
6830 processUp(mapper);
6831 processSync(mapper);
6832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6833 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6834 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6835 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6836}
6837
lilinnan687e58f2022-07-19 16:00:50 +08006838TEST_F(SingleTouchInputMapperTest,
6839 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6840 addConfigurationProperty("touch.deviceType", "touchScreen");
6841 prepareDisplay(DISPLAY_ORIENTATION_0);
6842 prepareButtons();
6843 prepareAxes(POSITION);
6844 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6845 NotifyMotionArgs motionArgs;
6846
6847 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006848 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006849 processSync(mapper);
6850
6851 // We should receive a down event
6852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6853 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6854
6855 // Change display id
6856 clearViewports();
6857 prepareSecondaryDisplay(ViewportType::INTERNAL);
6858
6859 // We should receive a cancel event
6860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6861 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6862 // Then receive reset called
6863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6864}
6865
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006866TEST_F(SingleTouchInputMapperTest,
6867 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6868 addConfigurationProperty("touch.deviceType", "touchScreen");
6869 prepareDisplay(DISPLAY_ORIENTATION_0);
6870 prepareButtons();
6871 prepareAxes(POSITION);
6872 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6874 NotifyMotionArgs motionArgs;
6875
6876 // Start a new gesture.
6877 processDown(mapper, 100, 200);
6878 processSync(mapper);
6879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6880 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6881
6882 // Make the viewport inactive. This will put the device in disabled mode.
6883 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6884 viewport->isActive = false;
6885 mFakePolicy->updateViewport(*viewport);
6886 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6887
6888 // We should receive a cancel event for the ongoing gesture.
6889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6890 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6891 // Then we should be notified that the device was reset.
6892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6893
6894 // No events are generated while the viewport is inactive.
6895 processMove(mapper, 101, 201);
6896 processSync(mapper);
6897 processDown(mapper, 102, 202);
6898 processSync(mapper);
6899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6900
6901 // Make the viewport active again. The device should resume processing events.
6902 viewport->isActive = true;
6903 mFakePolicy->updateViewport(*viewport);
6904 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6905
6906 // The device is reset because it changes back to direct mode, without generating any events.
6907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6909
6910 // Start a new gesture.
6911 processDown(mapper, 100, 200);
6912 processSync(mapper);
6913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6914 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6915
6916 // No more events.
6917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6919}
6920
Prabir Pradhan5632d622021-09-06 07:57:20 -07006921// --- TouchDisplayProjectionTest ---
6922
6923class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6924public:
6925 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6926 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6927 // rotated equivalent of the given un-rotated physical display bounds.
6928 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6929 uint32_t inverseRotationFlags;
6930 auto width = DISPLAY_WIDTH;
6931 auto height = DISPLAY_HEIGHT;
6932 switch (orientation) {
6933 case DISPLAY_ORIENTATION_90:
6934 inverseRotationFlags = ui::Transform::ROT_270;
6935 std::swap(width, height);
6936 break;
6937 case DISPLAY_ORIENTATION_180:
6938 inverseRotationFlags = ui::Transform::ROT_180;
6939 break;
6940 case DISPLAY_ORIENTATION_270:
6941 inverseRotationFlags = ui::Transform::ROT_90;
6942 std::swap(width, height);
6943 break;
6944 case DISPLAY_ORIENTATION_0:
6945 inverseRotationFlags = ui::Transform::ROT_0;
6946 break;
6947 default:
6948 FAIL() << "Invalid orientation: " << orientation;
6949 }
6950
6951 const ui::Transform rotation(inverseRotationFlags, width, height);
6952 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6953
6954 std::optional<DisplayViewport> internalViewport =
6955 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6956 DisplayViewport& v = *internalViewport;
6957 v.displayId = DISPLAY_ID;
6958 v.orientation = orientation;
6959
6960 v.logicalLeft = 0;
6961 v.logicalTop = 0;
6962 v.logicalRight = 100;
6963 v.logicalBottom = 100;
6964
6965 v.physicalLeft = rotatedPhysicalDisplay.left;
6966 v.physicalTop = rotatedPhysicalDisplay.top;
6967 v.physicalRight = rotatedPhysicalDisplay.right;
6968 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6969
6970 v.deviceWidth = width;
6971 v.deviceHeight = height;
6972
6973 v.isActive = true;
6974 v.uniqueId = UNIQUE_ID;
6975 v.type = ViewportType::INTERNAL;
6976 mFakePolicy->updateViewport(v);
6977 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6978 }
6979
6980 void assertReceivedMove(const Point& point) {
6981 NotifyMotionArgs motionArgs;
6982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6983 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6984 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6985 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6986 1, 0, 0, 0, 0, 0, 0, 0));
6987 }
6988};
6989
6990TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6991 addConfigurationProperty("touch.deviceType", "touchScreen");
6992 prepareDisplay(DISPLAY_ORIENTATION_0);
6993
6994 prepareButtons();
6995 prepareAxes(POSITION);
6996 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6997
6998 NotifyMotionArgs motionArgs;
6999
7000 // Configure the DisplayViewport such that the logical display maps to a subsection of
7001 // the display panel called the physical display. Here, the physical display is bounded by the
7002 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7003 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7004 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7005 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7006
7007 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7008 DISPLAY_ORIENTATION_270}) {
7009 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7010
7011 // Touches outside the physical display should be ignored, and should not generate any
7012 // events. Ensure touches at the following points that lie outside of the physical display
7013 // area do not generate any events.
7014 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7015 processDown(mapper, toRawX(point.x), toRawY(point.y));
7016 processSync(mapper);
7017 processUp(mapper);
7018 processSync(mapper);
7019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7020 << "Unexpected event generated for touch outside physical display at point: "
7021 << point.x << ", " << point.y;
7022 }
7023 }
7024}
7025
7026TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7027 addConfigurationProperty("touch.deviceType", "touchScreen");
7028 prepareDisplay(DISPLAY_ORIENTATION_0);
7029
7030 prepareButtons();
7031 prepareAxes(POSITION);
7032 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7033
7034 NotifyMotionArgs motionArgs;
7035
7036 // Configure the DisplayViewport such that the logical display maps to a subsection of
7037 // the display panel called the physical display. Here, the physical display is bounded by the
7038 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7039 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7040
7041 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7042 DISPLAY_ORIENTATION_270}) {
7043 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7044
7045 // Touches that start outside the physical display should be ignored until it enters the
7046 // physical display bounds, at which point it should generate a down event. Start a touch at
7047 // the point (5, 100), which is outside the physical display bounds.
7048 static const Point kOutsidePoint{5, 100};
7049 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7050 processSync(mapper);
7051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7052
7053 // Move the touch into the physical display area. This should generate a pointer down.
7054 processMove(mapper, toRawX(11), toRawY(21));
7055 processSync(mapper);
7056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7057 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7058 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7059 ASSERT_NO_FATAL_FAILURE(
7060 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7061
7062 // Move the touch inside the physical display area. This should generate a pointer move.
7063 processMove(mapper, toRawX(69), toRawY(159));
7064 processSync(mapper);
7065 assertReceivedMove({69, 159});
7066
7067 // Move outside the physical display area. Since the pointer is already down, this should
7068 // now continue generating events.
7069 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7070 processSync(mapper);
7071 assertReceivedMove(kOutsidePoint);
7072
7073 // Release. This should generate a pointer up.
7074 processUp(mapper);
7075 processSync(mapper);
7076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7077 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7078 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7079 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7080
7081 // Ensure no more events were generated.
7082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7084 }
7085}
7086
Michael Wrightd02c5b62014-02-10 15:10:22 -08007087// --- MultiTouchInputMapperTest ---
7088
7089class MultiTouchInputMapperTest : public TouchInputMapperTest {
7090protected:
7091 void prepareAxes(int axes);
7092
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007093 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7094 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7095 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7096 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7097 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7098 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7099 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7100 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7101 void processId(MultiTouchInputMapper& mapper, int32_t id);
7102 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7103 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7104 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
7105 void processMTSync(MultiTouchInputMapper& mapper);
7106 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007107};
7108
7109void MultiTouchInputMapperTest::prepareAxes(int axes) {
7110 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007111 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7112 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007113 }
7114 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007115 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7116 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007117 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007118 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7119 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007120 }
7121 }
7122 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007123 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7124 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007125 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007126 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007127 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007128 }
7129 }
7130 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007131 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7132 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007133 }
7134 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007135 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7136 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007137 }
7138 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007139 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7140 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007141 }
7142 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007143 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7144 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007145 }
7146 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007147 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7148 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007149 }
7150 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007151 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007152 }
7153}
7154
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007155void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7156 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007157 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7158 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007159}
7160
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007161void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7162 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007164}
7165
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007166void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7167 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007168 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007169}
7170
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007171void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007172 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007173}
7174
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007175void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007176 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007177}
7178
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007179void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7180 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007181 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007182}
7183
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007184void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007185 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007186}
7187
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007188void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007189 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007190}
7191
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007192void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007193 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007194}
7195
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007196void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007198}
7199
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007200void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007201 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007202}
7203
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007204void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7205 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007206 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007207}
7208
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007209void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007210 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007211}
7212
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007213void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007214 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007215}
7216
Michael Wrightd02c5b62014-02-10 15:10:22 -08007217TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007218 addConfigurationProperty("touch.deviceType", "touchScreen");
7219 prepareDisplay(DISPLAY_ORIENTATION_0);
7220 prepareAxes(POSITION);
7221 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007222 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007223
arthurhungdcef2dc2020-08-11 14:47:50 +08007224 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007225
7226 NotifyMotionArgs motionArgs;
7227
7228 // Two fingers down at once.
7229 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7230 processPosition(mapper, x1, y1);
7231 processMTSync(mapper);
7232 processPosition(mapper, x2, y2);
7233 processMTSync(mapper);
7234 processSync(mapper);
7235
7236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7237 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7238 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7239 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7240 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7241 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7242 ASSERT_EQ(0, motionArgs.flags);
7243 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7244 ASSERT_EQ(0, motionArgs.buttonState);
7245 ASSERT_EQ(0, motionArgs.edgeFlags);
7246 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7247 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7248 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7249 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7250 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7251 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7252 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7253 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7254
7255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7256 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7257 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7258 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7259 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007260 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007261 ASSERT_EQ(0, motionArgs.flags);
7262 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7263 ASSERT_EQ(0, motionArgs.buttonState);
7264 ASSERT_EQ(0, motionArgs.edgeFlags);
7265 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7266 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7267 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7268 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7269 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7270 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7271 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7272 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7273 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7274 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7275 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7276 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7277
7278 // Move.
7279 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7280 processPosition(mapper, x1, y1);
7281 processMTSync(mapper);
7282 processPosition(mapper, x2, y2);
7283 processMTSync(mapper);
7284 processSync(mapper);
7285
7286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7287 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7288 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7289 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7290 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7291 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7292 ASSERT_EQ(0, motionArgs.flags);
7293 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7294 ASSERT_EQ(0, motionArgs.buttonState);
7295 ASSERT_EQ(0, motionArgs.edgeFlags);
7296 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7297 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7298 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7299 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7300 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7301 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7302 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7303 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7304 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7305 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7306 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7307 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7308
7309 // First finger up.
7310 x2 += 15; y2 -= 20;
7311 processPosition(mapper, x2, y2);
7312 processMTSync(mapper);
7313 processSync(mapper);
7314
7315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7316 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7317 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7318 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7319 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007320 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007321 ASSERT_EQ(0, motionArgs.flags);
7322 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7323 ASSERT_EQ(0, motionArgs.buttonState);
7324 ASSERT_EQ(0, motionArgs.edgeFlags);
7325 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7326 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7327 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7328 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7329 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7330 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7331 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7332 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7333 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7334 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7335 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7336 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7337
7338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7339 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7340 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7341 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7342 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7343 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7344 ASSERT_EQ(0, motionArgs.flags);
7345 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7346 ASSERT_EQ(0, motionArgs.buttonState);
7347 ASSERT_EQ(0, motionArgs.edgeFlags);
7348 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7349 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7350 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7351 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7352 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7353 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7354 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7355 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7356
7357 // Move.
7358 x2 += 20; y2 -= 25;
7359 processPosition(mapper, x2, y2);
7360 processMTSync(mapper);
7361 processSync(mapper);
7362
7363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7364 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7365 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7366 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7367 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7368 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7369 ASSERT_EQ(0, motionArgs.flags);
7370 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7371 ASSERT_EQ(0, motionArgs.buttonState);
7372 ASSERT_EQ(0, motionArgs.edgeFlags);
7373 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7374 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7375 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7376 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7377 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7378 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7379 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7380 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7381
7382 // New finger down.
7383 int32_t x3 = 700, y3 = 300;
7384 processPosition(mapper, x2, y2);
7385 processMTSync(mapper);
7386 processPosition(mapper, x3, y3);
7387 processMTSync(mapper);
7388 processSync(mapper);
7389
7390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7391 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7392 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7393 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7394 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007395 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007396 ASSERT_EQ(0, motionArgs.flags);
7397 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7398 ASSERT_EQ(0, motionArgs.buttonState);
7399 ASSERT_EQ(0, motionArgs.edgeFlags);
7400 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7401 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7402 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7403 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7404 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7405 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7406 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7407 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7408 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7409 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7410 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7411 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7412
7413 // Second finger up.
7414 x3 += 30; y3 -= 20;
7415 processPosition(mapper, x3, y3);
7416 processMTSync(mapper);
7417 processSync(mapper);
7418
7419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7420 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7421 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7422 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7423 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007424 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007425 ASSERT_EQ(0, motionArgs.flags);
7426 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7427 ASSERT_EQ(0, motionArgs.buttonState);
7428 ASSERT_EQ(0, motionArgs.edgeFlags);
7429 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7430 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7431 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7432 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7433 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7434 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7435 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7437 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7438 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7439 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7440 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7441
7442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7443 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7444 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7445 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7446 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7447 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7448 ASSERT_EQ(0, motionArgs.flags);
7449 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7450 ASSERT_EQ(0, motionArgs.buttonState);
7451 ASSERT_EQ(0, motionArgs.edgeFlags);
7452 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7453 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7454 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7455 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7456 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7457 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7458 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7459 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7460
7461 // Last finger up.
7462 processMTSync(mapper);
7463 processSync(mapper);
7464
7465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7466 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7467 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7468 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7469 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7470 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7471 ASSERT_EQ(0, motionArgs.flags);
7472 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7473 ASSERT_EQ(0, motionArgs.buttonState);
7474 ASSERT_EQ(0, motionArgs.edgeFlags);
7475 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7476 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7477 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7478 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7479 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7480 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7481 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7482 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7483
7484 // Should not have sent any more keys or motions.
7485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7487}
7488
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007489TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7490 addConfigurationProperty("touch.deviceType", "touchScreen");
7491 prepareDisplay(DISPLAY_ORIENTATION_0);
7492
7493 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7494 /*fuzz*/ 0, /*resolution*/ 10);
7495 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7496 /*fuzz*/ 0, /*resolution*/ 11);
7497 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7498 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7499 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7500 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7501 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7502 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7503 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7504 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7505
7506 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7507
7508 // X and Y axes
7509 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7510 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7511 // Touch major and minor
7512 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7513 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7514 // Tool major and minor
7515 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7516 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7517}
7518
7519TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7520 addConfigurationProperty("touch.deviceType", "touchScreen");
7521 prepareDisplay(DISPLAY_ORIENTATION_0);
7522
7523 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7524 /*fuzz*/ 0, /*resolution*/ 10);
7525 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7526 /*fuzz*/ 0, /*resolution*/ 11);
7527
7528 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7529
7530 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7531
7532 // Touch major and minor
7533 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7534 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7535 // Tool major and minor
7536 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7537 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7538}
7539
Michael Wrightd02c5b62014-02-10 15:10:22 -08007540TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007541 addConfigurationProperty("touch.deviceType", "touchScreen");
7542 prepareDisplay(DISPLAY_ORIENTATION_0);
7543 prepareAxes(POSITION | ID);
7544 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007545 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007546
arthurhungdcef2dc2020-08-11 14:47:50 +08007547 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007548
7549 NotifyMotionArgs motionArgs;
7550
7551 // Two fingers down at once.
7552 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7553 processPosition(mapper, x1, y1);
7554 processId(mapper, 1);
7555 processMTSync(mapper);
7556 processPosition(mapper, x2, y2);
7557 processId(mapper, 2);
7558 processMTSync(mapper);
7559 processSync(mapper);
7560
7561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7562 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7563 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7564 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7565 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7567 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7568
7569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007570 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007571 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7572 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7573 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7574 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7575 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7576 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7577 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7579 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7580
7581 // Move.
7582 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7583 processPosition(mapper, x1, y1);
7584 processId(mapper, 1);
7585 processMTSync(mapper);
7586 processPosition(mapper, x2, y2);
7587 processId(mapper, 2);
7588 processMTSync(mapper);
7589 processSync(mapper);
7590
7591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7592 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7593 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7594 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7595 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7596 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7597 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7599 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7601 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7602
7603 // First finger up.
7604 x2 += 15; y2 -= 20;
7605 processPosition(mapper, x2, y2);
7606 processId(mapper, 2);
7607 processMTSync(mapper);
7608 processSync(mapper);
7609
7610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007611 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007612 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7613 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7614 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7615 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7616 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7617 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7618 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7619 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7620 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7621
7622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7623 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7624 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7625 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7626 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7627 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7628 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7629
7630 // Move.
7631 x2 += 20; y2 -= 25;
7632 processPosition(mapper, x2, y2);
7633 processId(mapper, 2);
7634 processMTSync(mapper);
7635 processSync(mapper);
7636
7637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7638 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7639 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7640 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7641 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7642 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7643 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7644
7645 // New finger down.
7646 int32_t x3 = 700, y3 = 300;
7647 processPosition(mapper, x2, y2);
7648 processId(mapper, 2);
7649 processMTSync(mapper);
7650 processPosition(mapper, x3, y3);
7651 processId(mapper, 3);
7652 processMTSync(mapper);
7653 processSync(mapper);
7654
7655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007656 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007657 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7658 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7659 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7660 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7661 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7662 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7663 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7664 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7665 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7666
7667 // Second finger up.
7668 x3 += 30; y3 -= 20;
7669 processPosition(mapper, x3, y3);
7670 processId(mapper, 3);
7671 processMTSync(mapper);
7672 processSync(mapper);
7673
7674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007675 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007676 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7677 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7678 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7679 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7680 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7681 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7682 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7683 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7684 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7685
7686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7687 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7688 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7689 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7690 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7691 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7692 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7693
7694 // Last finger up.
7695 processMTSync(mapper);
7696 processSync(mapper);
7697
7698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7699 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7700 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7701 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7702 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7703 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7704 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7705
7706 // Should not have sent any more keys or motions.
7707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7709}
7710
7711TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007712 addConfigurationProperty("touch.deviceType", "touchScreen");
7713 prepareDisplay(DISPLAY_ORIENTATION_0);
7714 prepareAxes(POSITION | ID | SLOT);
7715 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007716 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007717
arthurhungdcef2dc2020-08-11 14:47:50 +08007718 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007719
7720 NotifyMotionArgs motionArgs;
7721
7722 // Two fingers down at once.
7723 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7724 processPosition(mapper, x1, y1);
7725 processId(mapper, 1);
7726 processSlot(mapper, 1);
7727 processPosition(mapper, x2, y2);
7728 processId(mapper, 2);
7729 processSync(mapper);
7730
7731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7732 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7733 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7734 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7735 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7737 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7738
7739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007740 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007741 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7742 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7743 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7744 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7745 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7746 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7747 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7748 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7749 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7750
7751 // Move.
7752 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7753 processSlot(mapper, 0);
7754 processPosition(mapper, x1, y1);
7755 processSlot(mapper, 1);
7756 processPosition(mapper, x2, y2);
7757 processSync(mapper);
7758
7759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7760 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7761 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7762 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7763 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7764 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7765 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7767 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7769 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7770
7771 // First finger up.
7772 x2 += 15; y2 -= 20;
7773 processSlot(mapper, 0);
7774 processId(mapper, -1);
7775 processSlot(mapper, 1);
7776 processPosition(mapper, x2, y2);
7777 processSync(mapper);
7778
7779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007780 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007781 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7782 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7783 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7784 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7785 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7786 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7787 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7789 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7790
7791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7792 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7793 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7794 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7795 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7796 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7797 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7798
7799 // Move.
7800 x2 += 20; y2 -= 25;
7801 processPosition(mapper, x2, y2);
7802 processSync(mapper);
7803
7804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7805 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7806 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7807 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7808 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7809 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7810 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7811
7812 // New finger down.
7813 int32_t x3 = 700, y3 = 300;
7814 processPosition(mapper, x2, y2);
7815 processSlot(mapper, 0);
7816 processId(mapper, 3);
7817 processPosition(mapper, x3, y3);
7818 processSync(mapper);
7819
7820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007821 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007822 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7823 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7824 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7825 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7826 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7827 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7828 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7829 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7830 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7831
7832 // Second finger up.
7833 x3 += 30; y3 -= 20;
7834 processSlot(mapper, 1);
7835 processId(mapper, -1);
7836 processSlot(mapper, 0);
7837 processPosition(mapper, x3, y3);
7838 processSync(mapper);
7839
7840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007841 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007842 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7843 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7844 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7845 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7846 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7847 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7848 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7849 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7850 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7851
7852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7853 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7854 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7855 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7856 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7857 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7858 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7859
7860 // Last finger up.
7861 processId(mapper, -1);
7862 processSync(mapper);
7863
7864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7865 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7866 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7867 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7868 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7869 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7870 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7871
7872 // Should not have sent any more keys or motions.
7873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7875}
7876
7877TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007878 addConfigurationProperty("touch.deviceType", "touchScreen");
7879 prepareDisplay(DISPLAY_ORIENTATION_0);
7880 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007881 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007882
7883 // These calculations are based on the input device calibration documentation.
7884 int32_t rawX = 100;
7885 int32_t rawY = 200;
7886 int32_t rawTouchMajor = 7;
7887 int32_t rawTouchMinor = 6;
7888 int32_t rawToolMajor = 9;
7889 int32_t rawToolMinor = 8;
7890 int32_t rawPressure = 11;
7891 int32_t rawDistance = 0;
7892 int32_t rawOrientation = 3;
7893 int32_t id = 5;
7894
7895 float x = toDisplayX(rawX);
7896 float y = toDisplayY(rawY);
7897 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7898 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7899 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7900 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7901 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7902 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7903 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7904 float distance = float(rawDistance);
7905
7906 processPosition(mapper, rawX, rawY);
7907 processTouchMajor(mapper, rawTouchMajor);
7908 processTouchMinor(mapper, rawTouchMinor);
7909 processToolMajor(mapper, rawToolMajor);
7910 processToolMinor(mapper, rawToolMinor);
7911 processPressure(mapper, rawPressure);
7912 processOrientation(mapper, rawOrientation);
7913 processDistance(mapper, rawDistance);
7914 processId(mapper, id);
7915 processMTSync(mapper);
7916 processSync(mapper);
7917
7918 NotifyMotionArgs args;
7919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7920 ASSERT_EQ(0, args.pointerProperties[0].id);
7921 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7922 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7923 orientation, distance));
7924}
7925
7926TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007927 addConfigurationProperty("touch.deviceType", "touchScreen");
7928 prepareDisplay(DISPLAY_ORIENTATION_0);
7929 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7930 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007931 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007932
7933 // These calculations are based on the input device calibration documentation.
7934 int32_t rawX = 100;
7935 int32_t rawY = 200;
7936 int32_t rawTouchMajor = 140;
7937 int32_t rawTouchMinor = 120;
7938 int32_t rawToolMajor = 180;
7939 int32_t rawToolMinor = 160;
7940
7941 float x = toDisplayX(rawX);
7942 float y = toDisplayY(rawY);
7943 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7944 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7945 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7946 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7947 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7948
7949 processPosition(mapper, rawX, rawY);
7950 processTouchMajor(mapper, rawTouchMajor);
7951 processTouchMinor(mapper, rawTouchMinor);
7952 processToolMajor(mapper, rawToolMajor);
7953 processToolMinor(mapper, rawToolMinor);
7954 processMTSync(mapper);
7955 processSync(mapper);
7956
7957 NotifyMotionArgs args;
7958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7959 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7960 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7961}
7962
7963TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007964 addConfigurationProperty("touch.deviceType", "touchScreen");
7965 prepareDisplay(DISPLAY_ORIENTATION_0);
7966 prepareAxes(POSITION | TOUCH | TOOL);
7967 addConfigurationProperty("touch.size.calibration", "diameter");
7968 addConfigurationProperty("touch.size.scale", "10");
7969 addConfigurationProperty("touch.size.bias", "160");
7970 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007971 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007972
7973 // These calculations are based on the input device calibration documentation.
7974 // Note: We only provide a single common touch/tool value because the device is assumed
7975 // not to emit separate values for each pointer (isSummed = 1).
7976 int32_t rawX = 100;
7977 int32_t rawY = 200;
7978 int32_t rawX2 = 150;
7979 int32_t rawY2 = 250;
7980 int32_t rawTouchMajor = 5;
7981 int32_t rawToolMajor = 8;
7982
7983 float x = toDisplayX(rawX);
7984 float y = toDisplayY(rawY);
7985 float x2 = toDisplayX(rawX2);
7986 float y2 = toDisplayY(rawY2);
7987 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7988 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7989 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7990
7991 processPosition(mapper, rawX, rawY);
7992 processTouchMajor(mapper, rawTouchMajor);
7993 processToolMajor(mapper, rawToolMajor);
7994 processMTSync(mapper);
7995 processPosition(mapper, rawX2, rawY2);
7996 processTouchMajor(mapper, rawTouchMajor);
7997 processToolMajor(mapper, rawToolMajor);
7998 processMTSync(mapper);
7999 processSync(mapper);
8000
8001 NotifyMotionArgs args;
8002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8003 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8004
8005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008006 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008007 ASSERT_EQ(size_t(2), args.pointerCount);
8008 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8009 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8010 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8011 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8012}
8013
8014TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008015 addConfigurationProperty("touch.deviceType", "touchScreen");
8016 prepareDisplay(DISPLAY_ORIENTATION_0);
8017 prepareAxes(POSITION | TOUCH | TOOL);
8018 addConfigurationProperty("touch.size.calibration", "area");
8019 addConfigurationProperty("touch.size.scale", "43");
8020 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008021 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008022
8023 // These calculations are based on the input device calibration documentation.
8024 int32_t rawX = 100;
8025 int32_t rawY = 200;
8026 int32_t rawTouchMajor = 5;
8027 int32_t rawToolMajor = 8;
8028
8029 float x = toDisplayX(rawX);
8030 float y = toDisplayY(rawY);
8031 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8032 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8033 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8034
8035 processPosition(mapper, rawX, rawY);
8036 processTouchMajor(mapper, rawTouchMajor);
8037 processToolMajor(mapper, rawToolMajor);
8038 processMTSync(mapper);
8039 processSync(mapper);
8040
8041 NotifyMotionArgs args;
8042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8043 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8044 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8045}
8046
8047TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008048 addConfigurationProperty("touch.deviceType", "touchScreen");
8049 prepareDisplay(DISPLAY_ORIENTATION_0);
8050 prepareAxes(POSITION | PRESSURE);
8051 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8052 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008053 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008054
Michael Wrightaa449c92017-12-13 21:21:43 +00008055 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008056 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008057 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8058 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8059 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8060
Michael Wrightd02c5b62014-02-10 15:10:22 -08008061 // These calculations are based on the input device calibration documentation.
8062 int32_t rawX = 100;
8063 int32_t rawY = 200;
8064 int32_t rawPressure = 60;
8065
8066 float x = toDisplayX(rawX);
8067 float y = toDisplayY(rawY);
8068 float pressure = float(rawPressure) * 0.01f;
8069
8070 processPosition(mapper, rawX, rawY);
8071 processPressure(mapper, rawPressure);
8072 processMTSync(mapper);
8073 processSync(mapper);
8074
8075 NotifyMotionArgs args;
8076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8077 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8078 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8079}
8080
8081TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008082 addConfigurationProperty("touch.deviceType", "touchScreen");
8083 prepareDisplay(DISPLAY_ORIENTATION_0);
8084 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008085 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008086
8087 NotifyMotionArgs motionArgs;
8088 NotifyKeyArgs keyArgs;
8089
8090 processId(mapper, 1);
8091 processPosition(mapper, 100, 200);
8092 processSync(mapper);
8093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8094 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8095 ASSERT_EQ(0, motionArgs.buttonState);
8096
8097 // press BTN_LEFT, release BTN_LEFT
8098 processKey(mapper, BTN_LEFT, 1);
8099 processSync(mapper);
8100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8101 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8102 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8103
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8105 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8106 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8107
Michael Wrightd02c5b62014-02-10 15:10:22 -08008108 processKey(mapper, BTN_LEFT, 0);
8109 processSync(mapper);
8110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008111 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008112 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008113
8114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008115 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008116 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008117
8118 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8119 processKey(mapper, BTN_RIGHT, 1);
8120 processKey(mapper, BTN_MIDDLE, 1);
8121 processSync(mapper);
8122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8123 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8124 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8125 motionArgs.buttonState);
8126
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8128 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8129 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8130
8131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8132 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8133 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8134 motionArgs.buttonState);
8135
Michael Wrightd02c5b62014-02-10 15:10:22 -08008136 processKey(mapper, BTN_RIGHT, 0);
8137 processSync(mapper);
8138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008139 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008140 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008141
8142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008143 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008144 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008145
8146 processKey(mapper, BTN_MIDDLE, 0);
8147 processSync(mapper);
8148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008149 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008150 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008151
8152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008153 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008154 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008155
8156 // press BTN_BACK, release BTN_BACK
8157 processKey(mapper, BTN_BACK, 1);
8158 processSync(mapper);
8159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8160 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8161 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008162
Michael Wrightd02c5b62014-02-10 15:10:22 -08008163 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(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8166
8167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8168 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8169 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008170
8171 processKey(mapper, BTN_BACK, 0);
8172 processSync(mapper);
8173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008174 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008175 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008176
8177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008178 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008179 ASSERT_EQ(0, motionArgs.buttonState);
8180
Michael Wrightd02c5b62014-02-10 15:10:22 -08008181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8182 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8183 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8184
8185 // press BTN_SIDE, release BTN_SIDE
8186 processKey(mapper, BTN_SIDE, 1);
8187 processSync(mapper);
8188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8189 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8190 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008191
Michael Wrightd02c5b62014-02-10 15:10:22 -08008192 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(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8195
8196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8197 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8198 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008199
8200 processKey(mapper, BTN_SIDE, 0);
8201 processSync(mapper);
8202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008203 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008204 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008205
8206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008207 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008208 ASSERT_EQ(0, motionArgs.buttonState);
8209
Michael Wrightd02c5b62014-02-10 15:10:22 -08008210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8211 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8212 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8213
8214 // press BTN_FORWARD, release BTN_FORWARD
8215 processKey(mapper, BTN_FORWARD, 1);
8216 processSync(mapper);
8217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8218 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8219 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008220
Michael Wrightd02c5b62014-02-10 15:10:22 -08008221 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(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8224
8225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8226 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8227 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008228
8229 processKey(mapper, BTN_FORWARD, 0);
8230 processSync(mapper);
8231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008232 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008233 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008234
8235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008237 ASSERT_EQ(0, motionArgs.buttonState);
8238
Michael Wrightd02c5b62014-02-10 15:10:22 -08008239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8240 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8241 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8242
8243 // press BTN_EXTRA, release BTN_EXTRA
8244 processKey(mapper, BTN_EXTRA, 1);
8245 processSync(mapper);
8246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8247 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8248 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008249
Michael Wrightd02c5b62014-02-10 15:10:22 -08008250 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(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8253
8254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8255 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8256 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008257
8258 processKey(mapper, BTN_EXTRA, 0);
8259 processSync(mapper);
8260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008261 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008262 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008263
8264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008265 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008266 ASSERT_EQ(0, motionArgs.buttonState);
8267
Michael Wrightd02c5b62014-02-10 15:10:22 -08008268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8269 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8270 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8271
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8273
Michael Wrightd02c5b62014-02-10 15:10:22 -08008274 // press BTN_STYLUS, release BTN_STYLUS
8275 processKey(mapper, BTN_STYLUS, 1);
8276 processSync(mapper);
8277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8278 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008279 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8280
8281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8282 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8283 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008284
8285 processKey(mapper, BTN_STYLUS, 0);
8286 processSync(mapper);
8287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008288 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008289 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008290
8291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008292 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008293 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008294
8295 // press BTN_STYLUS2, release BTN_STYLUS2
8296 processKey(mapper, BTN_STYLUS2, 1);
8297 processSync(mapper);
8298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8299 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008300 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8301
8302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8303 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8304 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008305
8306 processKey(mapper, BTN_STYLUS2, 0);
8307 processSync(mapper);
8308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008309 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008310 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008311
8312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008313 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008314 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008315
8316 // release touch
8317 processId(mapper, -1);
8318 processSync(mapper);
8319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8320 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8321 ASSERT_EQ(0, motionArgs.buttonState);
8322}
8323
8324TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008325 addConfigurationProperty("touch.deviceType", "touchScreen");
8326 prepareDisplay(DISPLAY_ORIENTATION_0);
8327 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008328 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008329
8330 NotifyMotionArgs motionArgs;
8331
8332 // default tool type is finger
8333 processId(mapper, 1);
8334 processPosition(mapper, 100, 200);
8335 processSync(mapper);
8336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8337 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8338 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8339
8340 // eraser
8341 processKey(mapper, BTN_TOOL_RUBBER, 1);
8342 processSync(mapper);
8343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8344 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8345 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8346
8347 // stylus
8348 processKey(mapper, BTN_TOOL_RUBBER, 0);
8349 processKey(mapper, BTN_TOOL_PEN, 1);
8350 processSync(mapper);
8351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8352 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8353 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8354
8355 // brush
8356 processKey(mapper, BTN_TOOL_PEN, 0);
8357 processKey(mapper, BTN_TOOL_BRUSH, 1);
8358 processSync(mapper);
8359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8360 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8361 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8362
8363 // pencil
8364 processKey(mapper, BTN_TOOL_BRUSH, 0);
8365 processKey(mapper, BTN_TOOL_PENCIL, 1);
8366 processSync(mapper);
8367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8368 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8369 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8370
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008371 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008372 processKey(mapper, BTN_TOOL_PENCIL, 0);
8373 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8374 processSync(mapper);
8375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8376 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8377 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8378
8379 // mouse
8380 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8381 processKey(mapper, BTN_TOOL_MOUSE, 1);
8382 processSync(mapper);
8383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8384 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8385 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8386
8387 // lens
8388 processKey(mapper, BTN_TOOL_MOUSE, 0);
8389 processKey(mapper, BTN_TOOL_LENS, 1);
8390 processSync(mapper);
8391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8392 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8393 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8394
8395 // double-tap
8396 processKey(mapper, BTN_TOOL_LENS, 0);
8397 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8398 processSync(mapper);
8399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8400 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8401 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8402
8403 // triple-tap
8404 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8405 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8406 processSync(mapper);
8407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8408 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8409 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8410
8411 // quad-tap
8412 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8413 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8414 processSync(mapper);
8415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8416 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8417 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8418
8419 // finger
8420 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8421 processKey(mapper, BTN_TOOL_FINGER, 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_FINGER, motionArgs.pointerProperties[0].toolType);
8426
8427 // stylus trumps finger
8428 processKey(mapper, BTN_TOOL_PEN, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
8433
8434 // eraser trumps stylus
8435 processKey(mapper, BTN_TOOL_RUBBER, 1);
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_ERASER, motionArgs.pointerProperties[0].toolType);
8440
8441 // mouse trumps eraser
8442 processKey(mapper, BTN_TOOL_MOUSE, 1);
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_MOUSE, motionArgs.pointerProperties[0].toolType);
8447
8448 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8449 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8450 processSync(mapper);
8451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8452 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8453 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8454
8455 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8456 processToolType(mapper, MT_TOOL_PEN);
8457 processSync(mapper);
8458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8459 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8460 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8461
8462 // back to default tool type
8463 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8464 processKey(mapper, BTN_TOOL_MOUSE, 0);
8465 processKey(mapper, BTN_TOOL_RUBBER, 0);
8466 processKey(mapper, BTN_TOOL_PEN, 0);
8467 processKey(mapper, BTN_TOOL_FINGER, 0);
8468 processSync(mapper);
8469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8470 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8471 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8472}
8473
8474TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008475 addConfigurationProperty("touch.deviceType", "touchScreen");
8476 prepareDisplay(DISPLAY_ORIENTATION_0);
8477 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008478 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008479 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008480
8481 NotifyMotionArgs motionArgs;
8482
8483 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8484 processId(mapper, 1);
8485 processPosition(mapper, 100, 200);
8486 processSync(mapper);
8487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8488 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8490 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8491
8492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8493 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8495 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8496
8497 // move a little
8498 processPosition(mapper, 150, 250);
8499 processSync(mapper);
8500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8501 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8502 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8503 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8504
8505 // down when BTN_TOUCH is pressed, pressure defaults to 1
8506 processKey(mapper, BTN_TOUCH, 1);
8507 processSync(mapper);
8508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8509 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8511 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8512
8513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8514 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8516 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8517
8518 // up when BTN_TOUCH is released, hover restored
8519 processKey(mapper, BTN_TOUCH, 0);
8520 processSync(mapper);
8521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8522 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8523 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8524 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8525
8526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8527 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8529 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8530
8531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8532 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8534 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8535
8536 // exit hover when pointer goes away
8537 processId(mapper, -1);
8538 processSync(mapper);
8539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8540 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8541 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8542 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8543}
8544
8545TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008546 addConfigurationProperty("touch.deviceType", "touchScreen");
8547 prepareDisplay(DISPLAY_ORIENTATION_0);
8548 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008549 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008550
8551 NotifyMotionArgs motionArgs;
8552
8553 // initially hovering because pressure is 0
8554 processId(mapper, 1);
8555 processPosition(mapper, 100, 200);
8556 processPressure(mapper, 0);
8557 processSync(mapper);
8558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8559 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8561 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8562
8563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8564 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8566 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8567
8568 // move a little
8569 processPosition(mapper, 150, 250);
8570 processSync(mapper);
8571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8572 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8574 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8575
8576 // down when pressure becomes non-zero
8577 processPressure(mapper, RAW_PRESSURE_MAX);
8578 processSync(mapper);
8579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8580 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8582 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8583
8584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8585 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8586 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8587 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8588
8589 // up when pressure becomes 0, hover restored
8590 processPressure(mapper, 0);
8591 processSync(mapper);
8592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8593 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8594 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8595 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8596
8597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8598 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8600 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8601
8602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8603 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8604 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8605 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8606
8607 // exit hover when pointer goes away
8608 processId(mapper, -1);
8609 processSync(mapper);
8610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8611 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8613 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8614}
8615
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008616/**
8617 * Set the input device port <--> display port associations, and check that the
8618 * events are routed to the display that matches the display port.
8619 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8620 */
8621TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008622 const std::string usb2 = "USB2";
8623 const uint8_t hdmi1 = 0;
8624 const uint8_t hdmi2 = 1;
8625 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008626 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008627
8628 addConfigurationProperty("touch.deviceType", "touchScreen");
8629 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008630 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008631
8632 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8633 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8634
8635 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8636 // for this input device is specified, and the matching viewport is not present,
8637 // the input device should be disabled (at the mapper level).
8638
8639 // Add viewport for display 2 on hdmi2
8640 prepareSecondaryDisplay(type, hdmi2);
8641 // Send a touch event
8642 processPosition(mapper, 100, 100);
8643 processSync(mapper);
8644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8645
8646 // Add viewport for display 1 on hdmi1
8647 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8648 // Send a touch event again
8649 processPosition(mapper, 100, 100);
8650 processSync(mapper);
8651
8652 NotifyMotionArgs args;
8653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8654 ASSERT_EQ(DISPLAY_ID, args.displayId);
8655}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008656
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008657TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8658 addConfigurationProperty("touch.deviceType", "touchScreen");
8659 prepareAxes(POSITION);
8660 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8661
8662 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8663
8664 prepareDisplay(DISPLAY_ORIENTATION_0);
8665 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8666
8667 // Send a touch event
8668 processPosition(mapper, 100, 100);
8669 processSync(mapper);
8670
8671 NotifyMotionArgs args;
8672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8673 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8674}
8675
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008676TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008677 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008678 std::shared_ptr<FakePointerController> fakePointerController =
8679 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008680 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008681 fakePointerController->setPosition(100, 200);
8682 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008683 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008684
Garfield Tan888a6a42020-01-09 11:39:16 -08008685 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008686 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008687
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008688 prepareDisplay(DISPLAY_ORIENTATION_0);
8689 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008690 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008691
8692 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008693 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008694
8695 NotifyMotionArgs motionArgs;
8696 processPosition(mapper, 100, 100);
8697 processSync(mapper);
8698
8699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8700 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8701 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8702}
8703
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008704/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008705 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8706 */
8707TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8708 addConfigurationProperty("touch.deviceType", "touchScreen");
8709 prepareAxes(POSITION);
8710 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8711
8712 prepareDisplay(DISPLAY_ORIENTATION_0);
8713 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8714 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8715 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8716 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8717
8718 NotifyMotionArgs args;
8719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8720 ASSERT_EQ(26, args.readTime);
8721
8722 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8723 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8724 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8725
8726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8727 ASSERT_EQ(33, args.readTime);
8728}
8729
8730/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008731 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8732 * events should not be delivered to the listener.
8733 */
8734TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8735 addConfigurationProperty("touch.deviceType", "touchScreen");
8736 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8737 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8738 ViewportType::INTERNAL);
8739 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8740 prepareAxes(POSITION);
8741 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8742
8743 NotifyMotionArgs motionArgs;
8744 processPosition(mapper, 100, 100);
8745 processSync(mapper);
8746
8747 mFakeListener->assertNotifyMotionWasNotCalled();
8748}
8749
Garfield Tanc734e4f2021-01-15 20:01:39 -08008750TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8751 addConfigurationProperty("touch.deviceType", "touchScreen");
8752 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8753 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8754 ViewportType::INTERNAL);
8755 std::optional<DisplayViewport> optionalDisplayViewport =
8756 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8757 ASSERT_TRUE(optionalDisplayViewport.has_value());
8758 DisplayViewport displayViewport = *optionalDisplayViewport;
8759
8760 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8761 prepareAxes(POSITION);
8762 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8763
8764 // Finger down
8765 int32_t x = 100, y = 100;
8766 processPosition(mapper, x, y);
8767 processSync(mapper);
8768
8769 NotifyMotionArgs motionArgs;
8770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8771 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8772
8773 // Deactivate display viewport
8774 displayViewport.isActive = false;
8775 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8776 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8777
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008778 // The ongoing touch should be canceled immediately
8779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8780 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8781
8782 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008783 x += 10, y += 10;
8784 processPosition(mapper, x, y);
8785 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008787
8788 // Reactivate display viewport
8789 displayViewport.isActive = true;
8790 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8791 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8792
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008793 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008794 x += 10, y += 10;
8795 processPosition(mapper, x, y);
8796 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8798 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008799}
8800
Arthur Hung7c645402019-01-25 17:45:42 +08008801TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8802 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008803 prepareAxes(POSITION | ID | SLOT);
8804 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008805 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008806
8807 // Create the second touch screen device, and enable multi fingers.
8808 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008809 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008810 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008811 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008812 std::shared_ptr<InputDevice> device2 =
8813 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008814 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008815
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008816 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8817 0 /*flat*/, 0 /*fuzz*/);
8818 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8819 0 /*flat*/, 0 /*fuzz*/);
8820 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8821 0 /*flat*/, 0 /*fuzz*/);
8822 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8823 0 /*flat*/, 0 /*fuzz*/);
8824 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8825 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8826 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008827
8828 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008829 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008830 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8831 device2->reset(ARBITRARY_TIME);
8832
8833 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008834 std::shared_ptr<FakePointerController> fakePointerController =
8835 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008836 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008837
8838 // Setup policy for associated displays and show touches.
8839 const uint8_t hdmi1 = 0;
8840 const uint8_t hdmi2 = 1;
8841 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8842 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8843 mFakePolicy->setShowTouches(true);
8844
8845 // Create displays.
8846 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008847 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008848
8849 // Default device will reconfigure above, need additional reconfiguration for another device.
8850 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan197e0862022-07-01 14:28:00 +00008851 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8852 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008853
8854 // Two fingers down at default display.
8855 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8856 processPosition(mapper, x1, y1);
8857 processId(mapper, 1);
8858 processSlot(mapper, 1);
8859 processPosition(mapper, x2, y2);
8860 processId(mapper, 2);
8861 processSync(mapper);
8862
8863 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8864 fakePointerController->getSpots().find(DISPLAY_ID);
8865 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8866 ASSERT_EQ(size_t(2), iter->second.size());
8867
8868 // Two fingers down at second display.
8869 processPosition(mapper2, x1, y1);
8870 processId(mapper2, 1);
8871 processSlot(mapper2, 1);
8872 processPosition(mapper2, x2, y2);
8873 processId(mapper2, 2);
8874 processSync(mapper2);
8875
8876 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8877 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8878 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008879
8880 // Disable the show touches configuration and ensure the spots are cleared.
8881 mFakePolicy->setShowTouches(false);
8882 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8883 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
8884
8885 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008886}
8887
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008888TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008889 prepareAxes(POSITION);
8890 addConfigurationProperty("touch.deviceType", "touchScreen");
8891 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008892 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008893
8894 NotifyMotionArgs motionArgs;
8895 // Unrotated video frame
8896 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8897 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008898 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008899 processPosition(mapper, 100, 200);
8900 processSync(mapper);
8901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8902 ASSERT_EQ(frames, motionArgs.videoFrames);
8903
8904 // Subsequent touch events should not have any videoframes
8905 // This is implemented separately in FakeEventHub,
8906 // but that should match the behaviour of TouchVideoDevice.
8907 processPosition(mapper, 200, 200);
8908 processSync(mapper);
8909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8910 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8911}
8912
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008913TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008914 prepareAxes(POSITION);
8915 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008916 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008917 // Unrotated video frame
8918 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8919 NotifyMotionArgs motionArgs;
8920
8921 // Test all 4 orientations
8922 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008923 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8924 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8925 clearViewports();
8926 prepareDisplay(orientation);
8927 std::vector<TouchVideoFrame> frames{frame};
8928 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8929 processPosition(mapper, 100, 200);
8930 processSync(mapper);
8931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8932 ASSERT_EQ(frames, motionArgs.videoFrames);
8933 }
8934}
8935
8936TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8937 prepareAxes(POSITION);
8938 addConfigurationProperty("touch.deviceType", "touchScreen");
8939 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8940 // orientation-aware are affected by display rotation.
8941 addConfigurationProperty("touch.orientationAware", "0");
8942 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8943 // Unrotated video frame
8944 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8945 NotifyMotionArgs motionArgs;
8946
8947 // Test all 4 orientations
8948 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008949 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8950 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8951 clearViewports();
8952 prepareDisplay(orientation);
8953 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008954 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008955 processPosition(mapper, 100, 200);
8956 processSync(mapper);
8957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008958 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8959 // compared to the display. This is so that when the window transform (which contains the
8960 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8961 // window's coordinate space.
8962 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008963 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08008964
8965 // Release finger.
8966 processSync(mapper);
8967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008968 }
8969}
8970
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008971TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008972 prepareAxes(POSITION);
8973 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008974 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008975 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8976 // so mix these.
8977 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8978 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8979 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8980 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8981 NotifyMotionArgs motionArgs;
8982
8983 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008984 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008985 processPosition(mapper, 100, 200);
8986 processSync(mapper);
8987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008988 ASSERT_EQ(frames, motionArgs.videoFrames);
8989}
8990
8991TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8992 prepareAxes(POSITION);
8993 addConfigurationProperty("touch.deviceType", "touchScreen");
8994 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8995 // orientation-aware are affected by display rotation.
8996 addConfigurationProperty("touch.orientationAware", "0");
8997 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8998 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8999 // so mix these.
9000 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9001 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9002 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9003 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9004 NotifyMotionArgs motionArgs;
9005
9006 prepareDisplay(DISPLAY_ORIENTATION_90);
9007 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9008 processPosition(mapper, 100, 200);
9009 processSync(mapper);
9010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9011 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9012 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9013 // compared to the display. This is so that when the window transform (which contains the
9014 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9015 // window's coordinate space.
9016 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
9017 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009018 ASSERT_EQ(frames, motionArgs.videoFrames);
9019}
9020
Arthur Hung9da14732019-09-02 16:16:58 +08009021/**
9022 * If we had defined port associations, but the viewport is not ready, the touch device would be
9023 * expected to be disabled, and it should be enabled after the viewport has found.
9024 */
9025TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009026 constexpr uint8_t hdmi2 = 1;
9027 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009028 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009029
9030 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9031
9032 addConfigurationProperty("touch.deviceType", "touchScreen");
9033 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009034 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009035
9036 ASSERT_EQ(mDevice->isEnabled(), false);
9037
9038 // Add display on hdmi2, the device should be enabled and can receive touch event.
9039 prepareSecondaryDisplay(type, hdmi2);
9040 ASSERT_EQ(mDevice->isEnabled(), true);
9041
9042 // Send a touch event.
9043 processPosition(mapper, 100, 100);
9044 processSync(mapper);
9045
9046 NotifyMotionArgs args;
9047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9048 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9049}
9050
Arthur Hung421eb1c2020-01-16 00:09:42 +08009051TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009052 addConfigurationProperty("touch.deviceType", "touchScreen");
9053 prepareDisplay(DISPLAY_ORIENTATION_0);
9054 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009055 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009056
9057 NotifyMotionArgs motionArgs;
9058
9059 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9060 // finger down
9061 processId(mapper, 1);
9062 processPosition(mapper, x1, y1);
9063 processSync(mapper);
9064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9065 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9066 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9067
9068 // finger move
9069 processId(mapper, 1);
9070 processPosition(mapper, x2, y2);
9071 processSync(mapper);
9072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9073 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9074 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9075
9076 // finger up.
9077 processId(mapper, -1);
9078 processSync(mapper);
9079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9080 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9081 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9082
9083 // new finger down
9084 processId(mapper, 1);
9085 processPosition(mapper, x3, y3);
9086 processSync(mapper);
9087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9088 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9089 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9090}
9091
9092/**
arthurhungcc7f9802020-04-30 17:55:40 +08009093 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9094 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009095 */
arthurhungcc7f9802020-04-30 17:55:40 +08009096TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009097 addConfigurationProperty("touch.deviceType", "touchScreen");
9098 prepareDisplay(DISPLAY_ORIENTATION_0);
9099 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009100 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009101
9102 NotifyMotionArgs motionArgs;
9103
9104 // default tool type is finger
9105 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009106 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009107 processPosition(mapper, x1, y1);
9108 processSync(mapper);
9109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9110 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9111 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9112
9113 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9114 processToolType(mapper, MT_TOOL_PALM);
9115 processSync(mapper);
9116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9117 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9118
9119 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009120 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009121 processPosition(mapper, x2, y2);
9122 processSync(mapper);
9123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9124
9125 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009126 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009127 processSync(mapper);
9128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9129
9130 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009131 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009132 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009133 processPosition(mapper, x3, y3);
9134 processSync(mapper);
9135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9136 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9137 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9138}
9139
arthurhungbf89a482020-04-17 17:37:55 +08009140/**
arthurhungcc7f9802020-04-30 17:55:40 +08009141 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9142 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009143 */
arthurhungcc7f9802020-04-30 17:55:40 +08009144TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009145 addConfigurationProperty("touch.deviceType", "touchScreen");
9146 prepareDisplay(DISPLAY_ORIENTATION_0);
9147 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9148 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9149
9150 NotifyMotionArgs motionArgs;
9151
9152 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009153 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9154 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009155 processPosition(mapper, x1, y1);
9156 processSync(mapper);
9157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9158 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9159 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9160
9161 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009162 processSlot(mapper, SECOND_SLOT);
9163 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009164 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009165 processSync(mapper);
9166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009167 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009168 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9169
9170 // If the tool type of the first finger changes to MT_TOOL_PALM,
9171 // we expect to receive ACTION_POINTER_UP with cancel flag.
9172 processSlot(mapper, FIRST_SLOT);
9173 processId(mapper, FIRST_TRACKING_ID);
9174 processToolType(mapper, MT_TOOL_PALM);
9175 processSync(mapper);
9176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009177 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009178 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9179
9180 // The following MOVE events of second finger should be processed.
9181 processSlot(mapper, SECOND_SLOT);
9182 processId(mapper, SECOND_TRACKING_ID);
9183 processPosition(mapper, x2 + 1, y2 + 1);
9184 processSync(mapper);
9185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9186 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9187 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9188
9189 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9190 // it. Second finger receive move.
9191 processSlot(mapper, FIRST_SLOT);
9192 processId(mapper, INVALID_TRACKING_ID);
9193 processSync(mapper);
9194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9195 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9196 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9197
9198 // Second finger keeps moving.
9199 processSlot(mapper, SECOND_SLOT);
9200 processId(mapper, SECOND_TRACKING_ID);
9201 processPosition(mapper, x2 + 2, y2 + 2);
9202 processSync(mapper);
9203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9204 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9205 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9206
9207 // Second finger up.
9208 processId(mapper, INVALID_TRACKING_ID);
9209 processSync(mapper);
9210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9211 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9212 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9213}
9214
9215/**
9216 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9217 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9218 */
9219TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9220 addConfigurationProperty("touch.deviceType", "touchScreen");
9221 prepareDisplay(DISPLAY_ORIENTATION_0);
9222 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9223 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9224
9225 NotifyMotionArgs motionArgs;
9226
9227 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9228 // First finger down.
9229 processId(mapper, FIRST_TRACKING_ID);
9230 processPosition(mapper, x1, y1);
9231 processSync(mapper);
9232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9233 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9234 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9235
9236 // Second finger down.
9237 processSlot(mapper, SECOND_SLOT);
9238 processId(mapper, SECOND_TRACKING_ID);
9239 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009240 processSync(mapper);
9241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009242 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009243 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9244
arthurhungcc7f9802020-04-30 17:55:40 +08009245 // If the tool type of the first finger changes to MT_TOOL_PALM,
9246 // we expect to receive ACTION_POINTER_UP with cancel flag.
9247 processSlot(mapper, FIRST_SLOT);
9248 processId(mapper, FIRST_TRACKING_ID);
9249 processToolType(mapper, MT_TOOL_PALM);
9250 processSync(mapper);
9251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009252 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009253 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9254
9255 // Second finger keeps moving.
9256 processSlot(mapper, SECOND_SLOT);
9257 processId(mapper, SECOND_TRACKING_ID);
9258 processPosition(mapper, x2 + 1, y2 + 1);
9259 processSync(mapper);
9260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9262
9263 // second finger becomes palm, receive cancel due to only 1 finger is active.
9264 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009265 processToolType(mapper, MT_TOOL_PALM);
9266 processSync(mapper);
9267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9268 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9269
arthurhungcc7f9802020-04-30 17:55:40 +08009270 // third finger down.
9271 processSlot(mapper, THIRD_SLOT);
9272 processId(mapper, THIRD_TRACKING_ID);
9273 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009274 processPosition(mapper, x3, y3);
9275 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9277 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9278 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009279 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9280
9281 // third finger move
9282 processId(mapper, THIRD_TRACKING_ID);
9283 processPosition(mapper, x3 + 1, y3 + 1);
9284 processSync(mapper);
9285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9286 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9287
9288 // first finger up, third finger receive move.
9289 processSlot(mapper, FIRST_SLOT);
9290 processId(mapper, INVALID_TRACKING_ID);
9291 processSync(mapper);
9292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9293 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9294 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9295
9296 // second finger up, third finger receive move.
9297 processSlot(mapper, SECOND_SLOT);
9298 processId(mapper, INVALID_TRACKING_ID);
9299 processSync(mapper);
9300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9301 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9302 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9303
9304 // third finger up.
9305 processSlot(mapper, THIRD_SLOT);
9306 processId(mapper, INVALID_TRACKING_ID);
9307 processSync(mapper);
9308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9309 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9310 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9311}
9312
9313/**
9314 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9315 * and the active finger could still be allowed to receive the events
9316 */
9317TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9318 addConfigurationProperty("touch.deviceType", "touchScreen");
9319 prepareDisplay(DISPLAY_ORIENTATION_0);
9320 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9321 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9322
9323 NotifyMotionArgs motionArgs;
9324
9325 // default tool type is finger
9326 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9327 processId(mapper, FIRST_TRACKING_ID);
9328 processPosition(mapper, x1, y1);
9329 processSync(mapper);
9330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9331 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9332 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9333
9334 // Second finger down.
9335 processSlot(mapper, SECOND_SLOT);
9336 processId(mapper, SECOND_TRACKING_ID);
9337 processPosition(mapper, x2, y2);
9338 processSync(mapper);
9339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009340 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009341 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9342
9343 // If the tool type of the second finger changes to MT_TOOL_PALM,
9344 // we expect to receive ACTION_POINTER_UP with cancel flag.
9345 processId(mapper, SECOND_TRACKING_ID);
9346 processToolType(mapper, MT_TOOL_PALM);
9347 processSync(mapper);
9348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009349 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009350 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9351
9352 // The following MOVE event should be processed.
9353 processSlot(mapper, FIRST_SLOT);
9354 processId(mapper, FIRST_TRACKING_ID);
9355 processPosition(mapper, x1 + 1, y1 + 1);
9356 processSync(mapper);
9357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9358 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9359 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9360
9361 // second finger up.
9362 processSlot(mapper, SECOND_SLOT);
9363 processId(mapper, INVALID_TRACKING_ID);
9364 processSync(mapper);
9365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9366 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9367
9368 // first finger keep moving
9369 processSlot(mapper, FIRST_SLOT);
9370 processId(mapper, FIRST_TRACKING_ID);
9371 processPosition(mapper, x1 + 2, y1 + 2);
9372 processSync(mapper);
9373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9374 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9375
9376 // first finger up.
9377 processId(mapper, INVALID_TRACKING_ID);
9378 processSync(mapper);
9379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9380 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9381 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009382}
9383
Arthur Hung9ad18942021-06-19 02:04:46 +00009384/**
9385 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9386 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9387 * cause slot be valid again.
9388 */
9389TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9390 addConfigurationProperty("touch.deviceType", "touchScreen");
9391 prepareDisplay(DISPLAY_ORIENTATION_0);
9392 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9393 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9394
9395 NotifyMotionArgs motionArgs;
9396
9397 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9398 // First finger down.
9399 processId(mapper, FIRST_TRACKING_ID);
9400 processPosition(mapper, x1, y1);
9401 processPressure(mapper, RAW_PRESSURE_MAX);
9402 processSync(mapper);
9403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9404 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9405 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9406
9407 // First finger move.
9408 processId(mapper, FIRST_TRACKING_ID);
9409 processPosition(mapper, x1 + 1, y1 + 1);
9410 processPressure(mapper, RAW_PRESSURE_MAX);
9411 processSync(mapper);
9412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9413 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9414 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9415
9416 // Second finger down.
9417 processSlot(mapper, SECOND_SLOT);
9418 processId(mapper, SECOND_TRACKING_ID);
9419 processPosition(mapper, x2, y2);
9420 processPressure(mapper, RAW_PRESSURE_MAX);
9421 processSync(mapper);
9422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009423 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009424 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9425
9426 // second finger up with some unexpected data.
9427 processSlot(mapper, SECOND_SLOT);
9428 processId(mapper, INVALID_TRACKING_ID);
9429 processPosition(mapper, x2, y2);
9430 processSync(mapper);
9431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009432 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009433 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9434
9435 // first finger up with some unexpected data.
9436 processSlot(mapper, FIRST_SLOT);
9437 processId(mapper, INVALID_TRACKING_ID);
9438 processPosition(mapper, x2, y2);
9439 processPressure(mapper, RAW_PRESSURE_MAX);
9440 processSync(mapper);
9441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9442 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9443 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9444}
9445
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009446// --- MultiTouchInputMapperTest_ExternalDevice ---
9447
9448class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9449protected:
Chris Yea52ade12020-08-27 16:49:20 -07009450 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009451};
9452
9453/**
9454 * Expect fallback to internal viewport if device is external and external viewport is not present.
9455 */
9456TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9457 prepareAxes(POSITION);
9458 addConfigurationProperty("touch.deviceType", "touchScreen");
9459 prepareDisplay(DISPLAY_ORIENTATION_0);
9460 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9461
9462 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9463
9464 NotifyMotionArgs motionArgs;
9465
9466 // Expect the event to be sent to the internal viewport,
9467 // because an external viewport is not present.
9468 processPosition(mapper, 100, 100);
9469 processSync(mapper);
9470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9471 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9472
9473 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009474 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009475 processPosition(mapper, 100, 100);
9476 processSync(mapper);
9477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9478 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9479}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009480
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009481TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9482 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9483 std::shared_ptr<FakePointerController> fakePointerController =
9484 std::make_shared<FakePointerController>();
9485 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9486 fakePointerController->setPosition(0, 0);
9487 fakePointerController->setButtonState(0);
9488
9489 // prepare device and capture
9490 prepareDisplay(DISPLAY_ORIENTATION_0);
9491 prepareAxes(POSITION | ID | SLOT);
9492 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9493 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9494 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009495 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009496 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9497
9498 // captured touchpad should be a touchpad source
9499 NotifyDeviceResetArgs resetArgs;
9500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9501 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9502
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009503 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009504
9505 const InputDeviceInfo::MotionRange* relRangeX =
9506 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9507 ASSERT_NE(relRangeX, nullptr);
9508 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9509 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9510 const InputDeviceInfo::MotionRange* relRangeY =
9511 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9512 ASSERT_NE(relRangeY, nullptr);
9513 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9514 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9515
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009516 // run captured pointer tests - note that this is unscaled, so input listener events should be
9517 // identical to what the hardware sends (accounting for any
9518 // calibration).
9519 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009520 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009521 processId(mapper, 1);
9522 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9523 processKey(mapper, BTN_TOUCH, 1);
9524 processSync(mapper);
9525
9526 // expect coord[0] to contain initial location of touch 0
9527 NotifyMotionArgs args;
9528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9529 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9530 ASSERT_EQ(1U, args.pointerCount);
9531 ASSERT_EQ(0, args.pointerProperties[0].id);
9532 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9533 ASSERT_NO_FATAL_FAILURE(
9534 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9535
9536 // FINGER 1 DOWN
9537 processSlot(mapper, 1);
9538 processId(mapper, 2);
9539 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9540 processSync(mapper);
9541
9542 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009544 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009545 ASSERT_EQ(2U, args.pointerCount);
9546 ASSERT_EQ(0, args.pointerProperties[0].id);
9547 ASSERT_EQ(1, args.pointerProperties[1].id);
9548 ASSERT_NO_FATAL_FAILURE(
9549 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9550 ASSERT_NO_FATAL_FAILURE(
9551 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9552
9553 // FINGER 1 MOVE
9554 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9555 processSync(mapper);
9556
9557 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9558 // from move
9559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9560 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9561 ASSERT_NO_FATAL_FAILURE(
9562 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9563 ASSERT_NO_FATAL_FAILURE(
9564 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9565
9566 // FINGER 0 MOVE
9567 processSlot(mapper, 0);
9568 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9569 processSync(mapper);
9570
9571 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9573 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9574 ASSERT_NO_FATAL_FAILURE(
9575 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9576 ASSERT_NO_FATAL_FAILURE(
9577 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9578
9579 // BUTTON DOWN
9580 processKey(mapper, BTN_LEFT, 1);
9581 processSync(mapper);
9582
9583 // touchinputmapper design sends a move before button press
9584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9585 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9587 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9588
9589 // BUTTON UP
9590 processKey(mapper, BTN_LEFT, 0);
9591 processSync(mapper);
9592
9593 // touchinputmapper design sends a move after button release
9594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9595 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9597 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9598
9599 // FINGER 0 UP
9600 processId(mapper, -1);
9601 processSync(mapper);
9602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9603 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9604
9605 // FINGER 1 MOVE
9606 processSlot(mapper, 1);
9607 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9608 processSync(mapper);
9609
9610 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9612 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9613 ASSERT_EQ(1U, args.pointerCount);
9614 ASSERT_EQ(1, args.pointerProperties[0].id);
9615 ASSERT_NO_FATAL_FAILURE(
9616 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9617
9618 // FINGER 1 UP
9619 processId(mapper, -1);
9620 processKey(mapper, BTN_TOUCH, 0);
9621 processSync(mapper);
9622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9623 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9624
9625 // non captured touchpad should be a mouse source
9626 mFakePolicy->setPointerCapture(false);
9627 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9629 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9630}
9631
9632TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9633 std::shared_ptr<FakePointerController> fakePointerController =
9634 std::make_shared<FakePointerController>();
9635 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9636 fakePointerController->setPosition(0, 0);
9637 fakePointerController->setButtonState(0);
9638
9639 // prepare device and capture
9640 prepareDisplay(DISPLAY_ORIENTATION_0);
9641 prepareAxes(POSITION | ID | SLOT);
9642 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9643 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009644 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009645 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9646 // run uncaptured pointer tests - pushes out generic events
9647 // FINGER 0 DOWN
9648 processId(mapper, 3);
9649 processPosition(mapper, 100, 100);
9650 processKey(mapper, BTN_TOUCH, 1);
9651 processSync(mapper);
9652
9653 // start at (100,100), cursor should be at (0,0) * scale
9654 NotifyMotionArgs args;
9655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9656 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9657 ASSERT_NO_FATAL_FAILURE(
9658 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9659
9660 // FINGER 0 MOVE
9661 processPosition(mapper, 200, 200);
9662 processSync(mapper);
9663
9664 // compute scaling to help with touch position checking
9665 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9666 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9667 float scale =
9668 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9669
9670 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9672 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9673 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9674 0, 0, 0, 0, 0, 0, 0));
9675}
9676
9677TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9678 std::shared_ptr<FakePointerController> fakePointerController =
9679 std::make_shared<FakePointerController>();
9680
9681 prepareDisplay(DISPLAY_ORIENTATION_0);
9682 prepareAxes(POSITION | ID | SLOT);
9683 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009684 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009685 mFakePolicy->setPointerCapture(false);
9686 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9687
9688 // uncaptured touchpad should be a pointer device
9689 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9690
9691 // captured touchpad should be a touchpad device
9692 mFakePolicy->setPointerCapture(true);
9693 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9694 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9695}
9696
HQ Liue6983c72022-04-19 22:14:56 +00009697class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9698protected:
9699 float mPointerMovementScale;
9700 float mPointerXZoomScale;
9701 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9702 addConfigurationProperty("touch.deviceType", "pointer");
9703 std::shared_ptr<FakePointerController> fakePointerController =
9704 std::make_shared<FakePointerController>();
9705 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9706 fakePointerController->setPosition(0, 0);
9707 fakePointerController->setButtonState(0);
9708 prepareDisplay(DISPLAY_ORIENTATION_0);
9709
9710 prepareAxes(POSITION);
9711 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9712 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9713 // needs to be disabled, and the pointer gesture needs to be enabled.
9714 mFakePolicy->setPointerCapture(false);
9715 mFakePolicy->setPointerGestureEnabled(true);
9716 mFakePolicy->setPointerController(fakePointerController);
9717
9718 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9719 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9720 mPointerMovementScale =
9721 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9722 mPointerXZoomScale =
9723 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9724 }
9725
9726 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9727 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9728 /*flat*/ 0,
9729 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9730 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9731 /*flat*/ 0,
9732 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9733 }
9734};
9735
9736/**
9737 * Two fingers down on a pointer mode touch pad. The width
9738 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9739 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9740 * be greater than the both value to be freeform gesture, so that after two
9741 * fingers start to move downwards, the gesture should be swipe.
9742 */
9743TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9744 // The min freeform gesture width is 25units/mm x 30mm = 750
9745 // which is greater than fraction of the diagnal length of the touchpad (349).
9746 // Thus, MaxSwipWidth is 750.
9747 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9748 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9749 NotifyMotionArgs motionArgs;
9750
9751 // Two fingers down at once.
9752 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9753 // Pointer's initial position is used the [0,0] coordinate.
9754 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9755
9756 processId(mapper, FIRST_TRACKING_ID);
9757 processPosition(mapper, x1, y1);
9758 processMTSync(mapper);
9759 processId(mapper, SECOND_TRACKING_ID);
9760 processPosition(mapper, x2, y2);
9761 processMTSync(mapper);
9762 processSync(mapper);
9763
9764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9765 ASSERT_EQ(1U, motionArgs.pointerCount);
9766 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9767 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009768 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009769 ASSERT_NO_FATAL_FAILURE(
9770 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9771
9772 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9773 // that there should be 1 pointer.
9774 int32_t movingDistance = 200;
9775 y1 += movingDistance;
9776 y2 += movingDistance;
9777
9778 processId(mapper, FIRST_TRACKING_ID);
9779 processPosition(mapper, x1, y1);
9780 processMTSync(mapper);
9781 processId(mapper, SECOND_TRACKING_ID);
9782 processPosition(mapper, x2, y2);
9783 processMTSync(mapper);
9784 processSync(mapper);
9785
9786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9787 ASSERT_EQ(1U, motionArgs.pointerCount);
9788 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9789 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009790 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009791 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9792 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9793 0, 0, 0, 0));
9794}
9795
9796/**
9797 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9798 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9799 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9800 * value to be freeform gesture, so that after two fingers start to move downwards,
9801 * the gesture should be swipe.
9802 */
9803TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9804 // The min freeform gesture width is 5units/mm x 30mm = 150
9805 // which is greater than fraction of the diagnal length of the touchpad (349).
9806 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
9807 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
9808 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9809 NotifyMotionArgs motionArgs;
9810
9811 // Two fingers down at once.
9812 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9813 // Pointer's initial position is used the [0,0] coordinate.
9814 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9815
9816 processId(mapper, FIRST_TRACKING_ID);
9817 processPosition(mapper, x1, y1);
9818 processMTSync(mapper);
9819 processId(mapper, SECOND_TRACKING_ID);
9820 processPosition(mapper, x2, y2);
9821 processMTSync(mapper);
9822 processSync(mapper);
9823
9824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9825 ASSERT_EQ(1U, motionArgs.pointerCount);
9826 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9827 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009828 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009829 ASSERT_NO_FATAL_FAILURE(
9830 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9831
9832 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9833 // and there should be 1 pointer.
9834 int32_t movingDistance = 200;
9835 y1 += movingDistance;
9836 y2 += movingDistance;
9837
9838 processId(mapper, FIRST_TRACKING_ID);
9839 processPosition(mapper, x1, y1);
9840 processMTSync(mapper);
9841 processId(mapper, SECOND_TRACKING_ID);
9842 processPosition(mapper, x2, y2);
9843 processMTSync(mapper);
9844 processSync(mapper);
9845
9846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9847 ASSERT_EQ(1U, motionArgs.pointerCount);
9848 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9849 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009850 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009851 // New coordinate is the scaled relative coordinate from the initial coordinate.
9852 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9853 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9854 0, 0, 0, 0));
9855}
9856
9857/**
9858 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
9859 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
9860 * freeform gestures after two fingers start to move downwards.
9861 */
9862TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
9863 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9864 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9865
9866 NotifyMotionArgs motionArgs;
9867
9868 // Two fingers down at once. Wider than the max swipe width.
9869 // The gesture is expected to be PRESS, then transformed to FREEFORM
9870 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
9871
9872 processId(mapper, FIRST_TRACKING_ID);
9873 processPosition(mapper, x1, y1);
9874 processMTSync(mapper);
9875 processId(mapper, SECOND_TRACKING_ID);
9876 processPosition(mapper, x2, y2);
9877 processMTSync(mapper);
9878 processSync(mapper);
9879
9880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9881 ASSERT_EQ(1U, motionArgs.pointerCount);
9882 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9883 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009884 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009885 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
9886 ASSERT_NO_FATAL_FAILURE(
9887 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9888
9889 int32_t movingDistance = 200;
9890
9891 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
9892 // then two down events for two pointers.
9893 y1 += movingDistance;
9894 y2 += movingDistance;
9895
9896 processId(mapper, FIRST_TRACKING_ID);
9897 processPosition(mapper, x1, y1);
9898 processMTSync(mapper);
9899 processId(mapper, SECOND_TRACKING_ID);
9900 processPosition(mapper, x2, y2);
9901 processMTSync(mapper);
9902 processSync(mapper);
9903
9904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9905 // The previous PRESS gesture is cancelled, because it is transformed to freeform
9906 ASSERT_EQ(1U, motionArgs.pointerCount);
9907 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9909 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9910 ASSERT_EQ(1U, motionArgs.pointerCount);
9911 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9913 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009914 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009915 ASSERT_EQ(2U, motionArgs.pointerCount);
9916 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
9917 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009918 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009919 // Two pointers' scaled relative coordinates from their initial centroid.
9920 // Initial y coordinates are 0 as y1 and y2 have the same value.
9921 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
9922 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
9923 // When pointers move, the new coordinates equal to the initial coordinates plus
9924 // scaled moving distance.
9925 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9926 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9927 0, 0, 0, 0));
9928 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9929 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9930 0, 0, 0, 0));
9931
9932 // Move two fingers down again, expect one MOVE motion event.
9933 y1 += movingDistance;
9934 y2 += movingDistance;
9935
9936 processId(mapper, FIRST_TRACKING_ID);
9937 processPosition(mapper, x1, y1);
9938 processMTSync(mapper);
9939 processId(mapper, SECOND_TRACKING_ID);
9940 processPosition(mapper, x2, y2);
9941 processMTSync(mapper);
9942 processSync(mapper);
9943
9944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9945 ASSERT_EQ(2U, motionArgs.pointerCount);
9946 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9947 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009948 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009949 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9950 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9951 0, 0, 0, 0, 0));
9952 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9953 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9954 0, 0, 0, 0, 0));
9955}
9956
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009957// --- JoystickInputMapperTest ---
9958
9959class JoystickInputMapperTest : public InputMapperTest {
9960protected:
9961 static const int32_t RAW_X_MIN;
9962 static const int32_t RAW_X_MAX;
9963 static const int32_t RAW_Y_MIN;
9964 static const int32_t RAW_Y_MAX;
9965
9966 void SetUp() override {
9967 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9968 }
9969 void prepareAxes() {
9970 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9971 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9972 }
9973
9974 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9975 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9976 }
9977
9978 void processSync(JoystickInputMapper& mapper) {
9979 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9980 }
9981
9982 void prepareVirtualDisplay(int32_t orientation) {
9983 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9984 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9985 NO_PORT, ViewportType::VIRTUAL);
9986 }
9987};
9988
9989const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9990const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9991const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9992const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9993
9994TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9995 prepareAxes();
9996 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9997
9998 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9999
10000 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
10001
10002 // Send an axis event
10003 processAxis(mapper, ABS_X, 100);
10004 processSync(mapper);
10005
10006 NotifyMotionArgs args;
10007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10008 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10009
10010 // Send another axis event
10011 processAxis(mapper, ABS_Y, 100);
10012 processSync(mapper);
10013
10014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10015 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10016}
10017
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010018// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010019
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010020class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010021protected:
10022 static const char* DEVICE_NAME;
10023 static const char* DEVICE_LOCATION;
10024 static const int32_t DEVICE_ID;
10025 static const int32_t DEVICE_GENERATION;
10026 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010027 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010028 static const int32_t EVENTHUB_ID;
10029
10030 std::shared_ptr<FakeEventHub> mFakeEventHub;
10031 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010032 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010033 std::unique_ptr<InstrumentedInputReader> mReader;
10034 std::shared_ptr<InputDevice> mDevice;
10035
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010036 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010037 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010038 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010039 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010040 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010041 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010042 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10043 }
10044
10045 void SetUp() override { SetUp(DEVICE_CLASSES); }
10046
10047 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010048 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010049 mFakePolicy.clear();
10050 }
10051
10052 void configureDevice(uint32_t changes) {
10053 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10054 mReader->requestRefreshConfiguration(changes);
10055 mReader->loopOnce();
10056 }
10057 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
10058 }
10059
10060 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10061 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010062 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010063 InputDeviceIdentifier identifier;
10064 identifier.name = name;
10065 identifier.location = location;
10066 std::shared_ptr<InputDevice> device =
10067 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10068 identifier);
10069 mReader->pushNextDevice(device);
10070 mFakeEventHub->addDevice(eventHubId, name, classes);
10071 mReader->loopOnce();
10072 return device;
10073 }
10074
10075 template <class T, typename... Args>
10076 T& addControllerAndConfigure(Args... args) {
10077 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10078
10079 return controller;
10080 }
10081};
10082
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010083const char* PeripheralControllerTest::DEVICE_NAME = "device";
10084const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10085const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10086const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10087const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010088const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10089 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010090const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010091
10092// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010093class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010094protected:
10095 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010096 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010097 }
10098};
10099
10100TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010101 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010102
10103 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
10104 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
10105}
10106
10107TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010108 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010109
10110 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
10111 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
10112}
10113
10114// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010115class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010116protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010117 void SetUp() override {
10118 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10119 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010120};
10121
Chris Ye85758332021-05-16 23:05:17 -070010122TEST_F(LightControllerTest, MonoLight) {
10123 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010124 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010125 .maxBrightness = 255,
10126 .flags = InputLightClass::BRIGHTNESS,
10127 .path = ""};
10128 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010129
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010130 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010131 InputDeviceInfo info;
10132 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010133 std::vector<InputDeviceLightInfo> lights = info.getLights();
10134 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010135 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10136 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10137
10138 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10139 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10140}
10141
10142TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10143 RawLightInfo infoMono = {.id = 1,
10144 .name = "mono_keyboard_backlight",
10145 .maxBrightness = 255,
10146 .flags = InputLightClass::BRIGHTNESS |
10147 InputLightClass::KEYBOARD_BACKLIGHT,
10148 .path = ""};
10149 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10150
10151 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10152 InputDeviceInfo info;
10153 controller.populateDeviceInfo(&info);
10154 std::vector<InputDeviceLightInfo> lights = info.getLights();
10155 ASSERT_EQ(1U, lights.size());
10156 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10157 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010158
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010159 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10160 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010161}
10162
10163TEST_F(LightControllerTest, RGBLight) {
10164 RawLightInfo infoRed = {.id = 1,
10165 .name = "red",
10166 .maxBrightness = 255,
10167 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10168 .path = ""};
10169 RawLightInfo infoGreen = {.id = 2,
10170 .name = "green",
10171 .maxBrightness = 255,
10172 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10173 .path = ""};
10174 RawLightInfo infoBlue = {.id = 3,
10175 .name = "blue",
10176 .maxBrightness = 255,
10177 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10178 .path = ""};
10179 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10180 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10181 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10182
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010183 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010184 InputDeviceInfo info;
10185 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010186 std::vector<InputDeviceLightInfo> lights = info.getLights();
10187 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010188 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10189 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10190 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10191
10192 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10193 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10194}
10195
10196TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10197 RawLightInfo infoRed = {.id = 1,
10198 .name = "red_keyboard_backlight",
10199 .maxBrightness = 255,
10200 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10201 InputLightClass::KEYBOARD_BACKLIGHT,
10202 .path = ""};
10203 RawLightInfo infoGreen = {.id = 2,
10204 .name = "green_keyboard_backlight",
10205 .maxBrightness = 255,
10206 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10207 InputLightClass::KEYBOARD_BACKLIGHT,
10208 .path = ""};
10209 RawLightInfo infoBlue = {.id = 3,
10210 .name = "blue_keyboard_backlight",
10211 .maxBrightness = 255,
10212 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10213 InputLightClass::KEYBOARD_BACKLIGHT,
10214 .path = ""};
10215 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10216 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10217 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10218
10219 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10220 InputDeviceInfo info;
10221 controller.populateDeviceInfo(&info);
10222 std::vector<InputDeviceLightInfo> lights = info.getLights();
10223 ASSERT_EQ(1U, lights.size());
10224 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10225 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10226 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10227
10228 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10229 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10230}
10231
10232TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10233 RawLightInfo infoRed = {.id = 1,
10234 .name = "red",
10235 .maxBrightness = 255,
10236 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10237 .path = ""};
10238 RawLightInfo infoGreen = {.id = 2,
10239 .name = "green",
10240 .maxBrightness = 255,
10241 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10242 .path = ""};
10243 RawLightInfo infoBlue = {.id = 3,
10244 .name = "blue",
10245 .maxBrightness = 255,
10246 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10247 .path = ""};
10248 RawLightInfo infoGlobal = {.id = 3,
10249 .name = "global_keyboard_backlight",
10250 .maxBrightness = 255,
10251 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10252 InputLightClass::KEYBOARD_BACKLIGHT,
10253 .path = ""};
10254 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10255 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10256 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10257 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10258
10259 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10260 InputDeviceInfo info;
10261 controller.populateDeviceInfo(&info);
10262 std::vector<InputDeviceLightInfo> lights = info.getLights();
10263 ASSERT_EQ(1U, lights.size());
10264 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10265 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10266 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010267
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010268 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10269 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010270}
10271
10272TEST_F(LightControllerTest, MultiColorRGBLight) {
10273 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010274 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010275 .maxBrightness = 255,
10276 .flags = InputLightClass::BRIGHTNESS |
10277 InputLightClass::MULTI_INTENSITY |
10278 InputLightClass::MULTI_INDEX,
10279 .path = ""};
10280
10281 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10282
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010283 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010284 InputDeviceInfo info;
10285 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010286 std::vector<InputDeviceLightInfo> lights = info.getLights();
10287 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010288 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10289 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10290 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10291
10292 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10293 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10294}
10295
10296TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10297 RawLightInfo infoColor = {.id = 1,
10298 .name = "multi_color_keyboard_backlight",
10299 .maxBrightness = 255,
10300 .flags = InputLightClass::BRIGHTNESS |
10301 InputLightClass::MULTI_INTENSITY |
10302 InputLightClass::MULTI_INDEX |
10303 InputLightClass::KEYBOARD_BACKLIGHT,
10304 .path = ""};
10305
10306 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10307
10308 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10309 InputDeviceInfo info;
10310 controller.populateDeviceInfo(&info);
10311 std::vector<InputDeviceLightInfo> lights = info.getLights();
10312 ASSERT_EQ(1U, lights.size());
10313 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10314 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10315 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010316
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010317 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10318 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010319}
10320
10321TEST_F(LightControllerTest, PlayerIdLight) {
10322 RawLightInfo info1 = {.id = 1,
10323 .name = "player1",
10324 .maxBrightness = 255,
10325 .flags = InputLightClass::BRIGHTNESS,
10326 .path = ""};
10327 RawLightInfo info2 = {.id = 2,
10328 .name = "player2",
10329 .maxBrightness = 255,
10330 .flags = InputLightClass::BRIGHTNESS,
10331 .path = ""};
10332 RawLightInfo info3 = {.id = 3,
10333 .name = "player3",
10334 .maxBrightness = 255,
10335 .flags = InputLightClass::BRIGHTNESS,
10336 .path = ""};
10337 RawLightInfo info4 = {.id = 4,
10338 .name = "player4",
10339 .maxBrightness = 255,
10340 .flags = InputLightClass::BRIGHTNESS,
10341 .path = ""};
10342 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10343 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10344 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10345 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10346
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010347 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010348 InputDeviceInfo info;
10349 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010350 std::vector<InputDeviceLightInfo> lights = info.getLights();
10351 ASSERT_EQ(1U, lights.size());
10352 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010353 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10354 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010355
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010356 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10357 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10358 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010359}
10360
Michael Wrightd02c5b62014-02-10 15:10:22 -080010361} // namespace android