blob: 8972e9fcdfaa5458361ec872302f3ecb97060a4b [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
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006838TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6839 addConfigurationProperty("touch.deviceType", "touchScreen");
6840 prepareDisplay(DISPLAY_ORIENTATION_0);
6841 prepareButtons();
6842 prepareAxes(POSITION | PRESSURE);
6843 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6844
6845 // Set the initial state for the touch pointer.
6846 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6847 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6848 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6849 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6850
6851 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
6852 // state by reading the current axis values.
6853 mapper.reset(ARBITRARY_TIME);
6854
6855 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6856 // the recreated touch state to generate a down event.
6857 processSync(mapper);
6858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6859 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6860
6861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6862}
6863
lilinnan687e58f2022-07-19 16:00:50 +08006864TEST_F(SingleTouchInputMapperTest,
6865 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6866 addConfigurationProperty("touch.deviceType", "touchScreen");
6867 prepareDisplay(DISPLAY_ORIENTATION_0);
6868 prepareButtons();
6869 prepareAxes(POSITION);
6870 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6871 NotifyMotionArgs motionArgs;
6872
6873 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006874 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006875 processSync(mapper);
6876
6877 // We should receive a down event
6878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6879 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6880
6881 // Change display id
6882 clearViewports();
6883 prepareSecondaryDisplay(ViewportType::INTERNAL);
6884
6885 // We should receive a cancel event
6886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6887 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6888 // Then receive reset called
6889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6890}
6891
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006892TEST_F(SingleTouchInputMapperTest,
6893 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6894 addConfigurationProperty("touch.deviceType", "touchScreen");
6895 prepareDisplay(DISPLAY_ORIENTATION_0);
6896 prepareButtons();
6897 prepareAxes(POSITION);
6898 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6900 NotifyMotionArgs motionArgs;
6901
6902 // Start a new gesture.
6903 processDown(mapper, 100, 200);
6904 processSync(mapper);
6905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6906 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6907
6908 // Make the viewport inactive. This will put the device in disabled mode.
6909 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6910 viewport->isActive = false;
6911 mFakePolicy->updateViewport(*viewport);
6912 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6913
6914 // We should receive a cancel event for the ongoing gesture.
6915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6916 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6917 // Then we should be notified that the device was reset.
6918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6919
6920 // No events are generated while the viewport is inactive.
6921 processMove(mapper, 101, 201);
6922 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006923 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006924 processSync(mapper);
6925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6926
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006927 // Start a new gesture while the viewport is still inactive.
6928 processDown(mapper, 300, 400);
6929 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6930 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6931 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6932 processSync(mapper);
6933
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006934 // Make the viewport active again. The device should resume processing events.
6935 viewport->isActive = true;
6936 mFakePolicy->updateViewport(*viewport);
6937 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6938
6939 // The device is reset because it changes back to direct mode, without generating any events.
6940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6942
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006943 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006944 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6946 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006947
6948 // No more events.
6949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6951}
6952
Prabir Pradhan5632d622021-09-06 07:57:20 -07006953// --- TouchDisplayProjectionTest ---
6954
6955class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6956public:
6957 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6958 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6959 // rotated equivalent of the given un-rotated physical display bounds.
6960 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6961 uint32_t inverseRotationFlags;
6962 auto width = DISPLAY_WIDTH;
6963 auto height = DISPLAY_HEIGHT;
6964 switch (orientation) {
6965 case DISPLAY_ORIENTATION_90:
6966 inverseRotationFlags = ui::Transform::ROT_270;
6967 std::swap(width, height);
6968 break;
6969 case DISPLAY_ORIENTATION_180:
6970 inverseRotationFlags = ui::Transform::ROT_180;
6971 break;
6972 case DISPLAY_ORIENTATION_270:
6973 inverseRotationFlags = ui::Transform::ROT_90;
6974 std::swap(width, height);
6975 break;
6976 case DISPLAY_ORIENTATION_0:
6977 inverseRotationFlags = ui::Transform::ROT_0;
6978 break;
6979 default:
6980 FAIL() << "Invalid orientation: " << orientation;
6981 }
6982
6983 const ui::Transform rotation(inverseRotationFlags, width, height);
6984 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6985
6986 std::optional<DisplayViewport> internalViewport =
6987 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6988 DisplayViewport& v = *internalViewport;
6989 v.displayId = DISPLAY_ID;
6990 v.orientation = orientation;
6991
6992 v.logicalLeft = 0;
6993 v.logicalTop = 0;
6994 v.logicalRight = 100;
6995 v.logicalBottom = 100;
6996
6997 v.physicalLeft = rotatedPhysicalDisplay.left;
6998 v.physicalTop = rotatedPhysicalDisplay.top;
6999 v.physicalRight = rotatedPhysicalDisplay.right;
7000 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7001
7002 v.deviceWidth = width;
7003 v.deviceHeight = height;
7004
7005 v.isActive = true;
7006 v.uniqueId = UNIQUE_ID;
7007 v.type = ViewportType::INTERNAL;
7008 mFakePolicy->updateViewport(v);
7009 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7010 }
7011
7012 void assertReceivedMove(const Point& point) {
7013 NotifyMotionArgs motionArgs;
7014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7015 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7016 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7017 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7018 1, 0, 0, 0, 0, 0, 0, 0));
7019 }
7020};
7021
7022TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7023 addConfigurationProperty("touch.deviceType", "touchScreen");
7024 prepareDisplay(DISPLAY_ORIENTATION_0);
7025
7026 prepareButtons();
7027 prepareAxes(POSITION);
7028 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7029
7030 NotifyMotionArgs motionArgs;
7031
7032 // Configure the DisplayViewport such that the logical display maps to a subsection of
7033 // the display panel called the physical display. Here, the physical display is bounded by the
7034 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7035 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7036 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7037 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7038
7039 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7040 DISPLAY_ORIENTATION_270}) {
7041 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7042
7043 // Touches outside the physical display should be ignored, and should not generate any
7044 // events. Ensure touches at the following points that lie outside of the physical display
7045 // area do not generate any events.
7046 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7047 processDown(mapper, toRawX(point.x), toRawY(point.y));
7048 processSync(mapper);
7049 processUp(mapper);
7050 processSync(mapper);
7051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7052 << "Unexpected event generated for touch outside physical display at point: "
7053 << point.x << ", " << point.y;
7054 }
7055 }
7056}
7057
7058TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7059 addConfigurationProperty("touch.deviceType", "touchScreen");
7060 prepareDisplay(DISPLAY_ORIENTATION_0);
7061
7062 prepareButtons();
7063 prepareAxes(POSITION);
7064 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7065
7066 NotifyMotionArgs motionArgs;
7067
7068 // Configure the DisplayViewport such that the logical display maps to a subsection of
7069 // the display panel called the physical display. Here, the physical display is bounded by the
7070 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7071 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7072
7073 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7074 DISPLAY_ORIENTATION_270}) {
7075 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7076
7077 // Touches that start outside the physical display should be ignored until it enters the
7078 // physical display bounds, at which point it should generate a down event. Start a touch at
7079 // the point (5, 100), which is outside the physical display bounds.
7080 static const Point kOutsidePoint{5, 100};
7081 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7082 processSync(mapper);
7083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7084
7085 // Move the touch into the physical display area. This should generate a pointer down.
7086 processMove(mapper, toRawX(11), toRawY(21));
7087 processSync(mapper);
7088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7089 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7090 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7091 ASSERT_NO_FATAL_FAILURE(
7092 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7093
7094 // Move the touch inside the physical display area. This should generate a pointer move.
7095 processMove(mapper, toRawX(69), toRawY(159));
7096 processSync(mapper);
7097 assertReceivedMove({69, 159});
7098
7099 // Move outside the physical display area. Since the pointer is already down, this should
7100 // now continue generating events.
7101 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7102 processSync(mapper);
7103 assertReceivedMove(kOutsidePoint);
7104
7105 // Release. This should generate a pointer up.
7106 processUp(mapper);
7107 processSync(mapper);
7108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7109 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7110 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7111 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7112
7113 // Ensure no more events were generated.
7114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7116 }
7117}
7118
Michael Wrightd02c5b62014-02-10 15:10:22 -08007119// --- MultiTouchInputMapperTest ---
7120
7121class MultiTouchInputMapperTest : public TouchInputMapperTest {
7122protected:
7123 void prepareAxes(int axes);
7124
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007125 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7126 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7127 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7128 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7129 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7130 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7131 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7132 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7133 void processId(MultiTouchInputMapper& mapper, int32_t id);
7134 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7135 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7136 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
7137 void processMTSync(MultiTouchInputMapper& mapper);
7138 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007139};
7140
7141void MultiTouchInputMapperTest::prepareAxes(int axes) {
7142 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007143 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7144 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007145 }
7146 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007147 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7148 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007149 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007150 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7151 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007152 }
7153 }
7154 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007155 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7156 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007157 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007158 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007159 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007160 }
7161 }
7162 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007163 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7164 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007165 }
7166 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007167 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7168 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007169 }
7170 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007171 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7172 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007173 }
7174 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007175 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7176 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007177 }
7178 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007179 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7180 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007181 }
7182 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007183 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007184 }
7185}
7186
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007187void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7188 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007189 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7190 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007191}
7192
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007193void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7194 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007195 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007196}
7197
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007198void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7199 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007200 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007201}
7202
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007203void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007204 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007205}
7206
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007207void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007208 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007209}
7210
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007211void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7212 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007213 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007214}
7215
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007216void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007217 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007218}
7219
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007220void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007221 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007222}
7223
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007224void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007225 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007226}
7227
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007228void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007229 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007230}
7231
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007232void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007233 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007234}
7235
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007236void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7237 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007238 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007239}
7240
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007241void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007242 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007243}
7244
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007245void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007246 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007247}
7248
Michael Wrightd02c5b62014-02-10 15:10:22 -08007249TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007250 addConfigurationProperty("touch.deviceType", "touchScreen");
7251 prepareDisplay(DISPLAY_ORIENTATION_0);
7252 prepareAxes(POSITION);
7253 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007254 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007255
arthurhungdcef2dc2020-08-11 14:47:50 +08007256 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007257
7258 NotifyMotionArgs motionArgs;
7259
7260 // Two fingers down at once.
7261 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7262 processPosition(mapper, x1, y1);
7263 processMTSync(mapper);
7264 processPosition(mapper, x2, y2);
7265 processMTSync(mapper);
7266 processSync(mapper);
7267
7268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7269 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7270 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7271 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7272 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7273 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7274 ASSERT_EQ(0, motionArgs.flags);
7275 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7276 ASSERT_EQ(0, motionArgs.buttonState);
7277 ASSERT_EQ(0, motionArgs.edgeFlags);
7278 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7279 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7280 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7282 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7283 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7284 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7285 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7286
7287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7288 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7289 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7290 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7291 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007292 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007293 ASSERT_EQ(0, motionArgs.flags);
7294 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7295 ASSERT_EQ(0, motionArgs.buttonState);
7296 ASSERT_EQ(0, motionArgs.edgeFlags);
7297 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7298 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7299 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7300 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7301 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7302 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7303 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7304 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7305 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7306 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7307 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7308 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7309
7310 // Move.
7311 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7312 processPosition(mapper, x1, y1);
7313 processMTSync(mapper);
7314 processPosition(mapper, x2, y2);
7315 processMTSync(mapper);
7316 processSync(mapper);
7317
7318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7319 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7320 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7321 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7322 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7323 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7324 ASSERT_EQ(0, motionArgs.flags);
7325 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7326 ASSERT_EQ(0, motionArgs.buttonState);
7327 ASSERT_EQ(0, motionArgs.edgeFlags);
7328 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7329 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7330 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7331 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7332 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7333 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7334 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7335 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7336 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7337 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7338 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7339 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7340
7341 // First finger up.
7342 x2 += 15; y2 -= 20;
7343 processPosition(mapper, x2, y2);
7344 processMTSync(mapper);
7345 processSync(mapper);
7346
7347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7348 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7349 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7350 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7351 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007352 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007353 ASSERT_EQ(0, motionArgs.flags);
7354 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7355 ASSERT_EQ(0, motionArgs.buttonState);
7356 ASSERT_EQ(0, motionArgs.edgeFlags);
7357 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7358 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7359 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7360 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7361 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7363 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7365 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7366 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7367 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7368 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7369
7370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7371 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7372 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7373 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7374 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7376 ASSERT_EQ(0, motionArgs.flags);
7377 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7378 ASSERT_EQ(0, motionArgs.buttonState);
7379 ASSERT_EQ(0, motionArgs.edgeFlags);
7380 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7381 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7382 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7384 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7385 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7386 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7387 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7388
7389 // Move.
7390 x2 += 20; y2 -= 25;
7391 processPosition(mapper, x2, y2);
7392 processMTSync(mapper);
7393 processSync(mapper);
7394
7395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7396 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7397 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7398 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7399 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7400 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7401 ASSERT_EQ(0, motionArgs.flags);
7402 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7403 ASSERT_EQ(0, motionArgs.buttonState);
7404 ASSERT_EQ(0, motionArgs.edgeFlags);
7405 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7406 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7407 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7408 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7409 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7410 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7411 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7412 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7413
7414 // New finger down.
7415 int32_t x3 = 700, y3 = 300;
7416 processPosition(mapper, x2, y2);
7417 processMTSync(mapper);
7418 processPosition(mapper, x3, y3);
7419 processMTSync(mapper);
7420 processSync(mapper);
7421
7422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7423 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7424 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7425 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7426 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007427 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007428 ASSERT_EQ(0, motionArgs.flags);
7429 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7430 ASSERT_EQ(0, motionArgs.buttonState);
7431 ASSERT_EQ(0, motionArgs.edgeFlags);
7432 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7433 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7434 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7435 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7436 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7437 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7438 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7439 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7440 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7441 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7442 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7443 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7444
7445 // Second finger up.
7446 x3 += 30; y3 -= 20;
7447 processPosition(mapper, x3, y3);
7448 processMTSync(mapper);
7449 processSync(mapper);
7450
7451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7452 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7453 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7454 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7455 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007456 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007457 ASSERT_EQ(0, motionArgs.flags);
7458 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7459 ASSERT_EQ(0, motionArgs.buttonState);
7460 ASSERT_EQ(0, motionArgs.edgeFlags);
7461 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7462 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7463 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7464 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7465 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7466 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7467 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7468 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7469 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7470 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7471 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7472 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7473
7474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7475 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7476 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7477 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7478 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7480 ASSERT_EQ(0, motionArgs.flags);
7481 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7482 ASSERT_EQ(0, motionArgs.buttonState);
7483 ASSERT_EQ(0, motionArgs.edgeFlags);
7484 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7485 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7486 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7488 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7489 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7490 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7491 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7492
7493 // Last finger up.
7494 processMTSync(mapper);
7495 processSync(mapper);
7496
7497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7498 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7499 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7500 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7501 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7502 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7503 ASSERT_EQ(0, motionArgs.flags);
7504 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7505 ASSERT_EQ(0, motionArgs.buttonState);
7506 ASSERT_EQ(0, motionArgs.edgeFlags);
7507 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7508 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7509 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7511 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7512 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7513 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7514 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7515
7516 // Should not have sent any more keys or motions.
7517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7519}
7520
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007521TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7522 addConfigurationProperty("touch.deviceType", "touchScreen");
7523 prepareDisplay(DISPLAY_ORIENTATION_0);
7524
7525 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7526 /*fuzz*/ 0, /*resolution*/ 10);
7527 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7528 /*fuzz*/ 0, /*resolution*/ 11);
7529 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7530 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7531 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7532 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7533 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7534 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7535 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7536 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7537
7538 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7539
7540 // X and Y axes
7541 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7542 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7543 // Touch major and minor
7544 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7545 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7546 // Tool major and minor
7547 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7548 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7549}
7550
7551TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7552 addConfigurationProperty("touch.deviceType", "touchScreen");
7553 prepareDisplay(DISPLAY_ORIENTATION_0);
7554
7555 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7556 /*fuzz*/ 0, /*resolution*/ 10);
7557 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7558 /*fuzz*/ 0, /*resolution*/ 11);
7559
7560 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7561
7562 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7563
7564 // Touch major and minor
7565 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7566 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7567 // Tool major and minor
7568 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7569 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7570}
7571
Michael Wrightd02c5b62014-02-10 15:10:22 -08007572TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007573 addConfigurationProperty("touch.deviceType", "touchScreen");
7574 prepareDisplay(DISPLAY_ORIENTATION_0);
7575 prepareAxes(POSITION | ID);
7576 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007577 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007578
arthurhungdcef2dc2020-08-11 14:47:50 +08007579 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007580
7581 NotifyMotionArgs motionArgs;
7582
7583 // Two fingers down at once.
7584 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7585 processPosition(mapper, x1, y1);
7586 processId(mapper, 1);
7587 processMTSync(mapper);
7588 processPosition(mapper, x2, y2);
7589 processId(mapper, 2);
7590 processMTSync(mapper);
7591 processSync(mapper);
7592
7593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7594 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7595 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7596 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7597 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7599 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7600
7601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007602 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007603 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7604 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7605 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7606 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7607 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7608 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7609 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7611 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7612
7613 // Move.
7614 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7615 processPosition(mapper, x1, y1);
7616 processId(mapper, 1);
7617 processMTSync(mapper);
7618 processPosition(mapper, x2, y2);
7619 processId(mapper, 2);
7620 processMTSync(mapper);
7621 processSync(mapper);
7622
7623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7624 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7625 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7626 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7627 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7628 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7629 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7631 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7632 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7633 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7634
7635 // First finger up.
7636 x2 += 15; y2 -= 20;
7637 processPosition(mapper, x2, y2);
7638 processId(mapper, 2);
7639 processMTSync(mapper);
7640 processSync(mapper);
7641
7642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007643 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007644 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7645 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7646 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7647 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7648 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7649 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7650 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7651 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7652 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7653
7654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7655 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7656 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7657 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7658 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7660 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7661
7662 // Move.
7663 x2 += 20; y2 -= 25;
7664 processPosition(mapper, x2, y2);
7665 processId(mapper, 2);
7666 processMTSync(mapper);
7667 processSync(mapper);
7668
7669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7670 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7671 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7672 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7673 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7674 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7675 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7676
7677 // New finger down.
7678 int32_t x3 = 700, y3 = 300;
7679 processPosition(mapper, x2, y2);
7680 processId(mapper, 2);
7681 processMTSync(mapper);
7682 processPosition(mapper, x3, y3);
7683 processId(mapper, 3);
7684 processMTSync(mapper);
7685 processSync(mapper);
7686
7687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007688 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007689 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7690 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7691 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7692 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7693 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7694 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7695 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7697 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7698
7699 // Second finger up.
7700 x3 += 30; y3 -= 20;
7701 processPosition(mapper, x3, y3);
7702 processId(mapper, 3);
7703 processMTSync(mapper);
7704 processSync(mapper);
7705
7706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007707 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007708 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7709 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7710 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7711 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7712 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7713 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7714 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7715 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7716 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7717
7718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7719 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7720 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7721 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7722 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7723 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7724 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7725
7726 // Last finger up.
7727 processMTSync(mapper);
7728 processSync(mapper);
7729
7730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7731 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7732 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7733 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7734 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7735 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7736 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7737
7738 // Should not have sent any more keys or motions.
7739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7741}
7742
7743TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007744 addConfigurationProperty("touch.deviceType", "touchScreen");
7745 prepareDisplay(DISPLAY_ORIENTATION_0);
7746 prepareAxes(POSITION | ID | SLOT);
7747 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007748 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007749
arthurhungdcef2dc2020-08-11 14:47:50 +08007750 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007751
7752 NotifyMotionArgs motionArgs;
7753
7754 // Two fingers down at once.
7755 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7756 processPosition(mapper, x1, y1);
7757 processId(mapper, 1);
7758 processSlot(mapper, 1);
7759 processPosition(mapper, x2, y2);
7760 processId(mapper, 2);
7761 processSync(mapper);
7762
7763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7764 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7765 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7766 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7767 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7769 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7770
7771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007772 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007773 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7774 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7775 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7776 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7777 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7778 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7779 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7780 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7781 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7782
7783 // Move.
7784 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7785 processSlot(mapper, 0);
7786 processPosition(mapper, x1, y1);
7787 processSlot(mapper, 1);
7788 processPosition(mapper, x2, y2);
7789 processSync(mapper);
7790
7791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7792 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7793 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7794 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7795 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7796 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7797 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7798 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7799 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7800 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7801 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7802
7803 // First finger up.
7804 x2 += 15; y2 -= 20;
7805 processSlot(mapper, 0);
7806 processId(mapper, -1);
7807 processSlot(mapper, 1);
7808 processPosition(mapper, x2, y2);
7809 processSync(mapper);
7810
7811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007812 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007813 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7814 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7815 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7816 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7817 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7818 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7819 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7820 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7821 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7822
7823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7824 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7825 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7826 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7827 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7828 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7829 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7830
7831 // Move.
7832 x2 += 20; y2 -= 25;
7833 processPosition(mapper, x2, y2);
7834 processSync(mapper);
7835
7836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7837 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7838 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7839 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7840 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7842 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7843
7844 // New finger down.
7845 int32_t x3 = 700, y3 = 300;
7846 processPosition(mapper, x2, y2);
7847 processSlot(mapper, 0);
7848 processId(mapper, 3);
7849 processPosition(mapper, x3, y3);
7850 processSync(mapper);
7851
7852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007853 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007854 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7855 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7856 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7857 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7858 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7859 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7860 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7861 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7862 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7863
7864 // Second finger up.
7865 x3 += 30; y3 -= 20;
7866 processSlot(mapper, 1);
7867 processId(mapper, -1);
7868 processSlot(mapper, 0);
7869 processPosition(mapper, x3, y3);
7870 processSync(mapper);
7871
7872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007873 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007874 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7875 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7876 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7877 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7878 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7879 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7880 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7881 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7882 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7883
7884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7885 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7886 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7887 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7888 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7889 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7890 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7891
7892 // Last finger up.
7893 processId(mapper, -1);
7894 processSync(mapper);
7895
7896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7897 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7898 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7899 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7900 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7901 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7902 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7903
7904 // Should not have sent any more keys or motions.
7905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7907}
7908
7909TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007910 addConfigurationProperty("touch.deviceType", "touchScreen");
7911 prepareDisplay(DISPLAY_ORIENTATION_0);
7912 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007913 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007914
7915 // These calculations are based on the input device calibration documentation.
7916 int32_t rawX = 100;
7917 int32_t rawY = 200;
7918 int32_t rawTouchMajor = 7;
7919 int32_t rawTouchMinor = 6;
7920 int32_t rawToolMajor = 9;
7921 int32_t rawToolMinor = 8;
7922 int32_t rawPressure = 11;
7923 int32_t rawDistance = 0;
7924 int32_t rawOrientation = 3;
7925 int32_t id = 5;
7926
7927 float x = toDisplayX(rawX);
7928 float y = toDisplayY(rawY);
7929 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7930 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7931 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7932 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7933 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7934 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7935 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7936 float distance = float(rawDistance);
7937
7938 processPosition(mapper, rawX, rawY);
7939 processTouchMajor(mapper, rawTouchMajor);
7940 processTouchMinor(mapper, rawTouchMinor);
7941 processToolMajor(mapper, rawToolMajor);
7942 processToolMinor(mapper, rawToolMinor);
7943 processPressure(mapper, rawPressure);
7944 processOrientation(mapper, rawOrientation);
7945 processDistance(mapper, rawDistance);
7946 processId(mapper, id);
7947 processMTSync(mapper);
7948 processSync(mapper);
7949
7950 NotifyMotionArgs args;
7951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7952 ASSERT_EQ(0, args.pointerProperties[0].id);
7953 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7954 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7955 orientation, distance));
7956}
7957
7958TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007959 addConfigurationProperty("touch.deviceType", "touchScreen");
7960 prepareDisplay(DISPLAY_ORIENTATION_0);
7961 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7962 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007963 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007964
7965 // These calculations are based on the input device calibration documentation.
7966 int32_t rawX = 100;
7967 int32_t rawY = 200;
7968 int32_t rawTouchMajor = 140;
7969 int32_t rawTouchMinor = 120;
7970 int32_t rawToolMajor = 180;
7971 int32_t rawToolMinor = 160;
7972
7973 float x = toDisplayX(rawX);
7974 float y = toDisplayY(rawY);
7975 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7976 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7977 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7978 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7979 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7980
7981 processPosition(mapper, rawX, rawY);
7982 processTouchMajor(mapper, rawTouchMajor);
7983 processTouchMinor(mapper, rawTouchMinor);
7984 processToolMajor(mapper, rawToolMajor);
7985 processToolMinor(mapper, rawToolMinor);
7986 processMTSync(mapper);
7987 processSync(mapper);
7988
7989 NotifyMotionArgs args;
7990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7991 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7992 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7993}
7994
7995TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007996 addConfigurationProperty("touch.deviceType", "touchScreen");
7997 prepareDisplay(DISPLAY_ORIENTATION_0);
7998 prepareAxes(POSITION | TOUCH | TOOL);
7999 addConfigurationProperty("touch.size.calibration", "diameter");
8000 addConfigurationProperty("touch.size.scale", "10");
8001 addConfigurationProperty("touch.size.bias", "160");
8002 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008003 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008004
8005 // These calculations are based on the input device calibration documentation.
8006 // Note: We only provide a single common touch/tool value because the device is assumed
8007 // not to emit separate values for each pointer (isSummed = 1).
8008 int32_t rawX = 100;
8009 int32_t rawY = 200;
8010 int32_t rawX2 = 150;
8011 int32_t rawY2 = 250;
8012 int32_t rawTouchMajor = 5;
8013 int32_t rawToolMajor = 8;
8014
8015 float x = toDisplayX(rawX);
8016 float y = toDisplayY(rawY);
8017 float x2 = toDisplayX(rawX2);
8018 float y2 = toDisplayY(rawY2);
8019 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8020 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8021 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8022
8023 processPosition(mapper, rawX, rawY);
8024 processTouchMajor(mapper, rawTouchMajor);
8025 processToolMajor(mapper, rawToolMajor);
8026 processMTSync(mapper);
8027 processPosition(mapper, rawX2, rawY2);
8028 processTouchMajor(mapper, rawTouchMajor);
8029 processToolMajor(mapper, rawToolMajor);
8030 processMTSync(mapper);
8031 processSync(mapper);
8032
8033 NotifyMotionArgs args;
8034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8035 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8036
8037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008038 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008039 ASSERT_EQ(size_t(2), args.pointerCount);
8040 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8041 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8042 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8043 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8044}
8045
8046TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008047 addConfigurationProperty("touch.deviceType", "touchScreen");
8048 prepareDisplay(DISPLAY_ORIENTATION_0);
8049 prepareAxes(POSITION | TOUCH | TOOL);
8050 addConfigurationProperty("touch.size.calibration", "area");
8051 addConfigurationProperty("touch.size.scale", "43");
8052 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008053 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008054
8055 // These calculations are based on the input device calibration documentation.
8056 int32_t rawX = 100;
8057 int32_t rawY = 200;
8058 int32_t rawTouchMajor = 5;
8059 int32_t rawToolMajor = 8;
8060
8061 float x = toDisplayX(rawX);
8062 float y = toDisplayY(rawY);
8063 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8064 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8065 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8066
8067 processPosition(mapper, rawX, rawY);
8068 processTouchMajor(mapper, rawTouchMajor);
8069 processToolMajor(mapper, rawToolMajor);
8070 processMTSync(mapper);
8071 processSync(mapper);
8072
8073 NotifyMotionArgs args;
8074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8075 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8076 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8077}
8078
8079TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008080 addConfigurationProperty("touch.deviceType", "touchScreen");
8081 prepareDisplay(DISPLAY_ORIENTATION_0);
8082 prepareAxes(POSITION | PRESSURE);
8083 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8084 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008085 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008086
Michael Wrightaa449c92017-12-13 21:21:43 +00008087 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008088 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008089 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8090 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8091 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8092
Michael Wrightd02c5b62014-02-10 15:10:22 -08008093 // These calculations are based on the input device calibration documentation.
8094 int32_t rawX = 100;
8095 int32_t rawY = 200;
8096 int32_t rawPressure = 60;
8097
8098 float x = toDisplayX(rawX);
8099 float y = toDisplayY(rawY);
8100 float pressure = float(rawPressure) * 0.01f;
8101
8102 processPosition(mapper, rawX, rawY);
8103 processPressure(mapper, rawPressure);
8104 processMTSync(mapper);
8105 processSync(mapper);
8106
8107 NotifyMotionArgs args;
8108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8109 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8110 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8111}
8112
8113TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008114 addConfigurationProperty("touch.deviceType", "touchScreen");
8115 prepareDisplay(DISPLAY_ORIENTATION_0);
8116 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008117 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008118
8119 NotifyMotionArgs motionArgs;
8120 NotifyKeyArgs keyArgs;
8121
8122 processId(mapper, 1);
8123 processPosition(mapper, 100, 200);
8124 processSync(mapper);
8125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8126 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8127 ASSERT_EQ(0, motionArgs.buttonState);
8128
8129 // press BTN_LEFT, release BTN_LEFT
8130 processKey(mapper, BTN_LEFT, 1);
8131 processSync(mapper);
8132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8134 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8135
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8137 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8138 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8139
Michael Wrightd02c5b62014-02-10 15:10:22 -08008140 processKey(mapper, BTN_LEFT, 0);
8141 processSync(mapper);
8142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008143 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008144 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008145
8146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008147 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008148 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008149
8150 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8151 processKey(mapper, BTN_RIGHT, 1);
8152 processKey(mapper, BTN_MIDDLE, 1);
8153 processSync(mapper);
8154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8155 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8156 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8157 motionArgs.buttonState);
8158
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8160 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8161 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8162
8163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8164 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8165 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8166 motionArgs.buttonState);
8167
Michael Wrightd02c5b62014-02-10 15:10:22 -08008168 processKey(mapper, BTN_RIGHT, 0);
8169 processSync(mapper);
8170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008171 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008172 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008173
8174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008175 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008176 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008177
8178 processKey(mapper, BTN_MIDDLE, 0);
8179 processSync(mapper);
8180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008181 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008182 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008183
8184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008185 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008186 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008187
8188 // press BTN_BACK, release BTN_BACK
8189 processKey(mapper, BTN_BACK, 1);
8190 processSync(mapper);
8191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8192 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8193 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008194
Michael Wrightd02c5b62014-02-10 15:10:22 -08008195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008196 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008197 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8198
8199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8200 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8201 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008202
8203 processKey(mapper, BTN_BACK, 0);
8204 processSync(mapper);
8205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008206 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008207 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008208
8209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008210 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008211 ASSERT_EQ(0, motionArgs.buttonState);
8212
Michael Wrightd02c5b62014-02-10 15:10:22 -08008213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8214 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8215 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8216
8217 // press BTN_SIDE, release BTN_SIDE
8218 processKey(mapper, BTN_SIDE, 1);
8219 processSync(mapper);
8220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8221 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8222 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008223
Michael Wrightd02c5b62014-02-10 15:10:22 -08008224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008225 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008226 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8227
8228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8229 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8230 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008231
8232 processKey(mapper, BTN_SIDE, 0);
8233 processSync(mapper);
8234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008235 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008236 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008237
8238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008239 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008240 ASSERT_EQ(0, motionArgs.buttonState);
8241
Michael Wrightd02c5b62014-02-10 15:10:22 -08008242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8243 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8244 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8245
8246 // press BTN_FORWARD, release BTN_FORWARD
8247 processKey(mapper, BTN_FORWARD, 1);
8248 processSync(mapper);
8249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8250 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8251 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008252
Michael Wrightd02c5b62014-02-10 15:10:22 -08008253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008254 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008255 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8256
8257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8258 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8259 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008260
8261 processKey(mapper, BTN_FORWARD, 0);
8262 processSync(mapper);
8263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008264 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008265 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008266
8267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008268 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008269 ASSERT_EQ(0, motionArgs.buttonState);
8270
Michael Wrightd02c5b62014-02-10 15:10:22 -08008271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8272 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8273 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8274
8275 // press BTN_EXTRA, release BTN_EXTRA
8276 processKey(mapper, BTN_EXTRA, 1);
8277 processSync(mapper);
8278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8279 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8280 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008281
Michael Wrightd02c5b62014-02-10 15:10:22 -08008282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008283 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008284 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8285
8286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8287 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8288 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008289
8290 processKey(mapper, BTN_EXTRA, 0);
8291 processSync(mapper);
8292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008293 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008294 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008295
8296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008298 ASSERT_EQ(0, motionArgs.buttonState);
8299
Michael Wrightd02c5b62014-02-10 15:10:22 -08008300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8301 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8302 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8303
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8305
Michael Wrightd02c5b62014-02-10 15:10:22 -08008306 // press BTN_STYLUS, release BTN_STYLUS
8307 processKey(mapper, BTN_STYLUS, 1);
8308 processSync(mapper);
8309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8310 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008311 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8312
8313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8314 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8315 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008316
8317 processKey(mapper, BTN_STYLUS, 0);
8318 processSync(mapper);
8319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008320 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008321 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008322
8323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008324 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008325 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008326
8327 // press BTN_STYLUS2, release BTN_STYLUS2
8328 processKey(mapper, BTN_STYLUS2, 1);
8329 processSync(mapper);
8330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8331 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008332 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8333
8334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8335 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8336 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008337
8338 processKey(mapper, BTN_STYLUS2, 0);
8339 processSync(mapper);
8340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008341 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008342 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008343
8344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008345 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008346 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008347
8348 // release touch
8349 processId(mapper, -1);
8350 processSync(mapper);
8351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8352 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8353 ASSERT_EQ(0, motionArgs.buttonState);
8354}
8355
8356TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008357 addConfigurationProperty("touch.deviceType", "touchScreen");
8358 prepareDisplay(DISPLAY_ORIENTATION_0);
8359 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008360 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008361
8362 NotifyMotionArgs motionArgs;
8363
8364 // default tool type is finger
8365 processId(mapper, 1);
8366 processPosition(mapper, 100, 200);
8367 processSync(mapper);
8368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8369 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8370 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8371
8372 // eraser
8373 processKey(mapper, BTN_TOOL_RUBBER, 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_ERASER, motionArgs.pointerProperties[0].toolType);
8378
8379 // stylus
8380 processKey(mapper, BTN_TOOL_RUBBER, 0);
8381 processKey(mapper, BTN_TOOL_PEN, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
8386
8387 // brush
8388 processKey(mapper, BTN_TOOL_PEN, 0);
8389 processKey(mapper, BTN_TOOL_BRUSH, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
8394
8395 // pencil
8396 processKey(mapper, BTN_TOOL_BRUSH, 0);
8397 processKey(mapper, BTN_TOOL_PENCIL, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
8402
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008403 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008404 processKey(mapper, BTN_TOOL_PENCIL, 0);
8405 processKey(mapper, BTN_TOOL_AIRBRUSH, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
8410
8411 // mouse
8412 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8413 processKey(mapper, BTN_TOOL_MOUSE, 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_MOUSE, motionArgs.pointerProperties[0].toolType);
8418
8419 // lens
8420 processKey(mapper, BTN_TOOL_MOUSE, 0);
8421 processKey(mapper, BTN_TOOL_LENS, 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_MOUSE, motionArgs.pointerProperties[0].toolType);
8426
8427 // double-tap
8428 processKey(mapper, BTN_TOOL_LENS, 0);
8429 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8430 processSync(mapper);
8431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8432 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8433 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8434
8435 // triple-tap
8436 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8437 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8438 processSync(mapper);
8439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8440 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8441 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8442
8443 // quad-tap
8444 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8445 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8446 processSync(mapper);
8447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8448 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8449 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8450
8451 // finger
8452 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8453 processKey(mapper, BTN_TOOL_FINGER, 1);
8454 processSync(mapper);
8455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8456 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8457 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8458
8459 // stylus trumps finger
8460 processKey(mapper, BTN_TOOL_PEN, 1);
8461 processSync(mapper);
8462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8463 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8464 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8465
8466 // eraser trumps stylus
8467 processKey(mapper, BTN_TOOL_RUBBER, 1);
8468 processSync(mapper);
8469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8470 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8471 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8472
8473 // mouse trumps eraser
8474 processKey(mapper, BTN_TOOL_MOUSE, 1);
8475 processSync(mapper);
8476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8477 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8478 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8479
8480 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8481 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8482 processSync(mapper);
8483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8484 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8485 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8486
8487 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8488 processToolType(mapper, MT_TOOL_PEN);
8489 processSync(mapper);
8490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8491 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8492 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8493
8494 // back to default tool type
8495 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8496 processKey(mapper, BTN_TOOL_MOUSE, 0);
8497 processKey(mapper, BTN_TOOL_RUBBER, 0);
8498 processKey(mapper, BTN_TOOL_PEN, 0);
8499 processKey(mapper, BTN_TOOL_FINGER, 0);
8500 processSync(mapper);
8501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8502 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8503 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8504}
8505
8506TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008507 addConfigurationProperty("touch.deviceType", "touchScreen");
8508 prepareDisplay(DISPLAY_ORIENTATION_0);
8509 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008510 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008511 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008512
8513 NotifyMotionArgs motionArgs;
8514
8515 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8516 processId(mapper, 1);
8517 processPosition(mapper, 100, 200);
8518 processSync(mapper);
8519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8520 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8521 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8522 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8523
8524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8525 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8526 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8527 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8528
8529 // move a little
8530 processPosition(mapper, 150, 250);
8531 processSync(mapper);
8532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8533 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8535 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8536
8537 // down when BTN_TOUCH is pressed, pressure defaults to 1
8538 processKey(mapper, BTN_TOUCH, 1);
8539 processSync(mapper);
8540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8541 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8543 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8544
8545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8546 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8547 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8548 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8549
8550 // up when BTN_TOUCH is released, hover restored
8551 processKey(mapper, BTN_TOUCH, 0);
8552 processSync(mapper);
8553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8554 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8556 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8557
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(150), toDisplayY(250), 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(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8567
8568 // exit hover when pointer goes away
8569 processId(mapper, -1);
8570 processSync(mapper);
8571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8572 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, 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
8577TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008578 addConfigurationProperty("touch.deviceType", "touchScreen");
8579 prepareDisplay(DISPLAY_ORIENTATION_0);
8580 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008581 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008582
8583 NotifyMotionArgs motionArgs;
8584
8585 // initially hovering because pressure is 0
8586 processId(mapper, 1);
8587 processPosition(mapper, 100, 200);
8588 processPressure(mapper, 0);
8589 processSync(mapper);
8590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8591 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8593 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8594
8595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8596 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8597 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8598 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8599
8600 // move a little
8601 processPosition(mapper, 150, 250);
8602 processSync(mapper);
8603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8604 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8606 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8607
8608 // down when pressure becomes non-zero
8609 processPressure(mapper, RAW_PRESSURE_MAX);
8610 processSync(mapper);
8611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8612 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8614 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8615
8616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8617 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8619 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8620
8621 // up when pressure becomes 0, hover restored
8622 processPressure(mapper, 0);
8623 processSync(mapper);
8624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8625 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8626 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8627 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8628
8629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8630 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8631 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8632 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8633
8634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8635 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8636 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8637 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8638
8639 // exit hover when pointer goes away
8640 processId(mapper, -1);
8641 processSync(mapper);
8642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8643 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8644 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8645 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8646}
8647
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008648/**
8649 * Set the input device port <--> display port associations, and check that the
8650 * events are routed to the display that matches the display port.
8651 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8652 */
8653TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008654 const std::string usb2 = "USB2";
8655 const uint8_t hdmi1 = 0;
8656 const uint8_t hdmi2 = 1;
8657 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008658 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008659
8660 addConfigurationProperty("touch.deviceType", "touchScreen");
8661 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008662 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008663
8664 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8665 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8666
8667 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8668 // for this input device is specified, and the matching viewport is not present,
8669 // the input device should be disabled (at the mapper level).
8670
8671 // Add viewport for display 2 on hdmi2
8672 prepareSecondaryDisplay(type, hdmi2);
8673 // Send a touch event
8674 processPosition(mapper, 100, 100);
8675 processSync(mapper);
8676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8677
8678 // Add viewport for display 1 on hdmi1
8679 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8680 // Send a touch event again
8681 processPosition(mapper, 100, 100);
8682 processSync(mapper);
8683
8684 NotifyMotionArgs args;
8685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8686 ASSERT_EQ(DISPLAY_ID, args.displayId);
8687}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008688
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008689TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8690 addConfigurationProperty("touch.deviceType", "touchScreen");
8691 prepareAxes(POSITION);
8692 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8693
8694 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8695
8696 prepareDisplay(DISPLAY_ORIENTATION_0);
8697 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8698
8699 // Send a touch event
8700 processPosition(mapper, 100, 100);
8701 processSync(mapper);
8702
8703 NotifyMotionArgs args;
8704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8705 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8706}
8707
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008708TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008709 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008710 std::shared_ptr<FakePointerController> fakePointerController =
8711 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008712 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008713 fakePointerController->setPosition(100, 200);
8714 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008715 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008716
Garfield Tan888a6a42020-01-09 11:39:16 -08008717 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008718 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008719
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008720 prepareDisplay(DISPLAY_ORIENTATION_0);
8721 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008722 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008723
8724 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008725 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008726
8727 NotifyMotionArgs motionArgs;
8728 processPosition(mapper, 100, 100);
8729 processSync(mapper);
8730
8731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8732 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8733 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8734}
8735
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008736/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008737 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8738 */
8739TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8740 addConfigurationProperty("touch.deviceType", "touchScreen");
8741 prepareAxes(POSITION);
8742 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8743
8744 prepareDisplay(DISPLAY_ORIENTATION_0);
8745 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8746 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8747 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8748 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8749
8750 NotifyMotionArgs args;
8751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8752 ASSERT_EQ(26, args.readTime);
8753
8754 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8755 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8756 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8757
8758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8759 ASSERT_EQ(33, args.readTime);
8760}
8761
8762/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008763 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8764 * events should not be delivered to the listener.
8765 */
8766TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8767 addConfigurationProperty("touch.deviceType", "touchScreen");
8768 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8769 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8770 ViewportType::INTERNAL);
8771 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8772 prepareAxes(POSITION);
8773 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8774
8775 NotifyMotionArgs motionArgs;
8776 processPosition(mapper, 100, 100);
8777 processSync(mapper);
8778
8779 mFakeListener->assertNotifyMotionWasNotCalled();
8780}
8781
Garfield Tanc734e4f2021-01-15 20:01:39 -08008782TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8783 addConfigurationProperty("touch.deviceType", "touchScreen");
8784 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8785 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8786 ViewportType::INTERNAL);
8787 std::optional<DisplayViewport> optionalDisplayViewport =
8788 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8789 ASSERT_TRUE(optionalDisplayViewport.has_value());
8790 DisplayViewport displayViewport = *optionalDisplayViewport;
8791
8792 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8793 prepareAxes(POSITION);
8794 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8795
8796 // Finger down
8797 int32_t x = 100, y = 100;
8798 processPosition(mapper, x, y);
8799 processSync(mapper);
8800
8801 NotifyMotionArgs motionArgs;
8802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8803 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8804
8805 // Deactivate display viewport
8806 displayViewport.isActive = false;
8807 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8808 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8809
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008810 // The ongoing touch should be canceled immediately
8811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8812 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8813
8814 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008815 x += 10, y += 10;
8816 processPosition(mapper, x, y);
8817 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008819
8820 // Reactivate display viewport
8821 displayViewport.isActive = true;
8822 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8823 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8824
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008825 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008826 x += 10, y += 10;
8827 processPosition(mapper, x, y);
8828 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8830 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008831}
8832
Arthur Hung7c645402019-01-25 17:45:42 +08008833TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8834 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008835 prepareAxes(POSITION | ID | SLOT);
8836 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008837 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008838
8839 // Create the second touch screen device, and enable multi fingers.
8840 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008841 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008842 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008843 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008844 std::shared_ptr<InputDevice> device2 =
8845 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008846 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008847
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008848 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8849 0 /*flat*/, 0 /*fuzz*/);
8850 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8851 0 /*flat*/, 0 /*fuzz*/);
8852 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8853 0 /*flat*/, 0 /*fuzz*/);
8854 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8855 0 /*flat*/, 0 /*fuzz*/);
8856 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8857 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8858 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008859
8860 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008861 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008862 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8863 device2->reset(ARBITRARY_TIME);
8864
8865 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008866 std::shared_ptr<FakePointerController> fakePointerController =
8867 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008868 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008869
8870 // Setup policy for associated displays and show touches.
8871 const uint8_t hdmi1 = 0;
8872 const uint8_t hdmi2 = 1;
8873 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8874 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8875 mFakePolicy->setShowTouches(true);
8876
8877 // Create displays.
8878 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008879 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008880
8881 // Default device will reconfigure above, need additional reconfiguration for another device.
8882 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan197e0862022-07-01 14:28:00 +00008883 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8884 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008885
8886 // Two fingers down at default display.
8887 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8888 processPosition(mapper, x1, y1);
8889 processId(mapper, 1);
8890 processSlot(mapper, 1);
8891 processPosition(mapper, x2, y2);
8892 processId(mapper, 2);
8893 processSync(mapper);
8894
8895 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8896 fakePointerController->getSpots().find(DISPLAY_ID);
8897 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8898 ASSERT_EQ(size_t(2), iter->second.size());
8899
8900 // Two fingers down at second display.
8901 processPosition(mapper2, x1, y1);
8902 processId(mapper2, 1);
8903 processSlot(mapper2, 1);
8904 processPosition(mapper2, x2, y2);
8905 processId(mapper2, 2);
8906 processSync(mapper2);
8907
8908 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8909 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8910 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008911
8912 // Disable the show touches configuration and ensure the spots are cleared.
8913 mFakePolicy->setShowTouches(false);
8914 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8915 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
8916
8917 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008918}
8919
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008920TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008921 prepareAxes(POSITION);
8922 addConfigurationProperty("touch.deviceType", "touchScreen");
8923 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008924 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008925
8926 NotifyMotionArgs motionArgs;
8927 // Unrotated video frame
8928 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8929 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008930 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008931 processPosition(mapper, 100, 200);
8932 processSync(mapper);
8933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8934 ASSERT_EQ(frames, motionArgs.videoFrames);
8935
8936 // Subsequent touch events should not have any videoframes
8937 // This is implemented separately in FakeEventHub,
8938 // but that should match the behaviour of TouchVideoDevice.
8939 processPosition(mapper, 200, 200);
8940 processSync(mapper);
8941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8942 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8943}
8944
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008945TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008946 prepareAxes(POSITION);
8947 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008948 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008949 // Unrotated video frame
8950 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8951 NotifyMotionArgs motionArgs;
8952
8953 // Test all 4 orientations
8954 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008955 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8956 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8957 clearViewports();
8958 prepareDisplay(orientation);
8959 std::vector<TouchVideoFrame> frames{frame};
8960 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8961 processPosition(mapper, 100, 200);
8962 processSync(mapper);
8963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8964 ASSERT_EQ(frames, motionArgs.videoFrames);
8965 }
8966}
8967
8968TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8969 prepareAxes(POSITION);
8970 addConfigurationProperty("touch.deviceType", "touchScreen");
8971 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8972 // orientation-aware are affected by display rotation.
8973 addConfigurationProperty("touch.orientationAware", "0");
8974 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8975 // Unrotated video frame
8976 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8977 NotifyMotionArgs motionArgs;
8978
8979 // Test all 4 orientations
8980 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008981 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8982 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8983 clearViewports();
8984 prepareDisplay(orientation);
8985 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008986 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008987 processPosition(mapper, 100, 200);
8988 processSync(mapper);
8989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008990 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8991 // compared to the display. This is so that when the window transform (which contains the
8992 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8993 // window's coordinate space.
8994 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008995 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08008996
8997 // Release finger.
8998 processSync(mapper);
8999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009000 }
9001}
9002
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009003TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009004 prepareAxes(POSITION);
9005 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009006 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009007 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9008 // so mix these.
9009 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9010 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9011 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9012 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9013 NotifyMotionArgs motionArgs;
9014
9015 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009016 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009017 processPosition(mapper, 100, 200);
9018 processSync(mapper);
9019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009020 ASSERT_EQ(frames, motionArgs.videoFrames);
9021}
9022
9023TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9024 prepareAxes(POSITION);
9025 addConfigurationProperty("touch.deviceType", "touchScreen");
9026 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9027 // orientation-aware are affected by display rotation.
9028 addConfigurationProperty("touch.orientationAware", "0");
9029 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9030 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9031 // so mix these.
9032 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9033 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9034 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9035 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9036 NotifyMotionArgs motionArgs;
9037
9038 prepareDisplay(DISPLAY_ORIENTATION_90);
9039 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9040 processPosition(mapper, 100, 200);
9041 processSync(mapper);
9042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9043 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9044 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9045 // compared to the display. This is so that when the window transform (which contains the
9046 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9047 // window's coordinate space.
9048 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
9049 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009050 ASSERT_EQ(frames, motionArgs.videoFrames);
9051}
9052
Arthur Hung9da14732019-09-02 16:16:58 +08009053/**
9054 * If we had defined port associations, but the viewport is not ready, the touch device would be
9055 * expected to be disabled, and it should be enabled after the viewport has found.
9056 */
9057TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009058 constexpr uint8_t hdmi2 = 1;
9059 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009060 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009061
9062 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9063
9064 addConfigurationProperty("touch.deviceType", "touchScreen");
9065 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009066 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009067
9068 ASSERT_EQ(mDevice->isEnabled(), false);
9069
9070 // Add display on hdmi2, the device should be enabled and can receive touch event.
9071 prepareSecondaryDisplay(type, hdmi2);
9072 ASSERT_EQ(mDevice->isEnabled(), true);
9073
9074 // Send a touch event.
9075 processPosition(mapper, 100, 100);
9076 processSync(mapper);
9077
9078 NotifyMotionArgs args;
9079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9080 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9081}
9082
Arthur Hung421eb1c2020-01-16 00:09:42 +08009083TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009084 addConfigurationProperty("touch.deviceType", "touchScreen");
9085 prepareDisplay(DISPLAY_ORIENTATION_0);
9086 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009087 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009088
9089 NotifyMotionArgs motionArgs;
9090
9091 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9092 // finger down
9093 processId(mapper, 1);
9094 processPosition(mapper, x1, y1);
9095 processSync(mapper);
9096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9097 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9098 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9099
9100 // finger move
9101 processId(mapper, 1);
9102 processPosition(mapper, x2, y2);
9103 processSync(mapper);
9104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9105 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9106 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9107
9108 // finger up.
9109 processId(mapper, -1);
9110 processSync(mapper);
9111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9112 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9113 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9114
9115 // new finger down
9116 processId(mapper, 1);
9117 processPosition(mapper, x3, y3);
9118 processSync(mapper);
9119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9120 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9121 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9122}
9123
9124/**
arthurhungcc7f9802020-04-30 17:55:40 +08009125 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9126 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009127 */
arthurhungcc7f9802020-04-30 17:55:40 +08009128TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009129 addConfigurationProperty("touch.deviceType", "touchScreen");
9130 prepareDisplay(DISPLAY_ORIENTATION_0);
9131 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009132 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009133
9134 NotifyMotionArgs motionArgs;
9135
9136 // default tool type is finger
9137 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009138 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009139 processPosition(mapper, x1, y1);
9140 processSync(mapper);
9141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9142 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9143 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9144
9145 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9146 processToolType(mapper, MT_TOOL_PALM);
9147 processSync(mapper);
9148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9149 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9150
9151 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009152 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009153 processPosition(mapper, x2, y2);
9154 processSync(mapper);
9155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9156
9157 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009158 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009159 processSync(mapper);
9160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9161
9162 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009163 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009164 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009165 processPosition(mapper, x3, y3);
9166 processSync(mapper);
9167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9168 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9169 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9170}
9171
arthurhungbf89a482020-04-17 17:37:55 +08009172/**
arthurhungcc7f9802020-04-30 17:55:40 +08009173 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9174 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009175 */
arthurhungcc7f9802020-04-30 17:55:40 +08009176TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009177 addConfigurationProperty("touch.deviceType", "touchScreen");
9178 prepareDisplay(DISPLAY_ORIENTATION_0);
9179 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9180 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9181
9182 NotifyMotionArgs motionArgs;
9183
9184 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009185 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9186 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009187 processPosition(mapper, x1, y1);
9188 processSync(mapper);
9189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9190 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9191 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9192
9193 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009194 processSlot(mapper, SECOND_SLOT);
9195 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009196 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009197 processSync(mapper);
9198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009199 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009200 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9201
9202 // If the tool type of the first finger changes to MT_TOOL_PALM,
9203 // we expect to receive ACTION_POINTER_UP with cancel flag.
9204 processSlot(mapper, FIRST_SLOT);
9205 processId(mapper, FIRST_TRACKING_ID);
9206 processToolType(mapper, MT_TOOL_PALM);
9207 processSync(mapper);
9208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009209 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009210 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9211
9212 // The following MOVE events of second finger should be processed.
9213 processSlot(mapper, SECOND_SLOT);
9214 processId(mapper, SECOND_TRACKING_ID);
9215 processPosition(mapper, x2 + 1, y2 + 1);
9216 processSync(mapper);
9217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9218 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9219 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9220
9221 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9222 // it. Second finger receive move.
9223 processSlot(mapper, FIRST_SLOT);
9224 processId(mapper, INVALID_TRACKING_ID);
9225 processSync(mapper);
9226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9227 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9228 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9229
9230 // Second finger keeps moving.
9231 processSlot(mapper, SECOND_SLOT);
9232 processId(mapper, SECOND_TRACKING_ID);
9233 processPosition(mapper, x2 + 2, y2 + 2);
9234 processSync(mapper);
9235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9237 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9238
9239 // Second finger up.
9240 processId(mapper, INVALID_TRACKING_ID);
9241 processSync(mapper);
9242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9243 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9244 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9245}
9246
9247/**
9248 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9249 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9250 */
9251TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9252 addConfigurationProperty("touch.deviceType", "touchScreen");
9253 prepareDisplay(DISPLAY_ORIENTATION_0);
9254 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9255 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9256
9257 NotifyMotionArgs motionArgs;
9258
9259 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9260 // First finger down.
9261 processId(mapper, FIRST_TRACKING_ID);
9262 processPosition(mapper, x1, y1);
9263 processSync(mapper);
9264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9265 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9266 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9267
9268 // Second finger down.
9269 processSlot(mapper, SECOND_SLOT);
9270 processId(mapper, SECOND_TRACKING_ID);
9271 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009272 processSync(mapper);
9273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009274 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009275 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9276
arthurhungcc7f9802020-04-30 17:55:40 +08009277 // If the tool type of the first finger changes to MT_TOOL_PALM,
9278 // we expect to receive ACTION_POINTER_UP with cancel flag.
9279 processSlot(mapper, FIRST_SLOT);
9280 processId(mapper, FIRST_TRACKING_ID);
9281 processToolType(mapper, MT_TOOL_PALM);
9282 processSync(mapper);
9283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009284 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009285 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9286
9287 // Second finger keeps moving.
9288 processSlot(mapper, SECOND_SLOT);
9289 processId(mapper, SECOND_TRACKING_ID);
9290 processPosition(mapper, x2 + 1, y2 + 1);
9291 processSync(mapper);
9292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9293 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9294
9295 // second finger becomes palm, receive cancel due to only 1 finger is active.
9296 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009297 processToolType(mapper, MT_TOOL_PALM);
9298 processSync(mapper);
9299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9300 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9301
arthurhungcc7f9802020-04-30 17:55:40 +08009302 // third finger down.
9303 processSlot(mapper, THIRD_SLOT);
9304 processId(mapper, THIRD_TRACKING_ID);
9305 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009306 processPosition(mapper, x3, y3);
9307 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9309 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9310 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009311 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9312
9313 // third finger move
9314 processId(mapper, THIRD_TRACKING_ID);
9315 processPosition(mapper, x3 + 1, y3 + 1);
9316 processSync(mapper);
9317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9318 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9319
9320 // first finger up, third finger receive move.
9321 processSlot(mapper, FIRST_SLOT);
9322 processId(mapper, INVALID_TRACKING_ID);
9323 processSync(mapper);
9324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9325 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9326 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9327
9328 // second finger up, third finger receive move.
9329 processSlot(mapper, SECOND_SLOT);
9330 processId(mapper, INVALID_TRACKING_ID);
9331 processSync(mapper);
9332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9334 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9335
9336 // third finger up.
9337 processSlot(mapper, THIRD_SLOT);
9338 processId(mapper, INVALID_TRACKING_ID);
9339 processSync(mapper);
9340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9341 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9342 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9343}
9344
9345/**
9346 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9347 * and the active finger could still be allowed to receive the events
9348 */
9349TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9350 addConfigurationProperty("touch.deviceType", "touchScreen");
9351 prepareDisplay(DISPLAY_ORIENTATION_0);
9352 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9353 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9354
9355 NotifyMotionArgs motionArgs;
9356
9357 // default tool type is finger
9358 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9359 processId(mapper, FIRST_TRACKING_ID);
9360 processPosition(mapper, x1, y1);
9361 processSync(mapper);
9362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9363 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9364 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9365
9366 // Second finger down.
9367 processSlot(mapper, SECOND_SLOT);
9368 processId(mapper, SECOND_TRACKING_ID);
9369 processPosition(mapper, x2, y2);
9370 processSync(mapper);
9371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009372 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009373 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9374
9375 // If the tool type of the second finger changes to MT_TOOL_PALM,
9376 // we expect to receive ACTION_POINTER_UP with cancel flag.
9377 processId(mapper, SECOND_TRACKING_ID);
9378 processToolType(mapper, MT_TOOL_PALM);
9379 processSync(mapper);
9380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009381 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009382 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9383
9384 // The following MOVE event should be processed.
9385 processSlot(mapper, FIRST_SLOT);
9386 processId(mapper, FIRST_TRACKING_ID);
9387 processPosition(mapper, x1 + 1, y1 + 1);
9388 processSync(mapper);
9389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9390 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9391 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9392
9393 // second finger up.
9394 processSlot(mapper, SECOND_SLOT);
9395 processId(mapper, INVALID_TRACKING_ID);
9396 processSync(mapper);
9397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9398 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9399
9400 // first finger keep moving
9401 processSlot(mapper, FIRST_SLOT);
9402 processId(mapper, FIRST_TRACKING_ID);
9403 processPosition(mapper, x1 + 2, y1 + 2);
9404 processSync(mapper);
9405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9406 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9407
9408 // first finger up.
9409 processId(mapper, INVALID_TRACKING_ID);
9410 processSync(mapper);
9411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9412 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9413 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009414}
9415
Arthur Hung9ad18942021-06-19 02:04:46 +00009416/**
9417 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9418 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9419 * cause slot be valid again.
9420 */
9421TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9422 addConfigurationProperty("touch.deviceType", "touchScreen");
9423 prepareDisplay(DISPLAY_ORIENTATION_0);
9424 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9425 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9426
9427 NotifyMotionArgs motionArgs;
9428
9429 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9430 // First finger down.
9431 processId(mapper, FIRST_TRACKING_ID);
9432 processPosition(mapper, x1, y1);
9433 processPressure(mapper, RAW_PRESSURE_MAX);
9434 processSync(mapper);
9435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9436 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9437 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9438
9439 // First finger move.
9440 processId(mapper, FIRST_TRACKING_ID);
9441 processPosition(mapper, x1 + 1, y1 + 1);
9442 processPressure(mapper, RAW_PRESSURE_MAX);
9443 processSync(mapper);
9444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9445 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9446 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9447
9448 // Second finger down.
9449 processSlot(mapper, SECOND_SLOT);
9450 processId(mapper, SECOND_TRACKING_ID);
9451 processPosition(mapper, x2, y2);
9452 processPressure(mapper, RAW_PRESSURE_MAX);
9453 processSync(mapper);
9454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009455 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009456 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9457
9458 // second finger up with some unexpected data.
9459 processSlot(mapper, SECOND_SLOT);
9460 processId(mapper, INVALID_TRACKING_ID);
9461 processPosition(mapper, x2, y2);
9462 processSync(mapper);
9463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009464 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009465 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9466
9467 // first finger up with some unexpected data.
9468 processSlot(mapper, FIRST_SLOT);
9469 processId(mapper, INVALID_TRACKING_ID);
9470 processPosition(mapper, x2, y2);
9471 processPressure(mapper, RAW_PRESSURE_MAX);
9472 processSync(mapper);
9473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9474 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9475 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9476}
9477
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009478TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9479 addConfigurationProperty("touch.deviceType", "touchScreen");
9480 prepareDisplay(DISPLAY_ORIENTATION_0);
9481 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9482 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9483
9484 // First finger down.
9485 processId(mapper, FIRST_TRACKING_ID);
9486 processPosition(mapper, 100, 200);
9487 processPressure(mapper, RAW_PRESSURE_MAX);
9488 processSync(mapper);
9489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9490 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9491
9492 // Second finger down.
9493 processSlot(mapper, SECOND_SLOT);
9494 processId(mapper, SECOND_TRACKING_ID);
9495 processPosition(mapper, 300, 400);
9496 processPressure(mapper, RAW_PRESSURE_MAX);
9497 processSync(mapper);
9498 ASSERT_NO_FATAL_FAILURE(
9499 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9500
9501 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
9502 // preserved. Resetting should not generate any events.
9503 mapper.reset(ARBITRARY_TIME);
9504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9505
9506 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9507 // the existing touch state to generate a down event.
9508 processPosition(mapper, 301, 302);
9509 processSync(mapper);
9510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9511 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
9512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9513 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
9514
9515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9516}
9517
9518TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9519 addConfigurationProperty("touch.deviceType", "touchScreen");
9520 prepareDisplay(DISPLAY_ORIENTATION_0);
9521 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9522 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9523
9524 // First finger touches down and releases.
9525 processId(mapper, FIRST_TRACKING_ID);
9526 processPosition(mapper, 100, 200);
9527 processPressure(mapper, RAW_PRESSURE_MAX);
9528 processSync(mapper);
9529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9530 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9531 processId(mapper, INVALID_TRACKING_ID);
9532 processSync(mapper);
9533 ASSERT_NO_FATAL_FAILURE(
9534 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9535
9536 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9537 // raw state where no pointers are down.
9538 mapper.reset(ARBITRARY_TIME);
9539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9540
9541 // Send an empty sync frame. Since there are no pointers, no events are generated.
9542 processSync(mapper);
9543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9544}
9545
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009546// --- MultiTouchInputMapperTest_ExternalDevice ---
9547
9548class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9549protected:
Chris Yea52ade12020-08-27 16:49:20 -07009550 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009551};
9552
9553/**
9554 * Expect fallback to internal viewport if device is external and external viewport is not present.
9555 */
9556TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9557 prepareAxes(POSITION);
9558 addConfigurationProperty("touch.deviceType", "touchScreen");
9559 prepareDisplay(DISPLAY_ORIENTATION_0);
9560 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9561
9562 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9563
9564 NotifyMotionArgs motionArgs;
9565
9566 // Expect the event to be sent to the internal viewport,
9567 // because an external viewport is not present.
9568 processPosition(mapper, 100, 100);
9569 processSync(mapper);
9570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9571 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9572
9573 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009574 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009575 processPosition(mapper, 100, 100);
9576 processSync(mapper);
9577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9578 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9579}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009580
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009581TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9582 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9583 std::shared_ptr<FakePointerController> fakePointerController =
9584 std::make_shared<FakePointerController>();
9585 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9586 fakePointerController->setPosition(0, 0);
9587 fakePointerController->setButtonState(0);
9588
9589 // prepare device and capture
9590 prepareDisplay(DISPLAY_ORIENTATION_0);
9591 prepareAxes(POSITION | ID | SLOT);
9592 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9593 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9594 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009595 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009596 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9597
9598 // captured touchpad should be a touchpad source
9599 NotifyDeviceResetArgs resetArgs;
9600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9601 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9602
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009603 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009604
9605 const InputDeviceInfo::MotionRange* relRangeX =
9606 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9607 ASSERT_NE(relRangeX, nullptr);
9608 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9609 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9610 const InputDeviceInfo::MotionRange* relRangeY =
9611 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9612 ASSERT_NE(relRangeY, nullptr);
9613 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9614 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9615
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009616 // run captured pointer tests - note that this is unscaled, so input listener events should be
9617 // identical to what the hardware sends (accounting for any
9618 // calibration).
9619 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009620 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009621 processId(mapper, 1);
9622 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9623 processKey(mapper, BTN_TOUCH, 1);
9624 processSync(mapper);
9625
9626 // expect coord[0] to contain initial location of touch 0
9627 NotifyMotionArgs args;
9628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9629 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9630 ASSERT_EQ(1U, args.pointerCount);
9631 ASSERT_EQ(0, args.pointerProperties[0].id);
9632 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9633 ASSERT_NO_FATAL_FAILURE(
9634 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9635
9636 // FINGER 1 DOWN
9637 processSlot(mapper, 1);
9638 processId(mapper, 2);
9639 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9640 processSync(mapper);
9641
9642 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009644 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009645 ASSERT_EQ(2U, args.pointerCount);
9646 ASSERT_EQ(0, args.pointerProperties[0].id);
9647 ASSERT_EQ(1, args.pointerProperties[1].id);
9648 ASSERT_NO_FATAL_FAILURE(
9649 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9650 ASSERT_NO_FATAL_FAILURE(
9651 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9652
9653 // FINGER 1 MOVE
9654 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9655 processSync(mapper);
9656
9657 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9658 // from move
9659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9660 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9661 ASSERT_NO_FATAL_FAILURE(
9662 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9663 ASSERT_NO_FATAL_FAILURE(
9664 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9665
9666 // FINGER 0 MOVE
9667 processSlot(mapper, 0);
9668 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9669 processSync(mapper);
9670
9671 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9673 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9674 ASSERT_NO_FATAL_FAILURE(
9675 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9676 ASSERT_NO_FATAL_FAILURE(
9677 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9678
9679 // BUTTON DOWN
9680 processKey(mapper, BTN_LEFT, 1);
9681 processSync(mapper);
9682
9683 // touchinputmapper design sends a move before button press
9684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9685 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9687 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9688
9689 // BUTTON UP
9690 processKey(mapper, BTN_LEFT, 0);
9691 processSync(mapper);
9692
9693 // touchinputmapper design sends a move after button release
9694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9695 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9697 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9698
9699 // FINGER 0 UP
9700 processId(mapper, -1);
9701 processSync(mapper);
9702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9703 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9704
9705 // FINGER 1 MOVE
9706 processSlot(mapper, 1);
9707 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9708 processSync(mapper);
9709
9710 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9712 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9713 ASSERT_EQ(1U, args.pointerCount);
9714 ASSERT_EQ(1, args.pointerProperties[0].id);
9715 ASSERT_NO_FATAL_FAILURE(
9716 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9717
9718 // FINGER 1 UP
9719 processId(mapper, -1);
9720 processKey(mapper, BTN_TOUCH, 0);
9721 processSync(mapper);
9722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9723 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9724
9725 // non captured touchpad should be a mouse source
9726 mFakePolicy->setPointerCapture(false);
9727 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9729 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9730}
9731
9732TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9733 std::shared_ptr<FakePointerController> fakePointerController =
9734 std::make_shared<FakePointerController>();
9735 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9736 fakePointerController->setPosition(0, 0);
9737 fakePointerController->setButtonState(0);
9738
9739 // prepare device and capture
9740 prepareDisplay(DISPLAY_ORIENTATION_0);
9741 prepareAxes(POSITION | ID | SLOT);
9742 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9743 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009744 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009745 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9746 // run uncaptured pointer tests - pushes out generic events
9747 // FINGER 0 DOWN
9748 processId(mapper, 3);
9749 processPosition(mapper, 100, 100);
9750 processKey(mapper, BTN_TOUCH, 1);
9751 processSync(mapper);
9752
9753 // start at (100,100), cursor should be at (0,0) * scale
9754 NotifyMotionArgs args;
9755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9756 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9757 ASSERT_NO_FATAL_FAILURE(
9758 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9759
9760 // FINGER 0 MOVE
9761 processPosition(mapper, 200, 200);
9762 processSync(mapper);
9763
9764 // compute scaling to help with touch position checking
9765 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9766 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9767 float scale =
9768 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9769
9770 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9772 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9774 0, 0, 0, 0, 0, 0, 0));
9775}
9776
9777TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9778 std::shared_ptr<FakePointerController> fakePointerController =
9779 std::make_shared<FakePointerController>();
9780
9781 prepareDisplay(DISPLAY_ORIENTATION_0);
9782 prepareAxes(POSITION | ID | SLOT);
9783 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009784 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009785 mFakePolicy->setPointerCapture(false);
9786 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9787
9788 // uncaptured touchpad should be a pointer device
9789 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9790
9791 // captured touchpad should be a touchpad device
9792 mFakePolicy->setPointerCapture(true);
9793 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9794 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9795}
9796
HQ Liue6983c72022-04-19 22:14:56 +00009797class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9798protected:
9799 float mPointerMovementScale;
9800 float mPointerXZoomScale;
9801 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9802 addConfigurationProperty("touch.deviceType", "pointer");
9803 std::shared_ptr<FakePointerController> fakePointerController =
9804 std::make_shared<FakePointerController>();
9805 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9806 fakePointerController->setPosition(0, 0);
9807 fakePointerController->setButtonState(0);
9808 prepareDisplay(DISPLAY_ORIENTATION_0);
9809
9810 prepareAxes(POSITION);
9811 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9812 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9813 // needs to be disabled, and the pointer gesture needs to be enabled.
9814 mFakePolicy->setPointerCapture(false);
9815 mFakePolicy->setPointerGestureEnabled(true);
9816 mFakePolicy->setPointerController(fakePointerController);
9817
9818 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9819 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9820 mPointerMovementScale =
9821 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9822 mPointerXZoomScale =
9823 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9824 }
9825
9826 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9827 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9828 /*flat*/ 0,
9829 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9830 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9831 /*flat*/ 0,
9832 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9833 }
9834};
9835
9836/**
9837 * Two fingers down on a pointer mode touch pad. The width
9838 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9839 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9840 * be greater than the both value to be freeform gesture, so that after two
9841 * fingers start to move downwards, the gesture should be swipe.
9842 */
9843TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9844 // The min freeform gesture width is 25units/mm x 30mm = 750
9845 // which is greater than fraction of the diagnal length of the touchpad (349).
9846 // Thus, MaxSwipWidth is 750.
9847 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9848 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9849 NotifyMotionArgs motionArgs;
9850
9851 // Two fingers down at once.
9852 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9853 // Pointer's initial position is used the [0,0] coordinate.
9854 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9855
9856 processId(mapper, FIRST_TRACKING_ID);
9857 processPosition(mapper, x1, y1);
9858 processMTSync(mapper);
9859 processId(mapper, SECOND_TRACKING_ID);
9860 processPosition(mapper, x2, y2);
9861 processMTSync(mapper);
9862 processSync(mapper);
9863
9864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9865 ASSERT_EQ(1U, motionArgs.pointerCount);
9866 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9867 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009868 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009869 ASSERT_NO_FATAL_FAILURE(
9870 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9871
9872 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9873 // that there should be 1 pointer.
9874 int32_t movingDistance = 200;
9875 y1 += movingDistance;
9876 y2 += movingDistance;
9877
9878 processId(mapper, FIRST_TRACKING_ID);
9879 processPosition(mapper, x1, y1);
9880 processMTSync(mapper);
9881 processId(mapper, SECOND_TRACKING_ID);
9882 processPosition(mapper, x2, y2);
9883 processMTSync(mapper);
9884 processSync(mapper);
9885
9886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9887 ASSERT_EQ(1U, motionArgs.pointerCount);
9888 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9889 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009890 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009891 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9892 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9893 0, 0, 0, 0));
9894}
9895
9896/**
9897 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9898 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9899 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9900 * value to be freeform gesture, so that after two fingers start to move downwards,
9901 * the gesture should be swipe.
9902 */
9903TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9904 // The min freeform gesture width is 5units/mm x 30mm = 150
9905 // which is greater than fraction of the diagnal length of the touchpad (349).
9906 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
9907 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
9908 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9909 NotifyMotionArgs motionArgs;
9910
9911 // Two fingers down at once.
9912 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9913 // Pointer's initial position is used the [0,0] coordinate.
9914 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9915
9916 processId(mapper, FIRST_TRACKING_ID);
9917 processPosition(mapper, x1, y1);
9918 processMTSync(mapper);
9919 processId(mapper, SECOND_TRACKING_ID);
9920 processPosition(mapper, x2, y2);
9921 processMTSync(mapper);
9922 processSync(mapper);
9923
9924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9925 ASSERT_EQ(1U, motionArgs.pointerCount);
9926 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9927 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009928 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009929 ASSERT_NO_FATAL_FAILURE(
9930 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9931
9932 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9933 // and there should be 1 pointer.
9934 int32_t movingDistance = 200;
9935 y1 += movingDistance;
9936 y2 += movingDistance;
9937
9938 processId(mapper, FIRST_TRACKING_ID);
9939 processPosition(mapper, x1, y1);
9940 processMTSync(mapper);
9941 processId(mapper, SECOND_TRACKING_ID);
9942 processPosition(mapper, x2, y2);
9943 processMTSync(mapper);
9944 processSync(mapper);
9945
9946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9947 ASSERT_EQ(1U, motionArgs.pointerCount);
9948 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9949 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009950 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009951 // New coordinate is the scaled relative coordinate from the initial coordinate.
9952 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9953 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9954 0, 0, 0, 0));
9955}
9956
9957/**
9958 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
9959 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
9960 * freeform gestures after two fingers start to move downwards.
9961 */
9962TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
9963 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9964 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9965
9966 NotifyMotionArgs motionArgs;
9967
9968 // Two fingers down at once. Wider than the max swipe width.
9969 // The gesture is expected to be PRESS, then transformed to FREEFORM
9970 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
9971
9972 processId(mapper, FIRST_TRACKING_ID);
9973 processPosition(mapper, x1, y1);
9974 processMTSync(mapper);
9975 processId(mapper, SECOND_TRACKING_ID);
9976 processPosition(mapper, x2, y2);
9977 processMTSync(mapper);
9978 processSync(mapper);
9979
9980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9981 ASSERT_EQ(1U, motionArgs.pointerCount);
9982 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9983 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009984 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009985 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
9986 ASSERT_NO_FATAL_FAILURE(
9987 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9988
9989 int32_t movingDistance = 200;
9990
9991 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
9992 // then two down events for two pointers.
9993 y1 += movingDistance;
9994 y2 += movingDistance;
9995
9996 processId(mapper, FIRST_TRACKING_ID);
9997 processPosition(mapper, x1, y1);
9998 processMTSync(mapper);
9999 processId(mapper, SECOND_TRACKING_ID);
10000 processPosition(mapper, x2, y2);
10001 processMTSync(mapper);
10002 processSync(mapper);
10003
10004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10005 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10006 ASSERT_EQ(1U, motionArgs.pointerCount);
10007 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10009 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10010 ASSERT_EQ(1U, motionArgs.pointerCount);
10011 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10013 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010014 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010015 ASSERT_EQ(2U, motionArgs.pointerCount);
10016 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
10017 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010018 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010019 // Two pointers' scaled relative coordinates from their initial centroid.
10020 // Initial y coordinates are 0 as y1 and y2 have the same value.
10021 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10022 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10023 // When pointers move, the new coordinates equal to the initial coordinates plus
10024 // scaled moving distance.
10025 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10026 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10027 0, 0, 0, 0));
10028 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10029 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10030 0, 0, 0, 0));
10031
10032 // Move two fingers down again, expect one MOVE motion event.
10033 y1 += movingDistance;
10034 y2 += movingDistance;
10035
10036 processId(mapper, FIRST_TRACKING_ID);
10037 processPosition(mapper, x1, y1);
10038 processMTSync(mapper);
10039 processId(mapper, SECOND_TRACKING_ID);
10040 processPosition(mapper, x2, y2);
10041 processMTSync(mapper);
10042 processSync(mapper);
10043
10044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10045 ASSERT_EQ(2U, motionArgs.pointerCount);
10046 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10047 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010048 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010049 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10050 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10051 0, 0, 0, 0, 0));
10052 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10053 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10054 0, 0, 0, 0, 0));
10055}
10056
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010057// --- JoystickInputMapperTest ---
10058
10059class JoystickInputMapperTest : public InputMapperTest {
10060protected:
10061 static const int32_t RAW_X_MIN;
10062 static const int32_t RAW_X_MAX;
10063 static const int32_t RAW_Y_MIN;
10064 static const int32_t RAW_Y_MAX;
10065
10066 void SetUp() override {
10067 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10068 }
10069 void prepareAxes() {
10070 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10071 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10072 }
10073
10074 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10075 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10076 }
10077
10078 void processSync(JoystickInputMapper& mapper) {
10079 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10080 }
10081
10082 void prepareVirtualDisplay(int32_t orientation) {
10083 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10084 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10085 NO_PORT, ViewportType::VIRTUAL);
10086 }
10087};
10088
10089const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10090const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10091const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10092const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10093
10094TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10095 prepareAxes();
10096 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10097
10098 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10099
10100 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
10101
10102 // Send an axis event
10103 processAxis(mapper, ABS_X, 100);
10104 processSync(mapper);
10105
10106 NotifyMotionArgs args;
10107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10108 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10109
10110 // Send another axis event
10111 processAxis(mapper, ABS_Y, 100);
10112 processSync(mapper);
10113
10114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10115 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10116}
10117
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010118// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010119
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010120class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010121protected:
10122 static const char* DEVICE_NAME;
10123 static const char* DEVICE_LOCATION;
10124 static const int32_t DEVICE_ID;
10125 static const int32_t DEVICE_GENERATION;
10126 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010127 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010128 static const int32_t EVENTHUB_ID;
10129
10130 std::shared_ptr<FakeEventHub> mFakeEventHub;
10131 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010132 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010133 std::unique_ptr<InstrumentedInputReader> mReader;
10134 std::shared_ptr<InputDevice> mDevice;
10135
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010136 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010137 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010138 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010139 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010140 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010141 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010142 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10143 }
10144
10145 void SetUp() override { SetUp(DEVICE_CLASSES); }
10146
10147 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010148 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010149 mFakePolicy.clear();
10150 }
10151
10152 void configureDevice(uint32_t changes) {
10153 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10154 mReader->requestRefreshConfiguration(changes);
10155 mReader->loopOnce();
10156 }
10157 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
10158 }
10159
10160 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10161 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010162 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010163 InputDeviceIdentifier identifier;
10164 identifier.name = name;
10165 identifier.location = location;
10166 std::shared_ptr<InputDevice> device =
10167 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10168 identifier);
10169 mReader->pushNextDevice(device);
10170 mFakeEventHub->addDevice(eventHubId, name, classes);
10171 mReader->loopOnce();
10172 return device;
10173 }
10174
10175 template <class T, typename... Args>
10176 T& addControllerAndConfigure(Args... args) {
10177 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10178
10179 return controller;
10180 }
10181};
10182
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010183const char* PeripheralControllerTest::DEVICE_NAME = "device";
10184const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10185const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10186const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10187const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010188const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10189 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010190const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010191
10192// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010193class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010194protected:
10195 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010196 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010197 }
10198};
10199
10200TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010201 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010202
10203 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
10204 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
10205}
10206
10207TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010208 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010209
10210 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
10211 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
10212}
10213
10214// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010215class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010216protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010217 void SetUp() override {
10218 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10219 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010220};
10221
Chris Ye85758332021-05-16 23:05:17 -070010222TEST_F(LightControllerTest, MonoLight) {
10223 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010224 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010225 .maxBrightness = 255,
10226 .flags = InputLightClass::BRIGHTNESS,
10227 .path = ""};
10228 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010229
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010230 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010231 InputDeviceInfo info;
10232 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010233 std::vector<InputDeviceLightInfo> lights = info.getLights();
10234 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010235 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10236 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10237
10238 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10239 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10240}
10241
10242TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10243 RawLightInfo infoMono = {.id = 1,
10244 .name = "mono_keyboard_backlight",
10245 .maxBrightness = 255,
10246 .flags = InputLightClass::BRIGHTNESS |
10247 InputLightClass::KEYBOARD_BACKLIGHT,
10248 .path = ""};
10249 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10250
10251 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10252 InputDeviceInfo info;
10253 controller.populateDeviceInfo(&info);
10254 std::vector<InputDeviceLightInfo> lights = info.getLights();
10255 ASSERT_EQ(1U, lights.size());
10256 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10257 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010258
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010259 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10260 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010261}
10262
10263TEST_F(LightControllerTest, RGBLight) {
10264 RawLightInfo infoRed = {.id = 1,
10265 .name = "red",
10266 .maxBrightness = 255,
10267 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10268 .path = ""};
10269 RawLightInfo infoGreen = {.id = 2,
10270 .name = "green",
10271 .maxBrightness = 255,
10272 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10273 .path = ""};
10274 RawLightInfo infoBlue = {.id = 3,
10275 .name = "blue",
10276 .maxBrightness = 255,
10277 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10278 .path = ""};
10279 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10280 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10281 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
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, CorrectRGBKeyboardBacklight) {
10297 RawLightInfo infoRed = {.id = 1,
10298 .name = "red_keyboard_backlight",
10299 .maxBrightness = 255,
10300 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10301 InputLightClass::KEYBOARD_BACKLIGHT,
10302 .path = ""};
10303 RawLightInfo infoGreen = {.id = 2,
10304 .name = "green_keyboard_backlight",
10305 .maxBrightness = 255,
10306 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10307 InputLightClass::KEYBOARD_BACKLIGHT,
10308 .path = ""};
10309 RawLightInfo infoBlue = {.id = 3,
10310 .name = "blue_keyboard_backlight",
10311 .maxBrightness = 255,
10312 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10313 InputLightClass::KEYBOARD_BACKLIGHT,
10314 .path = ""};
10315 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10316 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10317 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10318
10319 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10320 InputDeviceInfo info;
10321 controller.populateDeviceInfo(&info);
10322 std::vector<InputDeviceLightInfo> lights = info.getLights();
10323 ASSERT_EQ(1U, lights.size());
10324 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10325 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10326 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10327
10328 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10329 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10330}
10331
10332TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10333 RawLightInfo infoRed = {.id = 1,
10334 .name = "red",
10335 .maxBrightness = 255,
10336 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10337 .path = ""};
10338 RawLightInfo infoGreen = {.id = 2,
10339 .name = "green",
10340 .maxBrightness = 255,
10341 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10342 .path = ""};
10343 RawLightInfo infoBlue = {.id = 3,
10344 .name = "blue",
10345 .maxBrightness = 255,
10346 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10347 .path = ""};
10348 RawLightInfo infoGlobal = {.id = 3,
10349 .name = "global_keyboard_backlight",
10350 .maxBrightness = 255,
10351 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10352 InputLightClass::KEYBOARD_BACKLIGHT,
10353 .path = ""};
10354 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10355 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10356 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10357 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10358
10359 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10360 InputDeviceInfo info;
10361 controller.populateDeviceInfo(&info);
10362 std::vector<InputDeviceLightInfo> lights = info.getLights();
10363 ASSERT_EQ(1U, lights.size());
10364 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10365 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10366 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010367
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010368 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10369 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010370}
10371
10372TEST_F(LightControllerTest, MultiColorRGBLight) {
10373 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010374 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010375 .maxBrightness = 255,
10376 .flags = InputLightClass::BRIGHTNESS |
10377 InputLightClass::MULTI_INTENSITY |
10378 InputLightClass::MULTI_INDEX,
10379 .path = ""};
10380
10381 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10382
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010383 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010384 InputDeviceInfo info;
10385 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010386 std::vector<InputDeviceLightInfo> lights = info.getLights();
10387 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010388 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10389 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10390 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10391
10392 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10393 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10394}
10395
10396TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10397 RawLightInfo infoColor = {.id = 1,
10398 .name = "multi_color_keyboard_backlight",
10399 .maxBrightness = 255,
10400 .flags = InputLightClass::BRIGHTNESS |
10401 InputLightClass::MULTI_INTENSITY |
10402 InputLightClass::MULTI_INDEX |
10403 InputLightClass::KEYBOARD_BACKLIGHT,
10404 .path = ""};
10405
10406 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10407
10408 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10409 InputDeviceInfo info;
10410 controller.populateDeviceInfo(&info);
10411 std::vector<InputDeviceLightInfo> lights = info.getLights();
10412 ASSERT_EQ(1U, lights.size());
10413 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10414 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10415 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010416
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010417 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10418 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010419}
10420
10421TEST_F(LightControllerTest, PlayerIdLight) {
10422 RawLightInfo info1 = {.id = 1,
10423 .name = "player1",
10424 .maxBrightness = 255,
10425 .flags = InputLightClass::BRIGHTNESS,
10426 .path = ""};
10427 RawLightInfo info2 = {.id = 2,
10428 .name = "player2",
10429 .maxBrightness = 255,
10430 .flags = InputLightClass::BRIGHTNESS,
10431 .path = ""};
10432 RawLightInfo info3 = {.id = 3,
10433 .name = "player3",
10434 .maxBrightness = 255,
10435 .flags = InputLightClass::BRIGHTNESS,
10436 .path = ""};
10437 RawLightInfo info4 = {.id = 4,
10438 .name = "player4",
10439 .maxBrightness = 255,
10440 .flags = InputLightClass::BRIGHTNESS,
10441 .path = ""};
10442 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10443 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10444 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10445 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10446
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010447 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010448 InputDeviceInfo info;
10449 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010450 std::vector<InputDeviceLightInfo> lights = info.getLights();
10451 ASSERT_EQ(1U, lights.size());
10452 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010453 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10454 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010455
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010456 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10457 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10458 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010459}
10460
Michael Wrightd02c5b62014-02-10 15:10:22 -080010461} // namespace android