blob: ae98415bf8e105e17abf0cc8cc969a05d9ff6a2b [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>
34#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080035#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000036#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070037#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080038#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050039#include <gui/constants.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080040
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000041#include "android/hardware/input/InputDeviceCountryCode.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000042#include "input/DisplayViewport.h"
43#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010044
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000045using android::hardware::input::InputDeviceCountryCode;
46
Michael Wrightd02c5b62014-02-10 15:10:22 -080047namespace android {
48
Dominik Laskowski2f01d772022-03-23 16:01:29 -070049using namespace ftl::flag_operators;
50
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070051using std::chrono_literals::operator""ms;
52
53// Timeout for waiting for an expected event
54static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
55
Michael Wrightd02c5b62014-02-10 15:10:22 -080056// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000057static constexpr nsecs_t ARBITRARY_TIME = 1234;
58static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080059
60// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080061static constexpr int32_t DISPLAY_ID = 0;
62static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
63static constexpr int32_t DISPLAY_WIDTH = 480;
64static constexpr int32_t DISPLAY_HEIGHT = 800;
65static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
66static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
67static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070068static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070069static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080070
arthurhungcc7f9802020-04-30 17:55:40 +080071static constexpr int32_t FIRST_SLOT = 0;
72static constexpr int32_t SECOND_SLOT = 1;
73static constexpr int32_t THIRD_SLOT = 2;
74static constexpr int32_t INVALID_TRACKING_ID = -1;
75static constexpr int32_t FIRST_TRACKING_ID = 0;
76static constexpr int32_t SECOND_TRACKING_ID = 1;
77static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080078static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080079static constexpr int32_t BATTERY_STATUS = 4;
80static constexpr int32_t BATTERY_CAPACITY = 66;
Chris Ye3fdbfef2021-01-06 18:45:18 -080081static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
82static constexpr int32_t LIGHT_COLOR = 0x7F448866;
83static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080084
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080085static constexpr int32_t ACTION_POINTER_0_DOWN =
86 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
87static constexpr int32_t ACTION_POINTER_0_UP =
88 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
89static constexpr int32_t ACTION_POINTER_1_DOWN =
90 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
91static constexpr int32_t ACTION_POINTER_1_UP =
92 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
93
Michael Wrightd02c5b62014-02-10 15:10:22 -080094// Error tolerance for floating point assertions.
95static const float EPSILON = 0.001f;
96
97template<typename T>
98static inline T min(T a, T b) {
99 return a < b ? a : b;
100}
101
102static inline float avg(float x, float y) {
103 return (x + y) / 2;
104}
105
Chris Ye3fdbfef2021-01-06 18:45:18 -0800106// Mapping for light color name and the light color
107const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
108 {"green", LightColor::GREEN},
109 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800110
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700111static int32_t getInverseRotation(int32_t orientation) {
112 switch (orientation) {
113 case DISPLAY_ORIENTATION_90:
114 return DISPLAY_ORIENTATION_270;
115 case DISPLAY_ORIENTATION_270:
116 return DISPLAY_ORIENTATION_90;
117 default:
118 return orientation;
119 }
120}
121
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800122static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
123 InputDeviceInfo info;
124 mapper.populateDeviceInfo(&info);
125
126 const InputDeviceInfo::MotionRange* motionRange =
127 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
128 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
129}
130
131static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
132 InputDeviceInfo info;
133 mapper.populateDeviceInfo(&info);
134
135 const InputDeviceInfo::MotionRange* motionRange =
136 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
137 ASSERT_EQ(nullptr, motionRange);
138}
139
Michael Wrightd02c5b62014-02-10 15:10:22 -0800140// --- FakePointerController ---
141
142class FakePointerController : public PointerControllerInterface {
143 bool mHaveBounds;
144 float mMinX, mMinY, mMaxX, mMaxY;
145 float mX, mY;
146 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800147 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800148
Michael Wrightd02c5b62014-02-10 15:10:22 -0800149public:
150 FakePointerController() :
151 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800152 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800153 }
154
Michael Wright17db18e2020-06-26 20:51:44 +0100155 virtual ~FakePointerController() {}
156
Michael Wrightd02c5b62014-02-10 15:10:22 -0800157 void setBounds(float minX, float minY, float maxX, float maxY) {
158 mHaveBounds = true;
159 mMinX = minX;
160 mMinY = minY;
161 mMaxX = maxX;
162 mMaxY = maxY;
163 }
164
Chris Yea52ade12020-08-27 16:49:20 -0700165 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800166 mX = x;
167 mY = y;
168 }
169
Chris Yea52ade12020-08-27 16:49:20 -0700170 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171
Chris Yea52ade12020-08-27 16:49:20 -0700172 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800173
Chris Yea52ade12020-08-27 16:49:20 -0700174 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800175 *outX = mX;
176 *outY = mY;
177 }
178
Chris Yea52ade12020-08-27 16:49:20 -0700179 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800180
Chris Yea52ade12020-08-27 16:49:20 -0700181 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800182 mDisplayId = viewport.displayId;
183 }
184
Arthur Hung7c645402019-01-25 17:45:42 +0800185 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
186 return mSpotsByDisplay;
187 }
188
Michael Wrightd02c5b62014-02-10 15:10:22 -0800189private:
Chris Yea52ade12020-08-27 16:49:20 -0700190 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800191 *outMinX = mMinX;
192 *outMinY = mMinY;
193 *outMaxX = mMaxX;
194 *outMaxY = mMaxY;
195 return mHaveBounds;
196 }
197
Chris Yea52ade12020-08-27 16:49:20 -0700198 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800199 mX += deltaX;
200 if (mX < mMinX) mX = mMinX;
201 if (mX > mMaxX) mX = mMaxX;
202 mY += deltaY;
203 if (mY < mMinY) mY = mMinY;
204 if (mY > mMaxY) mY = mMaxY;
205 }
206
Chris Yea52ade12020-08-27 16:49:20 -0700207 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800208
Chris Yea52ade12020-08-27 16:49:20 -0700209 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800210
Chris Yea52ade12020-08-27 16:49:20 -0700211 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800212
Chris Yea52ade12020-08-27 16:49:20 -0700213 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
214 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800215 std::vector<int32_t> newSpots;
216 // Add spots for fingers that are down.
217 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
218 uint32_t id = idBits.clearFirstMarkedBit();
219 newSpots.push_back(id);
220 }
221
222 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800223 }
224
Prabir Pradhan197e0862022-07-01 14:28:00 +0000225 void clearSpots() override { mSpotsByDisplay.clear(); }
Arthur Hung7c645402019-01-25 17:45:42 +0800226
227 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800228};
229
230
231// --- FakeInputReaderPolicy ---
232
233class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700234 std::mutex mLock;
235 std::condition_variable mDevicesChangedCondition;
236
Michael Wrightd02c5b62014-02-10 15:10:22 -0800237 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000238 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700239 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
240 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100241 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700242 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243
244protected:
Chris Yea52ade12020-08-27 16:49:20 -0700245 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800246
247public:
248 FakeInputReaderPolicy() {
249 }
250
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700251 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800252 waitForInputDevices([](bool devicesChanged) {
253 if (!devicesChanged) {
254 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
255 }
256 });
257 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700258
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800259 void assertInputDevicesNotChanged() {
260 waitForInputDevices([](bool devicesChanged) {
261 if (devicesChanged) {
262 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
263 }
264 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700265 }
266
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700267 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100268 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100269 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700270 }
271
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700272 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
273 return mConfig.getDisplayViewportByUniqueId(uniqueId);
274 }
275 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
276 return mConfig.getDisplayViewportByType(type);
277 }
278
279 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
280 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700281 }
282
Prabir Pradhan5632d622021-09-06 07:57:20 -0700283 void addDisplayViewport(DisplayViewport viewport) {
284 mViewports.push_back(std::move(viewport));
285 mConfig.setDisplayViewports(mViewports);
286 }
287
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700288 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000289 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700290 std::optional<uint8_t> physicalPort, ViewportType type) {
291 const bool isRotated =
292 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
293 DisplayViewport v;
294 v.displayId = displayId;
295 v.orientation = orientation;
296 v.logicalLeft = 0;
297 v.logicalTop = 0;
298 v.logicalRight = isRotated ? height : width;
299 v.logicalBottom = isRotated ? width : height;
300 v.physicalLeft = 0;
301 v.physicalTop = 0;
302 v.physicalRight = isRotated ? height : width;
303 v.physicalBottom = isRotated ? width : height;
304 v.deviceWidth = isRotated ? height : width;
305 v.deviceHeight = isRotated ? width : height;
306 v.isActive = isActive;
307 v.uniqueId = uniqueId;
308 v.physicalPort = physicalPort;
309 v.type = type;
310
311 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800312 }
313
Arthur Hung6cd19a42019-08-30 19:04:12 +0800314 bool updateViewport(const DisplayViewport& viewport) {
315 size_t count = mViewports.size();
316 for (size_t i = 0; i < count; i++) {
317 const DisplayViewport& currentViewport = mViewports[i];
318 if (currentViewport.displayId == viewport.displayId) {
319 mViewports[i] = viewport;
320 mConfig.setDisplayViewports(mViewports);
321 return true;
322 }
323 }
324 // no viewport found.
325 return false;
326 }
327
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100328 void addExcludedDeviceName(const std::string& deviceName) {
329 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800330 }
331
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700332 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
333 mConfig.portAssociations.insert({inputPort, displayPort});
334 }
335
Christine Franks1ba71cc2021-04-07 14:37:42 -0700336 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
337 const std::string& displayUniqueId) {
338 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
339 }
340
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000341 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700342
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000343 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700344
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000345 void setPointerController(std::shared_ptr<FakePointerController> controller) {
346 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800347 }
348
349 const InputReaderConfiguration* getReaderConfiguration() const {
350 return &mConfig;
351 }
352
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800353 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800354 return mInputDevices;
355 }
356
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100357 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700358 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700359 return transform;
360 }
361
362 void setTouchAffineTransformation(const TouchAffineTransformation t) {
363 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800364 }
365
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000366 PointerCaptureRequest setPointerCapture(bool enabled) {
367 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
368 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800369 }
370
Arthur Hung7c645402019-01-25 17:45:42 +0800371 void setShowTouches(bool enabled) {
372 mConfig.showTouches = enabled;
373 }
374
Garfield Tan888a6a42020-01-09 11:39:16 -0800375 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
376 mConfig.defaultPointerDisplayId = pointerDisplayId;
377 }
378
HQ Liue6983c72022-04-19 22:14:56 +0000379 void setPointerGestureEnabled(bool enabled) { mConfig.pointerGesturesEnabled = enabled; }
380
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800381 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
382
HQ Liue6983c72022-04-19 22:14:56 +0000383 float getPointerGestureZoomSpeedRatio() { return mConfig.pointerGestureZoomSpeedRatio; }
384
Prabir Pradhanf99d6e72022-04-21 15:28:35 +0000385 void setVelocityControlParams(const VelocityControlParameters& params) {
386 mConfig.pointerVelocityControlParameters = params;
387 mConfig.wheelVelocityControlParameters = params;
388 }
389
Michael Wrightd02c5b62014-02-10 15:10:22 -0800390private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000391 uint32_t mNextPointerCaptureSequenceNumber = 0;
392
Chris Yea52ade12020-08-27 16:49:20 -0700393 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800394 *outConfig = mConfig;
395 }
396
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000397 std::shared_ptr<PointerControllerInterface> obtainPointerController(
398 int32_t /*deviceId*/) override {
399 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800400 }
401
Chris Yea52ade12020-08-27 16:49:20 -0700402 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700403 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800404 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700405 mInputDevicesChanged = true;
406 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800407 }
408
Chris Yea52ade12020-08-27 16:49:20 -0700409 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
410 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700411 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800412 }
413
Chris Yea52ade12020-08-27 16:49:20 -0700414 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800415
416 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
417 std::unique_lock<std::mutex> lock(mLock);
418 base::ScopedLockAssertion assumeLocked(mLock);
419
420 const bool devicesChanged =
421 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
422 return mInputDevicesChanged;
423 });
424 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
425 mInputDevicesChanged = false;
426 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800427};
428
Michael Wrightd02c5b62014-02-10 15:10:22 -0800429// --- FakeEventHub ---
430
431class FakeEventHub : public EventHubInterface {
432 struct KeyInfo {
433 int32_t keyCode;
434 uint32_t flags;
435 };
436
Chris Yef59a2f42020-10-16 12:55:26 -0700437 struct SensorInfo {
438 InputDeviceSensorType sensorType;
439 int32_t sensorDataIndex;
440 };
441
Michael Wrightd02c5b62014-02-10 15:10:22 -0800442 struct Device {
443 InputDeviceIdentifier identifier;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700444 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800445 PropertyMap configuration;
446 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
447 KeyedVector<int, bool> relativeAxes;
448 KeyedVector<int32_t, int32_t> keyCodeStates;
449 KeyedVector<int32_t, int32_t> scanCodeStates;
450 KeyedVector<int32_t, int32_t> switchStates;
451 KeyedVector<int32_t, int32_t> absoluteAxisValue;
452 KeyedVector<int32_t, KeyInfo> keysByScanCode;
453 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
454 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100455 // fake mapping which would normally come from keyCharacterMap
456 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700457 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
458 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800459 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700460 bool enabled;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000461 InputDeviceCountryCode countryCode;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700462
463 status_t enable() {
464 enabled = true;
465 return OK;
466 }
467
468 status_t disable() {
469 enabled = false;
470 return OK;
471 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800472
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700473 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800474 };
475
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700476 std::mutex mLock;
477 std::condition_variable mEventsCondition;
478
Michael Wrightd02c5b62014-02-10 15:10:22 -0800479 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100480 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000481 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600482 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000483 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800484 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
485 // Simulates a device light brightness, from light id to light brightness.
486 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
487 // Simulates a device light intensities, from light id to light intensities map.
488 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
489 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800490
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700491public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800492 virtual ~FakeEventHub() {
493 for (size_t i = 0; i < mDevices.size(); i++) {
494 delete mDevices.valueAt(i);
495 }
496 }
497
Michael Wrightd02c5b62014-02-10 15:10:22 -0800498 FakeEventHub() { }
499
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700500 void addDevice(int32_t deviceId, const std::string& name,
501 ftl::Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800502 Device* device = new Device(classes);
503 device->identifier.name = name;
504 mDevices.add(deviceId, device);
505
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000506 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800507 }
508
509 void removeDevice(int32_t deviceId) {
510 delete mDevices.valueFor(deviceId);
511 mDevices.removeItem(deviceId);
512
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000513 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800514 }
515
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000516 bool isDeviceEnabled(int32_t deviceId) const override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700517 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700518 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700519 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
520 return false;
521 }
522 return device->enabled;
523 }
524
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000525 status_t enableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700526 status_t result;
527 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700528 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700529 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
530 return BAD_VALUE;
531 }
532 if (device->enabled) {
533 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
534 return OK;
535 }
536 result = device->enable();
537 return result;
538 }
539
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000540 status_t disableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700541 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700542 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700543 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
544 return BAD_VALUE;
545 }
546 if (!device->enabled) {
547 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
548 return OK;
549 }
550 return device->disable();
551 }
552
Michael Wrightd02c5b62014-02-10 15:10:22 -0800553 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000554 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800555 }
556
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700557 void addConfigurationProperty(int32_t deviceId, const char* key, const char* value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800558 Device* device = getDevice(deviceId);
559 device->configuration.addProperty(key, value);
560 }
561
562 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
563 Device* device = getDevice(deviceId);
564 device->configuration.addAll(configuration);
565 }
566
567 void addAbsoluteAxis(int32_t deviceId, int axis,
568 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
569 Device* device = getDevice(deviceId);
570
571 RawAbsoluteAxisInfo info;
572 info.valid = true;
573 info.minValue = minValue;
574 info.maxValue = maxValue;
575 info.flat = flat;
576 info.fuzz = fuzz;
577 info.resolution = resolution;
578 device->absoluteAxes.add(axis, info);
579 }
580
581 void addRelativeAxis(int32_t deviceId, int32_t axis) {
582 Device* device = getDevice(deviceId);
583 device->relativeAxes.add(axis, true);
584 }
585
586 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
587 Device* device = getDevice(deviceId);
588 device->keyCodeStates.replaceValueFor(keyCode, state);
589 }
590
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000591 void setCountryCode(int32_t deviceId, InputDeviceCountryCode countryCode) {
592 Device* device = getDevice(deviceId);
593 device->countryCode = countryCode;
594 }
595
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
597 Device* device = getDevice(deviceId);
598 device->scanCodeStates.replaceValueFor(scanCode, state);
599 }
600
601 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
602 Device* device = getDevice(deviceId);
603 device->switchStates.replaceValueFor(switchCode, state);
604 }
605
606 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
607 Device* device = getDevice(deviceId);
608 device->absoluteAxisValue.replaceValueFor(axis, value);
609 }
610
611 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
612 int32_t keyCode, uint32_t flags) {
613 Device* device = getDevice(deviceId);
614 KeyInfo info;
615 info.keyCode = keyCode;
616 info.flags = flags;
617 if (scanCode) {
618 device->keysByScanCode.add(scanCode, info);
619 }
620 if (usageCode) {
621 device->keysByUsageCode.add(usageCode, info);
622 }
623 }
624
Philip Junker4af3b3d2021-12-14 10:36:55 +0100625 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
626 Device* device = getDevice(deviceId);
627 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
628 }
629
Michael Wrightd02c5b62014-02-10 15:10:22 -0800630 void addLed(int32_t deviceId, int32_t led, bool initialState) {
631 Device* device = getDevice(deviceId);
632 device->leds.add(led, initialState);
633 }
634
Chris Yef59a2f42020-10-16 12:55:26 -0700635 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
636 int32_t sensorDataIndex) {
637 Device* device = getDevice(deviceId);
638 SensorInfo info;
639 info.sensorType = sensorType;
640 info.sensorDataIndex = sensorDataIndex;
641 device->sensorsByAbsCode.emplace(absCode, info);
642 }
643
644 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
645 Device* device = getDevice(deviceId);
646 typename BitArray<MSC_MAX>::Buffer buffer;
647 buffer[mscEvent / 32] = 1 << mscEvent % 32;
648 device->mscBitmask.loadFromBuffer(buffer);
649 }
650
Chris Ye3fdbfef2021-01-06 18:45:18 -0800651 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
652 mRawLightInfos.emplace(rawId, std::move(info));
653 }
654
655 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
656 mLightBrightness.emplace(rawId, brightness);
657 }
658
659 void fakeLightIntensities(int32_t rawId,
660 const std::unordered_map<LightColor, int32_t> intensities) {
661 mLightIntensities.emplace(rawId, std::move(intensities));
662 }
663
Michael Wrightd02c5b62014-02-10 15:10:22 -0800664 bool getLedState(int32_t deviceId, int32_t led) {
665 Device* device = getDevice(deviceId);
666 return device->leds.valueFor(led);
667 }
668
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100669 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800670 return mExcludedDevices;
671 }
672
673 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
674 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800675 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800676 }
677
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000678 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
679 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700680 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800681 RawEvent event;
682 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000683 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800684 event.deviceId = deviceId;
685 event.type = type;
686 event.code = code;
687 event.value = value;
688 mEvents.push_back(event);
689
690 if (type == EV_ABS) {
691 setAbsoluteAxisValue(deviceId, code, value);
692 }
693 }
694
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600695 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
696 std::vector<TouchVideoFrame>> videoFrames) {
697 mVideoFrames = std::move(videoFrames);
698 }
699
Michael Wrightd02c5b62014-02-10 15:10:22 -0800700 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700701 std::unique_lock<std::mutex> lock(mLock);
702 base::ScopedLockAssertion assumeLocked(mLock);
703 const bool queueIsEmpty =
704 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
705 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
706 if (!queueIsEmpty) {
707 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
708 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800709 }
710
711private:
712 Device* getDevice(int32_t deviceId) const {
713 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100714 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800715 }
716
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700717 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800718 Device* device = getDevice(deviceId);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700719 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800720 }
721
Chris Yea52ade12020-08-27 16:49:20 -0700722 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800723 Device* device = getDevice(deviceId);
724 return device ? device->identifier : InputDeviceIdentifier();
725 }
726
Chris Yea52ade12020-08-27 16:49:20 -0700727 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800728
Chris Yea52ade12020-08-27 16:49:20 -0700729 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800730 Device* device = getDevice(deviceId);
731 if (device) {
732 *outConfiguration = device->configuration;
733 }
734 }
735
Chris Yea52ade12020-08-27 16:49:20 -0700736 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
737 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800738 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800739 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800740 ssize_t index = device->absoluteAxes.indexOfKey(axis);
741 if (index >= 0) {
742 *outAxisInfo = device->absoluteAxes.valueAt(index);
743 return OK;
744 }
745 }
746 outAxisInfo->clear();
747 return -1;
748 }
749
Chris Yea52ade12020-08-27 16:49:20 -0700750 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800751 Device* device = getDevice(deviceId);
752 if (device) {
753 return device->relativeAxes.indexOfKey(axis) >= 0;
754 }
755 return false;
756 }
757
Chris Yea52ade12020-08-27 16:49:20 -0700758 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800759
Chris Yef59a2f42020-10-16 12:55:26 -0700760 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
761 Device* device = getDevice(deviceId);
762 if (device) {
763 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
764 }
765 return false;
766 }
767
Chris Yea52ade12020-08-27 16:49:20 -0700768 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
769 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800770 Device* device = getDevice(deviceId);
771 if (device) {
772 const KeyInfo* key = getKey(device, scanCode, usageCode);
773 if (key) {
774 if (outKeycode) {
775 *outKeycode = key->keyCode;
776 }
777 if (outFlags) {
778 *outFlags = key->flags;
779 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700780 if (outMetaState) {
781 *outMetaState = metaState;
782 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800783 return OK;
784 }
785 }
786 return NAME_NOT_FOUND;
787 }
788
789 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
790 if (usageCode) {
791 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
792 if (index >= 0) {
793 return &device->keysByUsageCode.valueAt(index);
794 }
795 }
796 if (scanCode) {
797 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
798 if (index >= 0) {
799 return &device->keysByScanCode.valueAt(index);
800 }
801 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700802 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800803 }
804
Chris Yea52ade12020-08-27 16:49:20 -0700805 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800806
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000807 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
808 int32_t deviceId, int32_t absCode) const override {
Chris Yef59a2f42020-10-16 12:55:26 -0700809 Device* device = getDevice(deviceId);
810 if (!device) {
811 return Errorf("Sensor device not found.");
812 }
813 auto it = device->sensorsByAbsCode.find(absCode);
814 if (it == device->sensorsByAbsCode.end()) {
815 return Errorf("Sensor map not found.");
816 }
817 const SensorInfo& info = it->second;
818 return std::make_pair(info.sensorType, info.sensorDataIndex);
819 }
820
Chris Yea52ade12020-08-27 16:49:20 -0700821 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800822 mExcludedDevices = devices;
823 }
824
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000825 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
826 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800827
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000828 const size_t filledSize = std::min(mEvents.size(), bufferSize);
829 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
830
831 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700832 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000833 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800834 }
835
Chris Yea52ade12020-08-27 16:49:20 -0700836 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600837 auto it = mVideoFrames.find(deviceId);
838 if (it != mVideoFrames.end()) {
839 std::vector<TouchVideoFrame> frames = std::move(it->second);
840 mVideoFrames.erase(deviceId);
841 return frames;
842 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800843 return {};
844 }
845
Chris Yea52ade12020-08-27 16:49:20 -0700846 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800847 Device* device = getDevice(deviceId);
848 if (device) {
849 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
850 if (index >= 0) {
851 return device->scanCodeStates.valueAt(index);
852 }
853 }
854 return AKEY_STATE_UNKNOWN;
855 }
856
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000857 InputDeviceCountryCode getCountryCode(int32_t deviceId) const override {
858 Device* device = getDevice(deviceId);
859 if (device) {
860 return device->countryCode;
861 }
862 return InputDeviceCountryCode::INVALID;
863 }
864
Chris Yea52ade12020-08-27 16:49:20 -0700865 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800866 Device* device = getDevice(deviceId);
867 if (device) {
868 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
869 if (index >= 0) {
870 return device->keyCodeStates.valueAt(index);
871 }
872 }
873 return AKEY_STATE_UNKNOWN;
874 }
875
Chris Yea52ade12020-08-27 16:49:20 -0700876 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800877 Device* device = getDevice(deviceId);
878 if (device) {
879 ssize_t index = device->switchStates.indexOfKey(sw);
880 if (index >= 0) {
881 return device->switchStates.valueAt(index);
882 }
883 }
884 return AKEY_STATE_UNKNOWN;
885 }
886
Chris Yea52ade12020-08-27 16:49:20 -0700887 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
888 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800889 Device* device = getDevice(deviceId);
890 if (device) {
891 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
892 if (index >= 0) {
893 *outValue = device->absoluteAxisValue.valueAt(index);
894 return OK;
895 }
896 }
897 *outValue = 0;
898 return -1;
899 }
900
Philip Junker4af3b3d2021-12-14 10:36:55 +0100901 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
902 Device* device = getDevice(deviceId);
903 if (!device) {
904 return AKEYCODE_UNKNOWN;
905 }
906 auto it = device->keyCodeMapping.find(locationKeyCode);
907 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
908 }
909
Chris Yea52ade12020-08-27 16:49:20 -0700910 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700911 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700912 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800913 bool result = false;
914 Device* device = getDevice(deviceId);
915 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700916 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700917 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800918 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
919 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
920 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800921 }
922 }
923 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
924 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
925 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800926 }
927 }
928 }
929 }
930 return result;
931 }
932
Chris Yea52ade12020-08-27 16:49:20 -0700933 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800934 Device* device = getDevice(deviceId);
935 if (device) {
936 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
937 return index >= 0;
938 }
939 return false;
940 }
941
Arthur Hungcb40a002021-08-03 14:31:01 +0000942 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
943 Device* device = getDevice(deviceId);
944 if (!device) {
945 return false;
946 }
947 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
948 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
949 return true;
950 }
951 }
952 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
953 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
954 return true;
955 }
956 }
957 return false;
958 }
959
Chris Yea52ade12020-08-27 16:49:20 -0700960 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800961 Device* device = getDevice(deviceId);
962 return device && device->leds.indexOfKey(led) >= 0;
963 }
964
Chris Yea52ade12020-08-27 16:49:20 -0700965 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800966 Device* device = getDevice(deviceId);
967 if (device) {
968 ssize_t index = device->leds.indexOfKey(led);
969 if (index >= 0) {
970 device->leds.replaceValueAt(led, on);
971 } else {
972 ADD_FAILURE()
973 << "Attempted to set the state of an LED that the EventHub declared "
974 "was not present. led=" << led;
975 }
976 }
977 }
978
Chris Yea52ade12020-08-27 16:49:20 -0700979 void getVirtualKeyDefinitions(
980 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800981 outVirtualKeys.clear();
982
983 Device* device = getDevice(deviceId);
984 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800985 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800986 }
987 }
988
Chris Yea52ade12020-08-27 16:49:20 -0700989 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700990 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800991 }
992
Chris Yea52ade12020-08-27 16:49:20 -0700993 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800994 return false;
995 }
996
Chris Yea52ade12020-08-27 16:49:20 -0700997 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800998
Chris Yea52ade12020-08-27 16:49:20 -0700999 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001000
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001001 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override { return mVibrators; };
Chris Ye87143712020-11-10 05:05:58 +00001002
Chris Yee2b1e5c2021-03-10 22:45:12 -08001003 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
1004 return BATTERY_CAPACITY;
1005 }
Kim Low03ea0352020-11-06 12:45:07 -08001006
Chris Yee2b1e5c2021-03-10 22:45:12 -08001007 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
1008 return BATTERY_STATUS;
1009 }
1010
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001011 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override { return {}; }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001012
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001013 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
1014 int32_t batteryId) const override {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001015 return std::nullopt;
1016 }
Kim Low03ea0352020-11-06 12:45:07 -08001017
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001018 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001019 std::vector<int32_t> ids;
1020 for (const auto& [rawId, info] : mRawLightInfos) {
1021 ids.push_back(rawId);
1022 }
1023 return ids;
1024 }
1025
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001026 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001027 auto it = mRawLightInfos.find(lightId);
1028 if (it == mRawLightInfos.end()) {
1029 return std::nullopt;
1030 }
1031 return it->second;
1032 }
1033
1034 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1035 mLightBrightness.emplace(lightId, brightness);
1036 }
1037
1038 void setLightIntensities(int32_t deviceId, int32_t lightId,
1039 std::unordered_map<LightColor, int32_t> intensities) override {
1040 mLightIntensities.emplace(lightId, intensities);
1041 };
1042
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001043 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001044 auto lightIt = mLightBrightness.find(lightId);
1045 if (lightIt == mLightBrightness.end()) {
1046 return std::nullopt;
1047 }
1048 return lightIt->second;
1049 }
1050
1051 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001052 int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001053 auto lightIt = mLightIntensities.find(lightId);
1054 if (lightIt == mLightIntensities.end()) {
1055 return std::nullopt;
1056 }
1057 return lightIt->second;
1058 };
1059
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001060 void dump(std::string&) const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001061
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001062 void monitor() const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001063
Chris Yea52ade12020-08-27 16:49:20 -07001064 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001065
Chris Yea52ade12020-08-27 16:49:20 -07001066 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001067};
1068
Michael Wrightd02c5b62014-02-10 15:10:22 -08001069// --- FakeInputMapper ---
1070
1071class FakeInputMapper : public InputMapper {
1072 uint32_t mSources;
1073 int32_t mKeyboardType;
1074 int32_t mMetaState;
1075 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1076 KeyedVector<int32_t, int32_t> mScanCodeStates;
1077 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001078 // fake mapping which would normally come from keyCharacterMap
1079 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001080 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001081
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001082 std::mutex mLock;
1083 std::condition_variable mStateChangedCondition;
1084 bool mConfigureWasCalled GUARDED_BY(mLock);
1085 bool mResetWasCalled GUARDED_BY(mLock);
1086 bool mProcessWasCalled GUARDED_BY(mLock);
1087 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001088
Arthur Hungc23540e2018-11-29 20:42:11 +08001089 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001090public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001091 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1092 : InputMapper(deviceContext),
1093 mSources(sources),
1094 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001095 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001096 mConfigureWasCalled(false),
1097 mResetWasCalled(false),
1098 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001099
Chris Yea52ade12020-08-27 16:49:20 -07001100 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001101
1102 void setKeyboardType(int32_t keyboardType) {
1103 mKeyboardType = keyboardType;
1104 }
1105
1106 void setMetaState(int32_t metaState) {
1107 mMetaState = metaState;
1108 }
1109
1110 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001111 std::unique_lock<std::mutex> lock(mLock);
1112 base::ScopedLockAssertion assumeLocked(mLock);
1113 const bool configureCalled =
1114 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1115 return mConfigureWasCalled;
1116 });
1117 if (!configureCalled) {
1118 FAIL() << "Expected configure() to have been called.";
1119 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001120 mConfigureWasCalled = false;
1121 }
1122
1123 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001124 std::unique_lock<std::mutex> lock(mLock);
1125 base::ScopedLockAssertion assumeLocked(mLock);
1126 const bool resetCalled =
1127 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1128 return mResetWasCalled;
1129 });
1130 if (!resetCalled) {
1131 FAIL() << "Expected reset() to have been called.";
1132 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001133 mResetWasCalled = false;
1134 }
1135
Yi Kong9b14ac62018-07-17 13:48:38 -07001136 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001137 std::unique_lock<std::mutex> lock(mLock);
1138 base::ScopedLockAssertion assumeLocked(mLock);
1139 const bool processCalled =
1140 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1141 return mProcessWasCalled;
1142 });
1143 if (!processCalled) {
1144 FAIL() << "Expected process() to have been called.";
1145 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001146 if (outLastEvent) {
1147 *outLastEvent = mLastEvent;
1148 }
1149 mProcessWasCalled = false;
1150 }
1151
1152 void setKeyCodeState(int32_t keyCode, int32_t state) {
1153 mKeyCodeStates.replaceValueFor(keyCode, state);
1154 }
1155
1156 void setScanCodeState(int32_t scanCode, int32_t state) {
1157 mScanCodeStates.replaceValueFor(scanCode, state);
1158 }
1159
1160 void setSwitchState(int32_t switchCode, int32_t state) {
1161 mSwitchStates.replaceValueFor(switchCode, state);
1162 }
1163
1164 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001165 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001166 }
1167
Philip Junker4af3b3d2021-12-14 10:36:55 +01001168 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1169 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1170 }
1171
Michael Wrightd02c5b62014-02-10 15:10:22 -08001172private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001173 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001174
Chris Yea52ade12020-08-27 16:49:20 -07001175 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001176 InputMapper::populateDeviceInfo(deviceInfo);
1177
1178 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1179 deviceInfo->setKeyboardType(mKeyboardType);
1180 }
1181 }
1182
Chris Yea52ade12020-08-27 16:49:20 -07001183 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001184 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001185 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001186
1187 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001188 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001189 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1190 mViewport = config->getDisplayViewportByPort(*displayPort);
1191 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001192
1193 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001194 }
1195
Chris Yea52ade12020-08-27 16:49:20 -07001196 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001197 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001198 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001199 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001200 }
1201
Chris Yea52ade12020-08-27 16:49:20 -07001202 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001203 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001204 mLastEvent = *rawEvent;
1205 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001206 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001207 }
1208
Chris Yea52ade12020-08-27 16:49:20 -07001209 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001210 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1211 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1212 }
1213
Philip Junker4af3b3d2021-12-14 10:36:55 +01001214 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1215 auto it = mKeyCodeMapping.find(locationKeyCode);
1216 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1217 }
1218
Chris Yea52ade12020-08-27 16:49:20 -07001219 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001220 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1221 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1222 }
1223
Chris Yea52ade12020-08-27 16:49:20 -07001224 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001225 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1226 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1227 }
1228
Chris Yea52ade12020-08-27 16:49:20 -07001229 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001230 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -07001231 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001232 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001233 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1234 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1235 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001236 }
1237 }
1238 }
Chris Yea52ade12020-08-27 16:49:20 -07001239 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001240 return result;
1241 }
1242
1243 virtual int32_t getMetaState() {
1244 return mMetaState;
1245 }
1246
1247 virtual void fadePointer() {
1248 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001249
1250 virtual std::optional<int32_t> getAssociatedDisplay() {
1251 if (mViewport) {
1252 return std::make_optional(mViewport->displayId);
1253 }
1254 return std::nullopt;
1255 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001256};
1257
1258
1259// --- InstrumentedInputReader ---
1260
1261class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001262 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001263
1264public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001265 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1266 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001267 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001268 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001269
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001270 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001271
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001272 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001273
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001274 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001275 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001276 InputDeviceIdentifier identifier;
1277 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001278 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001279 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001280 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001281 }
1282
Prabir Pradhan28efc192019-11-05 01:10:04 +00001283 // Make the protected loopOnce method accessible to tests.
1284 using InputReader::loopOnce;
1285
Michael Wrightd02c5b62014-02-10 15:10:22 -08001286protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001287 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1288 const InputDeviceIdentifier& identifier)
1289 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001290 if (!mNextDevices.empty()) {
1291 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1292 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001293 return device;
1294 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001295 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001296 }
1297
arthurhungdcef2dc2020-08-11 14:47:50 +08001298 // --- FakeInputReaderContext ---
1299 class FakeInputReaderContext : public ContextImpl {
1300 int32_t mGlobalMetaState;
1301 bool mUpdateGlobalMetaStateWasCalled;
1302 int32_t mGeneration;
1303
1304 public:
1305 FakeInputReaderContext(InputReader* reader)
1306 : ContextImpl(reader),
1307 mGlobalMetaState(0),
1308 mUpdateGlobalMetaStateWasCalled(false),
1309 mGeneration(1) {}
1310
1311 virtual ~FakeInputReaderContext() {}
1312
1313 void assertUpdateGlobalMetaStateWasCalled() {
1314 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1315 << "Expected updateGlobalMetaState() to have been called.";
1316 mUpdateGlobalMetaStateWasCalled = false;
1317 }
1318
1319 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1320
1321 uint32_t getGeneration() { return mGeneration; }
1322
1323 void updateGlobalMetaState() override {
1324 mUpdateGlobalMetaStateWasCalled = true;
1325 ContextImpl::updateGlobalMetaState();
1326 }
1327
1328 int32_t getGlobalMetaState() override {
1329 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1330 }
1331
1332 int32_t bumpGeneration() override {
1333 mGeneration = ContextImpl::bumpGeneration();
1334 return mGeneration;
1335 }
1336 } mFakeContext;
1337
Michael Wrightd02c5b62014-02-10 15:10:22 -08001338 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001339
1340public:
1341 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001342};
1343
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001344// --- InputReaderPolicyTest ---
1345class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001346protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001347 sp<FakeInputReaderPolicy> mFakePolicy;
1348
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001349 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -07001350 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001351};
1352
1353/**
1354 * Check that empty set of viewports is an acceptable configuration.
1355 * Also try to get internal viewport two different ways - by type and by uniqueId.
1356 *
1357 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1358 * Such configuration is not currently allowed.
1359 */
1360TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001361 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001362
1363 // We didn't add any viewports yet, so there shouldn't be any.
1364 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001365 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001366 ASSERT_FALSE(internalViewport);
1367
1368 // Add an internal viewport, then clear it
1369 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001370 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001371 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001372
1373 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001374 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001375 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001376 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001377
1378 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001379 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001380 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001381 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001382
1383 mFakePolicy->clearViewports();
1384 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001385 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001386 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001387 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001388 ASSERT_FALSE(internalViewport);
1389}
1390
1391TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1392 const std::string internalUniqueId = "local:0";
1393 const std::string externalUniqueId = "local:1";
1394 const std::string virtualUniqueId1 = "virtual:2";
1395 const std::string virtualUniqueId2 = "virtual:3";
1396 constexpr int32_t virtualDisplayId1 = 2;
1397 constexpr int32_t virtualDisplayId2 = 3;
1398
1399 // Add an internal viewport
1400 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001401 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1402 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001403 // Add an external viewport
1404 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001405 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1406 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001407 // Add an virtual viewport
1408 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001409 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1410 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001411 // Add another virtual viewport
1412 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001413 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1414 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001415
1416 // Check matching by type for internal
1417 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001418 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001419 ASSERT_TRUE(internalViewport);
1420 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1421
1422 // Check matching by type for external
1423 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001424 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001425 ASSERT_TRUE(externalViewport);
1426 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1427
1428 // Check matching by uniqueId for virtual viewport #1
1429 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001430 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001431 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001432 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001433 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1434 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1435
1436 // Check matching by uniqueId for virtual viewport #2
1437 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001438 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001439 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001440 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001441 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1442 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1443}
1444
1445
1446/**
1447 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1448 * that lookup works by checking display id.
1449 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1450 */
1451TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1452 const std::string uniqueId1 = "uniqueId1";
1453 const std::string uniqueId2 = "uniqueId2";
1454 constexpr int32_t displayId1 = 2;
1455 constexpr int32_t displayId2 = 3;
1456
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001457 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1458 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001459 for (const ViewportType& type : types) {
1460 mFakePolicy->clearViewports();
1461 // Add a viewport
1462 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001463 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1464 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001465 // Add another viewport
1466 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001467 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1468 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001469
1470 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001471 std::optional<DisplayViewport> viewport1 =
1472 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001473 ASSERT_TRUE(viewport1);
1474 ASSERT_EQ(displayId1, viewport1->displayId);
1475 ASSERT_EQ(type, viewport1->type);
1476
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001477 std::optional<DisplayViewport> viewport2 =
1478 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001479 ASSERT_TRUE(viewport2);
1480 ASSERT_EQ(displayId2, viewport2->displayId);
1481 ASSERT_EQ(type, viewport2->type);
1482
1483 // When there are multiple viewports of the same kind, and uniqueId is not specified
1484 // in the call to getDisplayViewport, then that situation is not supported.
1485 // The viewports can be stored in any order, so we cannot rely on the order, since that
1486 // is just implementation detail.
1487 // However, we can check that it still returns *a* viewport, we just cannot assert
1488 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001489 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001490 ASSERT_TRUE(someViewport);
1491 }
1492}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001493
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001494/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001495 * When we have multiple internal displays make sure we always return the default display when
1496 * querying by type.
1497 */
1498TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1499 const std::string uniqueId1 = "uniqueId1";
1500 const std::string uniqueId2 = "uniqueId2";
1501 constexpr int32_t nonDefaultDisplayId = 2;
1502 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1503 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1504
1505 // Add the default display first and ensure it gets returned.
1506 mFakePolicy->clearViewports();
1507 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001508 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001509 ViewportType::INTERNAL);
1510 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001511 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001512 ViewportType::INTERNAL);
1513
1514 std::optional<DisplayViewport> viewport =
1515 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1516 ASSERT_TRUE(viewport);
1517 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1518 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1519
1520 // Add the default display second to make sure order doesn't matter.
1521 mFakePolicy->clearViewports();
1522 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001523 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001524 ViewportType::INTERNAL);
1525 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001526 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001527 ViewportType::INTERNAL);
1528
1529 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1530 ASSERT_TRUE(viewport);
1531 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1532 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1533}
1534
1535/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001536 * Check getDisplayViewportByPort
1537 */
1538TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001539 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001540 const std::string uniqueId1 = "uniqueId1";
1541 const std::string uniqueId2 = "uniqueId2";
1542 constexpr int32_t displayId1 = 1;
1543 constexpr int32_t displayId2 = 2;
1544 const uint8_t hdmi1 = 0;
1545 const uint8_t hdmi2 = 1;
1546 const uint8_t hdmi3 = 2;
1547
1548 mFakePolicy->clearViewports();
1549 // Add a viewport that's associated with some display port that's not of interest.
1550 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001551 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1552 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001553 // Add another viewport, connected to HDMI1 port
1554 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001555 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1556 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001557
1558 // Check that correct display viewport was returned by comparing the display ports.
1559 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1560 ASSERT_TRUE(hdmi1Viewport);
1561 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1562 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1563
1564 // Check that we can still get the same viewport using the uniqueId
1565 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1566 ASSERT_TRUE(hdmi1Viewport);
1567 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1568 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1569 ASSERT_EQ(type, hdmi1Viewport->type);
1570
1571 // Check that we cannot find a port with "HDMI2", because we never added one
1572 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1573 ASSERT_FALSE(hdmi2Viewport);
1574}
1575
Michael Wrightd02c5b62014-02-10 15:10:22 -08001576// --- InputReaderTest ---
1577
1578class InputReaderTest : public testing::Test {
1579protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001580 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001581 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001582 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001583 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001584
Chris Yea52ade12020-08-27 16:49:20 -07001585 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001586 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001587 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001588 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001589
Prabir Pradhan28efc192019-11-05 01:10:04 +00001590 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001591 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001592 }
1593
Chris Yea52ade12020-08-27 16:49:20 -07001594 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001595 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001596 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001597 }
1598
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001599 void addDevice(int32_t eventHubId, const std::string& name,
1600 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001601 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001602
1603 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001604 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001605 }
1606 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001607 mReader->loopOnce();
1608 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001609 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1610 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001611 }
1612
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001613 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001614 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001615 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001616 }
1617
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001618 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001619 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001620 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001621 }
1622
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001623 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001624 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001625 ftl::Flags<InputDeviceClass> classes,
1626 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001627 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001628 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1629 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001630 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001631 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001632 return mapper;
1633 }
1634};
1635
Chris Ye98d3f532020-10-01 21:48:59 -07001636TEST_F(InputReaderTest, PolicyGetInputDevices) {
1637 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001638 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001639 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001640
1641 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001642 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001643 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001644 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001645 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001646 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1647 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001648 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001649}
1650
Chris Yee7310032020-09-22 15:36:28 -07001651TEST_F(InputReaderTest, GetMergedInputDevices) {
1652 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1653 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1654 // Add two subdevices to device
1655 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1656 // Must add at least one mapper or the device will be ignored!
1657 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1658 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1659
1660 // Push same device instance for next device to be added, so they'll have same identifier.
1661 mReader->pushNextDevice(device);
1662 mReader->pushNextDevice(device);
1663 ASSERT_NO_FATAL_FAILURE(
1664 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1665 ASSERT_NO_FATAL_FAILURE(
1666 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1667
1668 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001669 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001670}
1671
Chris Yee14523a2020-12-19 13:46:00 -08001672TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1673 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1674 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1675 // Add two subdevices to device
1676 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1677 // Must add at least one mapper or the device will be ignored!
1678 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1679 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1680
1681 // Push same device instance for next device to be added, so they'll have same identifier.
1682 mReader->pushNextDevice(device);
1683 mReader->pushNextDevice(device);
1684 // Sensor device is initially disabled
1685 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1686 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1687 nullptr));
1688 // Device is disabled because the only sub device is a sensor device and disabled initially.
1689 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1690 ASSERT_FALSE(device->isEnabled());
1691 ASSERT_NO_FATAL_FAILURE(
1692 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1693 // The merged device is enabled if any sub device is enabled
1694 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1695 ASSERT_TRUE(device->isEnabled());
1696}
1697
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001698TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001699 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001700 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001701 constexpr int32_t eventHubId = 1;
1702 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001703 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001704 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001705 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001706 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001707
Yi Kong9b14ac62018-07-17 13:48:38 -07001708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001709
1710 NotifyDeviceResetArgs resetArgs;
1711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001712 ASSERT_EQ(deviceId, resetArgs.deviceId);
1713
1714 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001715 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001716 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001717
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001719 ASSERT_EQ(deviceId, resetArgs.deviceId);
1720 ASSERT_EQ(device->isEnabled(), false);
1721
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001722 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001723 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001726 ASSERT_EQ(device->isEnabled(), false);
1727
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001728 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001729 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001731 ASSERT_EQ(deviceId, resetArgs.deviceId);
1732 ASSERT_EQ(device->isEnabled(), true);
1733}
1734
Michael Wrightd02c5b62014-02-10 15:10:22 -08001735TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001736 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001737 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001738 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001739 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001740 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001741 AINPUT_SOURCE_KEYBOARD, nullptr);
1742 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001743
1744 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1745 AINPUT_SOURCE_ANY, AKEYCODE_A))
1746 << "Should return unknown when the device id is >= 0 but unknown.";
1747
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001748 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1749 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1750 << "Should return unknown when the device id is valid but the sources are not "
1751 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001752
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001753 ASSERT_EQ(AKEY_STATE_DOWN,
1754 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1755 AKEYCODE_A))
1756 << "Should return value provided by mapper when device id is valid and the device "
1757 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001758
1759 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1760 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1761 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1762
1763 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1764 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1765 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1766}
1767
Philip Junker4af3b3d2021-12-14 10:36:55 +01001768TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1769 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1770 constexpr int32_t eventHubId = 1;
1771 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1772 InputDeviceClass::KEYBOARD,
1773 AINPUT_SOURCE_KEYBOARD, nullptr);
1774 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1775
1776 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1777 << "Should return unknown when the device with the specified id is not found.";
1778
1779 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1780 << "Should return correct mapping when device id is valid and mapping exists.";
1781
1782 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1783 << "Should return the location key code when device id is valid and there's no "
1784 "mapping.";
1785}
1786
1787TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1788 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1789 constexpr int32_t eventHubId = 1;
1790 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1791 InputDeviceClass::JOYSTICK,
1792 AINPUT_SOURCE_GAMEPAD, nullptr);
1793 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1794
1795 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1796 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1797}
1798
Michael Wrightd02c5b62014-02-10 15:10:22 -08001799TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001800 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001801 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001802 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001803 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001804 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001805 AINPUT_SOURCE_KEYBOARD, nullptr);
1806 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001807
1808 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1809 AINPUT_SOURCE_ANY, KEY_A))
1810 << "Should return unknown when the device id is >= 0 but unknown.";
1811
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001812 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1813 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1814 << "Should return unknown when the device id is valid but the sources are not "
1815 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001816
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001817 ASSERT_EQ(AKEY_STATE_DOWN,
1818 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1819 KEY_A))
1820 << "Should return value provided by mapper when device id is valid and the device "
1821 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001822
1823 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1824 AINPUT_SOURCE_TRACKBALL, KEY_A))
1825 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1826
1827 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1828 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1829 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1830}
1831
1832TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001833 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001834 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001835 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001836 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001837 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001838 AINPUT_SOURCE_KEYBOARD, nullptr);
1839 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001840
1841 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1842 AINPUT_SOURCE_ANY, SW_LID))
1843 << "Should return unknown when the device id is >= 0 but unknown.";
1844
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001845 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1846 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1847 << "Should return unknown when the device id is valid but the sources are not "
1848 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001849
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001850 ASSERT_EQ(AKEY_STATE_DOWN,
1851 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1852 SW_LID))
1853 << "Should return value provided by mapper when device id is valid and the device "
1854 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001855
1856 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1857 AINPUT_SOURCE_TRACKBALL, SW_LID))
1858 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1859
1860 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1861 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1862 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1863}
1864
1865TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001866 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001867 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001868 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001869 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001870 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001871 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001872
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001873 mapper.addSupportedKeyCode(AKEYCODE_A);
1874 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001875
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001876 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001877 uint8_t flags[4] = { 0, 0, 0, 1 };
1878
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001879 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08001880 << "Should return false when device id is >= 0 but unknown.";
1881 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1882
1883 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001884 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001885 << "Should return false when device id is valid but the sources are not supported by "
1886 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001887 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1888
1889 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001890 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001891 keyCodes, flags))
1892 << "Should return value provided by mapper when device id is valid and the device "
1893 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001894 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1895
1896 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001897 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1898 << "Should return false when the device id is < 0 but the sources are not supported by "
1899 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001900 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1901
1902 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001903 ASSERT_TRUE(
1904 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1905 << "Should return value provided by mapper when device id is < 0 and one of the "
1906 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001907 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1908}
1909
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001910TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001911 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001912 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001913
1914 NotifyConfigurationChangedArgs args;
1915
1916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1917 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1918}
1919
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001920TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001921 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001922 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001923 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001924 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001925 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001926 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001927 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001928 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001929
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001930 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001931 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001932 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1933
1934 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001935 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001936 ASSERT_EQ(when, event.when);
1937 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001938 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001939 ASSERT_EQ(EV_KEY, event.type);
1940 ASSERT_EQ(KEY_A, event.code);
1941 ASSERT_EQ(1, event.value);
1942}
1943
Garfield Tan1c7bc862020-01-28 13:24:04 -08001944TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001945 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001946 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001947 constexpr int32_t eventHubId = 1;
1948 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001949 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001950 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001951 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001952 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001953
1954 NotifyDeviceResetArgs resetArgs;
1955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001956 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001957
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001958 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001959 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001961 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001962 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001963
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001964 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001965 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001967 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001968 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001969
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001970 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001971 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001973 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001974 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001975}
1976
Garfield Tan1c7bc862020-01-28 13:24:04 -08001977TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1978 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001979 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001980 constexpr int32_t eventHubId = 1;
1981 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1982 // Must add at least one mapper or the device will be ignored!
1983 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001984 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001985 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1986
1987 NotifyDeviceResetArgs resetArgs;
1988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1989 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1990}
1991
Arthur Hungc23540e2018-11-29 20:42:11 +08001992TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001993 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001994 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001995 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001996 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001997 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1998 FakeInputMapper& mapper =
1999 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002000 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08002001
2002 const uint8_t hdmi1 = 1;
2003
2004 // Associated touch screen with second display.
2005 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2006
2007 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00002008 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08002009 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002010 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002011 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002012 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002013 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002014 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002015 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002016 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002017
2018 // Add the device, and make sure all of the callbacks are triggered.
2019 // The device is added after the input port associations are processed since
2020 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002021 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002024 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002025
Arthur Hung2c9a3342019-07-23 14:18:59 +08002026 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002027 ASSERT_EQ(deviceId, device->getId());
2028 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2029 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002030
2031 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002032 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002033 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002034 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002035}
2036
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002037TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2038 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002039 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002040 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2041 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2042 // Must add at least one mapper or the device will be ignored!
2043 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2044 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2045 mReader->pushNextDevice(device);
2046 mReader->pushNextDevice(device);
2047 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2048 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2049
2050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2051
2052 NotifyDeviceResetArgs resetArgs;
2053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2054 ASSERT_EQ(deviceId, resetArgs.deviceId);
2055 ASSERT_TRUE(device->isEnabled());
2056 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2057 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2058
2059 disableDevice(deviceId);
2060 mReader->loopOnce();
2061
2062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2063 ASSERT_EQ(deviceId, resetArgs.deviceId);
2064 ASSERT_FALSE(device->isEnabled());
2065 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2066 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2067
2068 enableDevice(deviceId);
2069 mReader->loopOnce();
2070
2071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2072 ASSERT_EQ(deviceId, resetArgs.deviceId);
2073 ASSERT_TRUE(device->isEnabled());
2074 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2075 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2076}
2077
2078TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2079 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002080 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002081 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2082 // Add two subdevices to device
2083 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2084 FakeInputMapper& mapperDevice1 =
2085 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2086 FakeInputMapper& mapperDevice2 =
2087 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2088 mReader->pushNextDevice(device);
2089 mReader->pushNextDevice(device);
2090 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2091 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2092
2093 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2094 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2095
2096 ASSERT_EQ(AKEY_STATE_DOWN,
2097 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2098 ASSERT_EQ(AKEY_STATE_DOWN,
2099 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2100 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2101 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2102}
2103
Prabir Pradhan7e186182020-11-10 13:56:45 -08002104TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2105 NotifyPointerCaptureChangedArgs args;
2106
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002107 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002108 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2109 mReader->loopOnce();
2110 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002111 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2112 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002113
2114 mFakePolicy->setPointerCapture(false);
2115 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2116 mReader->loopOnce();
2117 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002118 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002119
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002120 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002121 // does not change.
2122 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2123 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002124 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002125}
2126
Chris Ye87143712020-11-10 05:05:58 +00002127class FakeVibratorInputMapper : public FakeInputMapper {
2128public:
2129 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2130 : FakeInputMapper(deviceContext, sources) {}
2131
2132 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2133};
2134
2135TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2136 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002137 ftl::Flags<InputDeviceClass> deviceClass =
2138 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002139 constexpr int32_t eventHubId = 1;
2140 const char* DEVICE_LOCATION = "BLUETOOTH";
2141 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2142 FakeVibratorInputMapper& mapper =
2143 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2144 mReader->pushNextDevice(device);
2145
2146 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2147 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2148
2149 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2150 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2151}
2152
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002153// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002154
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002155class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002156public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002157 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002158
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002159 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002160
2161 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2162
2163 void dump(std::string& dump) override {}
2164
2165 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2166 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002167 }
2168
Chris Yee2b1e5c2021-03-10 22:45:12 -08002169 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2170 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002171 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002172
2173 bool setLightColor(int32_t lightId, int32_t color) override {
2174 getDeviceContext().setLightBrightness(lightId, color >> 24);
2175 return true;
2176 }
2177
2178 std::optional<int32_t> getLightColor(int32_t lightId) override {
2179 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2180 if (!result.has_value()) {
2181 return std::nullopt;
2182 }
2183 return result.value() << 24;
2184 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002185
2186 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2187
2188 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2189
2190private:
2191 InputDeviceContext& mDeviceContext;
2192 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2193 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002194};
2195
Chris Yee2b1e5c2021-03-10 22:45:12 -08002196TEST_F(InputReaderTest, BatteryGetCapacity) {
2197 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002198 ftl::Flags<InputDeviceClass> deviceClass =
2199 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002200 constexpr int32_t eventHubId = 1;
2201 const char* DEVICE_LOCATION = "BLUETOOTH";
2202 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002203 FakePeripheralController& controller =
2204 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002205 mReader->pushNextDevice(device);
2206
2207 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2208
2209 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2210 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2211}
2212
2213TEST_F(InputReaderTest, BatteryGetStatus) {
2214 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002215 ftl::Flags<InputDeviceClass> deviceClass =
2216 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002217 constexpr int32_t eventHubId = 1;
2218 const char* DEVICE_LOCATION = "BLUETOOTH";
2219 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002220 FakePeripheralController& controller =
2221 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002222 mReader->pushNextDevice(device);
2223
2224 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2225
2226 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2227 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2228}
2229
Chris Ye3fdbfef2021-01-06 18:45:18 -08002230TEST_F(InputReaderTest, LightGetColor) {
2231 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002232 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002233 constexpr int32_t eventHubId = 1;
2234 const char* DEVICE_LOCATION = "BLUETOOTH";
2235 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002236 FakePeripheralController& controller =
2237 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002238 mReader->pushNextDevice(device);
2239 RawLightInfo info = {.id = 1,
2240 .name = "Mono",
2241 .maxBrightness = 255,
2242 .flags = InputLightClass::BRIGHTNESS,
2243 .path = ""};
2244 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2245 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2246
2247 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002248
Chris Yee2b1e5c2021-03-10 22:45:12 -08002249 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2250 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002251 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2252 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2253}
2254
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002255// --- InputReaderIntegrationTest ---
2256
2257// These tests create and interact with the InputReader only through its interface.
2258// The InputReader is started during SetUp(), which starts its processing in its own
2259// thread. The tests use linux uinput to emulate input devices.
2260// NOTE: Interacting with the physical device while these tests are running may cause
2261// the tests to fail.
2262class InputReaderIntegrationTest : public testing::Test {
2263protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002264 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002265 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002266 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002267
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002268 std::shared_ptr<FakePointerController> mFakePointerController;
2269
Chris Yea52ade12020-08-27 16:49:20 -07002270 void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002271 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002272 mFakePointerController = std::make_shared<FakePointerController>();
2273 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002274 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2275 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002276
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002277 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2278 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002279 ASSERT_EQ(mReader->start(), OK);
2280
2281 // Since this test is run on a real device, all the input devices connected
2282 // to the test device will show up in mReader. We wait for those input devices to
2283 // show up before beginning the tests.
2284 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2285 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2286 }
2287
Chris Yea52ade12020-08-27 16:49:20 -07002288 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002289 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002290 mReader.reset();
2291 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002292 mFakePolicy.clear();
2293 }
2294};
2295
2296TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2297 // An invalid input device that is only used for this test.
2298 class InvalidUinputDevice : public UinputDevice {
2299 public:
2300 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2301
2302 private:
2303 void configureDevice(int fd, uinput_user_dev* device) override {}
2304 };
2305
2306 const size_t numDevices = mFakePolicy->getInputDevices().size();
2307
2308 // UinputDevice does not set any event or key bits, so InputReader should not
2309 // consider it as a valid device.
2310 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2311 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2312 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2313 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2314
2315 invalidDevice.reset();
2316 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2317 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2318 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2319}
2320
2321TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2322 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2323
2324 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2325 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2326 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2327 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2328
2329 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002330 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002331 const auto& it =
2332 std::find_if(inputDevices.begin(), inputDevices.end(),
2333 [&keyboard](const InputDeviceInfo& info) {
2334 return info.getIdentifier().name == keyboard->getName();
2335 });
2336
2337 ASSERT_NE(it, inputDevices.end());
2338 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2339 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2340 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002341
2342 keyboard.reset();
2343 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2344 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2345 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2346}
2347
2348TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2349 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2350 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2351
2352 NotifyConfigurationChangedArgs configChangedArgs;
2353 ASSERT_NO_FATAL_FAILURE(
2354 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002355 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002356 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2357
2358 NotifyKeyArgs keyArgs;
2359 keyboard->pressAndReleaseHomeKey();
2360 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2361 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002362 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002363 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002364 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002365 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002366 prevTimestamp = keyArgs.eventTime;
2367
2368 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2369 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002370 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002371 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002372 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002373}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002374
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002375/**
2376 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2377 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2378 * are passed to the listener.
2379 */
2380static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2381TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2382 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2383 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2384 NotifyKeyArgs keyArgs;
2385
2386 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2387 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2388 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2389 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2390
2391 controller->pressAndReleaseKey(BTN_GEAR_UP);
2392 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2393 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2394 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2395}
2396
Arthur Hungaab25622020-01-16 11:22:11 +08002397// --- TouchProcessTest ---
2398class TouchIntegrationTest : public InputReaderIntegrationTest {
2399protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002400 const std::string UNIQUE_ID = "local:0";
2401
Chris Yea52ade12020-08-27 16:49:20 -07002402 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002403 InputReaderIntegrationTest::SetUp();
2404 // At least add an internal display.
2405 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2406 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002407 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002408
2409 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2410 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2411 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2412 }
2413
2414 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2415 int32_t orientation, const std::string& uniqueId,
2416 std::optional<uint8_t> physicalPort,
2417 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002418 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2419 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002420 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2421 }
2422
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002423 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2424 NotifyMotionArgs args;
2425 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2426 EXPECT_EQ(action, args.action);
2427 ASSERT_EQ(points.size(), args.pointerCount);
2428 for (size_t i = 0; i < args.pointerCount; i++) {
2429 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2430 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2431 }
2432 }
2433
Arthur Hungaab25622020-01-16 11:22:11 +08002434 std::unique_ptr<UinputTouchScreen> mDevice;
2435};
2436
2437TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2438 NotifyMotionArgs args;
2439 const Point centerPoint = mDevice->getCenterPoint();
2440
2441 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002442 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002443 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002444 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002445 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2446 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2447
2448 // ACTION_MOVE
2449 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002450 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002451 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2452 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2453
2454 // ACTION_UP
2455 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002456 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002457 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2458 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2459}
2460
2461TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2462 NotifyMotionArgs args;
2463 const Point centerPoint = mDevice->getCenterPoint();
2464
2465 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002466 mDevice->sendSlot(FIRST_SLOT);
2467 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002468 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002469 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002470 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2471 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2472
2473 // ACTION_POINTER_DOWN (Second slot)
2474 const Point secondPoint = centerPoint + Point(100, 100);
2475 mDevice->sendSlot(SECOND_SLOT);
2476 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002477 mDevice->sendDown(secondPoint);
2478 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002479 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002480 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002481
2482 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002483 mDevice->sendMove(secondPoint + Point(1, 1));
2484 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002485 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2486 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2487
2488 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002489 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002490 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002491 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002492 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002493
2494 // ACTION_UP
2495 mDevice->sendSlot(FIRST_SLOT);
2496 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002497 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002498 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2499 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2500}
2501
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002502/**
2503 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2504 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2505 * data?
2506 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2507 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2508 * for Pointer 0 only is generated after.
2509 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2510 * events, we will not miss any information.
2511 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2512 * event generated afterwards that contains the newest movement of pointer 0.
2513 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2514 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2515 * losing information about non-palm pointers.
2516 */
2517TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2518 NotifyMotionArgs args;
2519 const Point centerPoint = mDevice->getCenterPoint();
2520
2521 // ACTION_DOWN
2522 mDevice->sendSlot(FIRST_SLOT);
2523 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2524 mDevice->sendDown(centerPoint);
2525 mDevice->sendSync();
2526 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2527
2528 // ACTION_POINTER_DOWN (Second slot)
2529 const Point secondPoint = centerPoint + Point(100, 100);
2530 mDevice->sendSlot(SECOND_SLOT);
2531 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2532 mDevice->sendDown(secondPoint);
2533 mDevice->sendSync();
2534 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2535
2536 // ACTION_MOVE (First slot)
2537 mDevice->sendSlot(FIRST_SLOT);
2538 mDevice->sendMove(centerPoint + Point(5, 5));
2539 // ACTION_POINTER_UP (Second slot)
2540 mDevice->sendSlot(SECOND_SLOT);
2541 mDevice->sendPointerUp();
2542 // Send a single sync for the above 2 pointer updates
2543 mDevice->sendSync();
2544
2545 // First, we should get POINTER_UP for the second pointer
2546 assertReceivedMotion(ACTION_POINTER_1_UP,
2547 {/*first pointer */ centerPoint + Point(5, 5),
2548 /*second pointer*/ secondPoint});
2549
2550 // Next, the MOVE event for the first pointer
2551 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2552}
2553
2554/**
2555 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2556 * move, and then it will go up, all in the same frame.
2557 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2558 * gets sent to the listener.
2559 */
2560TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2561 NotifyMotionArgs args;
2562 const Point centerPoint = mDevice->getCenterPoint();
2563
2564 // ACTION_DOWN
2565 mDevice->sendSlot(FIRST_SLOT);
2566 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2567 mDevice->sendDown(centerPoint);
2568 mDevice->sendSync();
2569 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2570
2571 // ACTION_POINTER_DOWN (Second slot)
2572 const Point secondPoint = centerPoint + Point(100, 100);
2573 mDevice->sendSlot(SECOND_SLOT);
2574 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2575 mDevice->sendDown(secondPoint);
2576 mDevice->sendSync();
2577 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2578
2579 // ACTION_MOVE (First slot)
2580 mDevice->sendSlot(FIRST_SLOT);
2581 mDevice->sendMove(centerPoint + Point(5, 5));
2582 // ACTION_POINTER_UP (Second slot)
2583 mDevice->sendSlot(SECOND_SLOT);
2584 mDevice->sendMove(secondPoint + Point(6, 6));
2585 mDevice->sendPointerUp();
2586 // Send a single sync for the above 2 pointer updates
2587 mDevice->sendSync();
2588
2589 // First, we should get POINTER_UP for the second pointer
2590 // The movement of the second pointer during the liftoff frame is ignored.
2591 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2592 assertReceivedMotion(ACTION_POINTER_1_UP,
2593 {/*first pointer */ centerPoint + Point(5, 5),
2594 /*second pointer*/ secondPoint});
2595
2596 // Next, the MOVE event for the first pointer
2597 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2598}
2599
Arthur Hungaab25622020-01-16 11:22:11 +08002600TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2601 NotifyMotionArgs args;
2602 const Point centerPoint = mDevice->getCenterPoint();
2603
2604 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002605 mDevice->sendSlot(FIRST_SLOT);
2606 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002607 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002608 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002609 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2610 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2611
arthurhungcc7f9802020-04-30 17:55:40 +08002612 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002613 const Point secondPoint = centerPoint + Point(100, 100);
2614 mDevice->sendSlot(SECOND_SLOT);
2615 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2616 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002617 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002618 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002619 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002620
arthurhungcc7f9802020-04-30 17:55:40 +08002621 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002622 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002623 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002624 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2625 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2626
arthurhungcc7f9802020-04-30 17:55:40 +08002627 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2628 // a palm event.
2629 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002630 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002631 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002632 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002633 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002634 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002635
arthurhungcc7f9802020-04-30 17:55:40 +08002636 // Send up to second slot, expect first slot send moving.
2637 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002638 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002639 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2640 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002641
arthurhungcc7f9802020-04-30 17:55:40 +08002642 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002643 mDevice->sendSlot(FIRST_SLOT);
2644 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002645 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002646
arthurhungcc7f9802020-04-30 17:55:40 +08002647 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2648 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002649}
2650
Michael Wrightd02c5b62014-02-10 15:10:22 -08002651// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002652class InputDeviceTest : public testing::Test {
2653protected:
2654 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002655 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002656 static const int32_t DEVICE_ID;
2657 static const int32_t DEVICE_GENERATION;
2658 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002659 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002660 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002661
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002662 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002663 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002664 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002665 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002666 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002667
Chris Yea52ade12020-08-27 16:49:20 -07002668 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002669 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002670 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002671 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002672 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002673 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002674 InputDeviceIdentifier identifier;
2675 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002676 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002677 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002678 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002679 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002680 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002681 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002682 }
2683
Chris Yea52ade12020-08-27 16:49:20 -07002684 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002685 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002686 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002687 }
2688};
2689
2690const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002691const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002692const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002693const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2694const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002695const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002696 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002697const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002698
2699TEST_F(InputDeviceTest, ImmutableProperties) {
2700 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002701 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002702 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002703}
2704
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002705TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
2706 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
2707
2708 // Configuration
2709 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2710 InputReaderConfiguration config;
2711 mDevice->configure(ARBITRARY_TIME, &config, 0);
2712
2713 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
2714}
2715
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002716TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2717 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002718}
2719
Michael Wrightd02c5b62014-02-10 15:10:22 -08002720TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2721 // Configuration.
2722 InputReaderConfiguration config;
2723 mDevice->configure(ARBITRARY_TIME, &config, 0);
2724
2725 // Reset.
2726 mDevice->reset(ARBITRARY_TIME);
2727
2728 NotifyDeviceResetArgs resetArgs;
2729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2730 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2731 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2732
2733 // Metadata.
2734 ASSERT_TRUE(mDevice->isIgnored());
2735 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2736
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002737 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002738 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002739 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002740 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2741 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2742
2743 // State queries.
2744 ASSERT_EQ(0, mDevice->getMetaState());
2745
2746 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2747 << "Ignored device should return unknown key code state.";
2748 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2749 << "Ignored device should return unknown scan code state.";
2750 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2751 << "Ignored device should return unknown switch state.";
2752
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002753 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002754 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002755 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002756 << "Ignored device should never mark any key codes.";
2757 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2758 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2759}
2760
2761TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2762 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002763 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08002764
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002765 FakeInputMapper& mapper1 =
2766 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002767 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2768 mapper1.setMetaState(AMETA_ALT_ON);
2769 mapper1.addSupportedKeyCode(AKEYCODE_A);
2770 mapper1.addSupportedKeyCode(AKEYCODE_B);
2771 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2772 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2773 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2774 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2775 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002776
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002777 FakeInputMapper& mapper2 =
2778 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002779 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002780
2781 InputReaderConfiguration config;
2782 mDevice->configure(ARBITRARY_TIME, &config, 0);
2783
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002784 std::string propertyValue;
2785 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002786 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002787 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002788
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002789 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2790 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002791
2792 // Reset
2793 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002794 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2795 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002796
2797 NotifyDeviceResetArgs resetArgs;
2798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2799 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2800 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2801
2802 // Metadata.
2803 ASSERT_FALSE(mDevice->isIgnored());
2804 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2805
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002806 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002807 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002808 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002809 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2810 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2811
2812 // State queries.
2813 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2814 << "Should query mappers and combine meta states.";
2815
2816 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2817 << "Should return unknown key code state when source not supported.";
2818 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2819 << "Should return unknown scan code state when source not supported.";
2820 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2821 << "Should return unknown switch state when source not supported.";
2822
2823 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2824 << "Should query mapper when source is supported.";
2825 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2826 << "Should query mapper when source is supported.";
2827 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2828 << "Should query mapper when source is supported.";
2829
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002830 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002831 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002832 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002833 << "Should do nothing when source is unsupported.";
2834 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2835 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2836 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2837 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2838
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002839 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002840 << "Should query mapper when source is supported.";
2841 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2842 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2843 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2844 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2845
2846 // Event handling.
2847 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002848 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002849 mDevice->process(&event, 1);
2850
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002851 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2852 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002853}
2854
Arthur Hung2c9a3342019-07-23 14:18:59 +08002855// A single input device is associated with a specific display. Check that:
2856// 1. Device is disabled if the viewport corresponding to the associated display is not found
2857// 2. Device is disabled when setEnabled API is called
2858TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002859 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002860
2861 // First Configuration.
2862 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2863
2864 // Device should be enabled by default.
2865 ASSERT_TRUE(mDevice->isEnabled());
2866
2867 // Prepare associated info.
2868 constexpr uint8_t hdmi = 1;
2869 const std::string UNIQUE_ID = "local:1";
2870
2871 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2872 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2873 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2874 // Device should be disabled because it is associated with a specific display via
2875 // input port <-> display port association, but the corresponding display is not found
2876 ASSERT_FALSE(mDevice->isEnabled());
2877
2878 // Prepare displays.
2879 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002880 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2881 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002882 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2883 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2884 ASSERT_TRUE(mDevice->isEnabled());
2885
2886 // Device should be disabled after set disable.
2887 mFakePolicy->addDisabledDevice(mDevice->getId());
2888 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2889 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2890 ASSERT_FALSE(mDevice->isEnabled());
2891
2892 // Device should still be disabled even found the associated display.
2893 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2894 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2895 ASSERT_FALSE(mDevice->isEnabled());
2896}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002897
Christine Franks1ba71cc2021-04-07 14:37:42 -07002898TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2899 // Device should be enabled by default.
2900 mFakePolicy->clearViewports();
2901 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2902 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2903 ASSERT_TRUE(mDevice->isEnabled());
2904
2905 // Device should be disabled because it is associated with a specific display, but the
2906 // corresponding display is not found.
2907 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
Christine Franks2a2293c2022-01-18 11:51:16 -08002908 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002909 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2910 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2911 ASSERT_FALSE(mDevice->isEnabled());
2912
2913 // Device should be enabled when a display is found.
2914 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2915 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2916 NO_PORT, ViewportType::INTERNAL);
2917 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2918 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2919 ASSERT_TRUE(mDevice->isEnabled());
2920
2921 // Device should be disabled after set disable.
2922 mFakePolicy->addDisabledDevice(mDevice->getId());
2923 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2924 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2925 ASSERT_FALSE(mDevice->isEnabled());
2926
2927 // Device should still be disabled even found the associated display.
2928 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2929 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2930 ASSERT_FALSE(mDevice->isEnabled());
2931}
2932
Christine Franks2a2293c2022-01-18 11:51:16 -08002933TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2934 mFakePolicy->clearViewports();
2935 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2936 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2937
2938 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2939 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2940 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2941 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2942 NO_PORT, ViewportType::INTERNAL);
2943 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2944 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2945 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2946}
2947
Michael Wrightd02c5b62014-02-10 15:10:22 -08002948// --- InputMapperTest ---
2949
2950class InputMapperTest : public testing::Test {
2951protected:
2952 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002953 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002954 static const int32_t DEVICE_ID;
2955 static const int32_t DEVICE_GENERATION;
2956 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002957 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002958 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002959
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002960 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002961 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002962 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002963 std::unique_ptr<InstrumentedInputReader> mReader;
2964 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002965
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002966 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002967 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002968 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002969 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002970 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002971 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002972 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002973 // Consume the device reset notification generated when adding a new device.
2974 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002975 }
2976
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002977 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002978 SetUp(DEVICE_CLASSES);
2979 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002980
Chris Yea52ade12020-08-27 16:49:20 -07002981 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002982 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002983 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002984 }
2985
2986 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002987 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002988 }
2989
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002990 void configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00002991 if (!changes ||
2992 (changes &
2993 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
2994 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002995 mReader->requestRefreshConfiguration(changes);
2996 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002997 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002998 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002999 // Loop the reader to flush the input listener queue.
3000 mReader->loopOnce();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003001 }
3002
arthurhungdcef2dc2020-08-11 14:47:50 +08003003 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
3004 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003005 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003006 InputDeviceIdentifier identifier;
3007 identifier.name = name;
3008 identifier.location = location;
3009 std::shared_ptr<InputDevice> device =
3010 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
3011 identifier);
3012 mReader->pushNextDevice(device);
3013 mFakeEventHub->addDevice(eventHubId, name, classes);
3014 mReader->loopOnce();
3015 return device;
3016 }
3017
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003018 template <class T, typename... Args>
3019 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003020 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003021 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003022 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07003023 mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003024 // Loop the reader to flush the input listener queue.
3025 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003026 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003027 }
3028
3029 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003030 int32_t orientation, const std::string& uniqueId,
3031 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003032 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3033 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003034 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3035 }
3036
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003037 void clearViewports() {
3038 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003039 }
3040
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003041 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
3042 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003043 RawEvent event;
3044 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003045 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003046 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003047 event.type = type;
3048 event.code = code;
3049 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003050 mapper.process(&event);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003051 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003052 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003053 }
3054
3055 static void assertMotionRange(const InputDeviceInfo& info,
3056 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3057 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003058 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003059 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3060 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3061 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3062 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3063 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3064 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3065 }
3066
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003067 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3068 float size, float touchMajor, float touchMinor, float toolMajor,
3069 float toolMinor, float orientation, float distance,
3070 float scaledAxisEpsilon = 1.f) {
3071 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3072 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003073 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3074 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003075 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3076 scaledAxisEpsilon);
3077 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3078 scaledAxisEpsilon);
3079 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3080 scaledAxisEpsilon);
3081 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3082 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003083 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3084 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3085 }
3086
Michael Wright17db18e2020-06-26 20:51:44 +01003087 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003088 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003089 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003090 ASSERT_NEAR(x, actualX, 1);
3091 ASSERT_NEAR(y, actualY, 1);
3092 }
3093};
3094
3095const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003096const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003097const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003098const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3099const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003100const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3101 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003102const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003103
3104// --- SwitchInputMapperTest ---
3105
3106class SwitchInputMapperTest : public InputMapperTest {
3107protected:
3108};
3109
3110TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003111 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003112
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003113 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003114}
3115
3116TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003117 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003118
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003119 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003120 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003121
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003122 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003123 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003124}
3125
3126TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003127 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003128
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003129 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3130 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3131 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3132 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003133
3134 NotifySwitchArgs args;
3135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3136 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003137 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3138 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003139 args.switchMask);
3140 ASSERT_EQ(uint32_t(0), args.policyFlags);
3141}
3142
Chris Ye87143712020-11-10 05:05:58 +00003143// --- VibratorInputMapperTest ---
3144class VibratorInputMapperTest : public InputMapperTest {
3145protected:
3146 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3147};
3148
3149TEST_F(VibratorInputMapperTest, GetSources) {
3150 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3151
3152 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3153}
3154
3155TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3156 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3157
3158 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3159}
3160
3161TEST_F(VibratorInputMapperTest, Vibrate) {
3162 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003163 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003164 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3165
3166 VibrationElement pattern(2);
3167 VibrationSequence sequence(2);
3168 pattern.duration = std::chrono::milliseconds(200);
3169 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3170 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3171 sequence.addElement(pattern);
3172 pattern.duration = std::chrono::milliseconds(500);
3173 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3174 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3175 sequence.addElement(pattern);
3176
3177 std::vector<int64_t> timings = {0, 1};
3178 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3179
3180 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003181 // Start vibrating
3182 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003183 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003184 // Verify vibrator state listener was notified.
3185 mReader->loopOnce();
3186 NotifyVibratorStateArgs args;
3187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3188 ASSERT_EQ(DEVICE_ID, args.deviceId);
3189 ASSERT_TRUE(args.isOn);
3190 // Stop vibrating
3191 mapper.cancelVibrate(VIBRATION_TOKEN);
3192 ASSERT_FALSE(mapper.isVibrating());
3193 // Verify vibrator state listener was notified.
3194 mReader->loopOnce();
3195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3196 ASSERT_EQ(DEVICE_ID, args.deviceId);
3197 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003198}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003199
Chris Yef59a2f42020-10-16 12:55:26 -07003200// --- SensorInputMapperTest ---
3201
3202class SensorInputMapperTest : public InputMapperTest {
3203protected:
3204 static const int32_t ACCEL_RAW_MIN;
3205 static const int32_t ACCEL_RAW_MAX;
3206 static const int32_t ACCEL_RAW_FUZZ;
3207 static const int32_t ACCEL_RAW_FLAT;
3208 static const int32_t ACCEL_RAW_RESOLUTION;
3209
3210 static const int32_t GYRO_RAW_MIN;
3211 static const int32_t GYRO_RAW_MAX;
3212 static const int32_t GYRO_RAW_FUZZ;
3213 static const int32_t GYRO_RAW_FLAT;
3214 static const int32_t GYRO_RAW_RESOLUTION;
3215
3216 static const float GRAVITY_MS2_UNIT;
3217 static const float DEGREE_RADIAN_UNIT;
3218
3219 void prepareAccelAxes();
3220 void prepareGyroAxes();
3221 void setAccelProperties();
3222 void setGyroProperties();
3223 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3224};
3225
3226const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3227const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3228const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3229const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3230const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3231
3232const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3233const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3234const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3235const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3236const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3237
3238const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3239const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3240
3241void SensorInputMapperTest::prepareAccelAxes() {
3242 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3243 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3244 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3245 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3246 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3247 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3248}
3249
3250void SensorInputMapperTest::prepareGyroAxes() {
3251 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3252 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3253 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3254 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3255 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3256 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3257}
3258
3259void SensorInputMapperTest::setAccelProperties() {
3260 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3261 /* sensorDataIndex */ 0);
3262 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3263 /* sensorDataIndex */ 1);
3264 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3265 /* sensorDataIndex */ 2);
3266 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3267 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3268 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3269 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3270 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3271}
3272
3273void SensorInputMapperTest::setGyroProperties() {
3274 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3275 /* sensorDataIndex */ 0);
3276 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3277 /* sensorDataIndex */ 1);
3278 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3279 /* sensorDataIndex */ 2);
3280 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3281 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3282 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3283 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3284 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3285}
3286
3287TEST_F(SensorInputMapperTest, GetSources) {
3288 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3289
3290 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3291}
3292
3293TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3294 setAccelProperties();
3295 prepareAccelAxes();
3296 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3297
3298 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3299 std::chrono::microseconds(10000),
3300 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003301 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3303 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3304 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3305 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3306 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003307
3308 NotifySensorArgs args;
3309 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3310 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3311 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3312
3313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3314 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3315 ASSERT_EQ(args.deviceId, DEVICE_ID);
3316 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3317 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3318 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3319 ASSERT_EQ(args.values, values);
3320 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3321}
3322
3323TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3324 setGyroProperties();
3325 prepareGyroAxes();
3326 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3327
3328 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3329 std::chrono::microseconds(10000),
3330 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003331 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003332 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3333 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3334 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3335 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3336 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003337
3338 NotifySensorArgs args;
3339 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3340 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3341 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3342
3343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3344 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3345 ASSERT_EQ(args.deviceId, DEVICE_ID);
3346 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3347 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3348 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3349 ASSERT_EQ(args.values, values);
3350 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3351}
3352
Michael Wrightd02c5b62014-02-10 15:10:22 -08003353// --- KeyboardInputMapperTest ---
3354
3355class KeyboardInputMapperTest : public InputMapperTest {
3356protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003357 const std::string UNIQUE_ID = "local:0";
3358
3359 void prepareDisplay(int32_t orientation);
3360
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003361 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003362 int32_t originalKeyCode, int32_t rotatedKeyCode,
3363 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003364};
3365
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003366/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3367 * orientation.
3368 */
3369void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003370 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3371 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003372}
3373
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003374void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003375 int32_t originalScanCode, int32_t originalKeyCode,
3376 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003377 NotifyKeyArgs args;
3378
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003379 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3381 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3382 ASSERT_EQ(originalScanCode, args.scanCode);
3383 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003384 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003385
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003386 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3388 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3389 ASSERT_EQ(originalScanCode, args.scanCode);
3390 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003391 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003392}
3393
Michael Wrightd02c5b62014-02-10 15:10:22 -08003394TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003395 KeyboardInputMapper& mapper =
3396 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3397 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003398
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003399 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003400}
3401
3402TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3403 const int32_t USAGE_A = 0x070004;
3404 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003405 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3406 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003407 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3408 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3409 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003410
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003411 KeyboardInputMapper& mapper =
3412 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3413 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003414 // Initial metastate is AMETA_NONE.
3415 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003416
3417 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003418 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003419 NotifyKeyArgs args;
3420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3421 ASSERT_EQ(DEVICE_ID, args.deviceId);
3422 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3423 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3424 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3425 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3426 ASSERT_EQ(KEY_HOME, args.scanCode);
3427 ASSERT_EQ(AMETA_NONE, args.metaState);
3428 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3429 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3430 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3431
3432 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003433 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3435 ASSERT_EQ(DEVICE_ID, args.deviceId);
3436 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3437 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3438 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3439 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3440 ASSERT_EQ(KEY_HOME, args.scanCode);
3441 ASSERT_EQ(AMETA_NONE, args.metaState);
3442 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3443 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3444 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3445
3446 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003447 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3448 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3450 ASSERT_EQ(DEVICE_ID, args.deviceId);
3451 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3452 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3453 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3454 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3455 ASSERT_EQ(0, args.scanCode);
3456 ASSERT_EQ(AMETA_NONE, args.metaState);
3457 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3458 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3459 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3460
3461 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003462 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3463 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3465 ASSERT_EQ(DEVICE_ID, args.deviceId);
3466 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3467 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3468 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3469 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3470 ASSERT_EQ(0, args.scanCode);
3471 ASSERT_EQ(AMETA_NONE, args.metaState);
3472 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3473 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3474 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3475
3476 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003477 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3478 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3480 ASSERT_EQ(DEVICE_ID, args.deviceId);
3481 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3482 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3483 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3484 ASSERT_EQ(0, args.keyCode);
3485 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3486 ASSERT_EQ(AMETA_NONE, args.metaState);
3487 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3488 ASSERT_EQ(0U, args.policyFlags);
3489 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3490
3491 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003492 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3493 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3495 ASSERT_EQ(DEVICE_ID, args.deviceId);
3496 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3497 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3498 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3499 ASSERT_EQ(0, args.keyCode);
3500 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3501 ASSERT_EQ(AMETA_NONE, args.metaState);
3502 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3503 ASSERT_EQ(0U, args.policyFlags);
3504 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3505}
3506
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003507/**
3508 * Ensure that the readTime is set to the time when the EV_KEY is received.
3509 */
3510TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3511 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3512
3513 KeyboardInputMapper& mapper =
3514 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3515 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3516 NotifyKeyArgs args;
3517
3518 // Key down
3519 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3521 ASSERT_EQ(12, args.readTime);
3522
3523 // Key up
3524 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3526 ASSERT_EQ(15, args.readTime);
3527}
3528
Michael Wrightd02c5b62014-02-10 15:10:22 -08003529TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003530 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3531 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003532 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3533 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3534 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003535
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003536 KeyboardInputMapper& mapper =
3537 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3538 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003539
Arthur Hung95f68612022-04-07 14:08:22 +08003540 // Initial metastate is AMETA_NONE.
3541 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003542
3543 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003544 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003545 NotifyKeyArgs args;
3546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3547 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003548 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003549 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003550
3551 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003552 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3554 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003555 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003556
3557 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003558 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3560 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003561 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003562
3563 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003564 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3566 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003567 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003568 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003569}
3570
3571TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003572 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3573 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3574 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3575 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 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
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003581 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003582 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3583 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3584 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3585 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3586 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3587 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3588 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3589 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3590}
3591
3592TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003593 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3594 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3595 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3596 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003597
Michael Wrightd02c5b62014-02-10 15:10:22 -08003598 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003599 KeyboardInputMapper& mapper =
3600 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3601 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003602
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003603 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003604 ASSERT_NO_FATAL_FAILURE(
3605 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3606 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3607 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3608 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3609 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3610 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3611 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003612
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003613 clearViewports();
3614 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003615 ASSERT_NO_FATAL_FAILURE(
3616 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3617 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3618 AKEYCODE_DPAD_UP, DISPLAY_ID));
3619 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3620 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3621 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3622 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003623
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003624 clearViewports();
3625 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003626 ASSERT_NO_FATAL_FAILURE(
3627 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3628 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3629 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3630 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3631 AKEYCODE_DPAD_UP, DISPLAY_ID));
3632 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3633 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003634
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003635 clearViewports();
3636 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003637 ASSERT_NO_FATAL_FAILURE(
3638 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3639 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3640 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3641 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3642 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3643 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3644 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003645
3646 // Special case: if orientation changes while key is down, we still emit the same keycode
3647 // in the key up as we did in the key down.
3648 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003649 clearViewports();
3650 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003651 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3653 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3654 ASSERT_EQ(KEY_UP, args.scanCode);
3655 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3656
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003657 clearViewports();
3658 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003659 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3661 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3662 ASSERT_EQ(KEY_UP, args.scanCode);
3663 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3664}
3665
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003666TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3667 // If the keyboard is not orientation aware,
3668 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003669 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003670
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003671 KeyboardInputMapper& mapper =
3672 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3673 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003674 NotifyKeyArgs args;
3675
3676 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003677 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003679 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3681 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3682
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003683 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003684 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3688 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3689}
3690
3691TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3692 // If the keyboard is orientation aware,
3693 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003694 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003695
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003696 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003697 KeyboardInputMapper& mapper =
3698 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3699 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003700 NotifyKeyArgs args;
3701
3702 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3703 // ^--- already checked by the previous test
3704
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003705 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003706 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003707 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003709 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3711 ASSERT_EQ(DISPLAY_ID, args.displayId);
3712
3713 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003714 clearViewports();
3715 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003716 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003717 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003719 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3721 ASSERT_EQ(newDisplayId, args.displayId);
3722}
3723
Michael Wrightd02c5b62014-02-10 15:10:22 -08003724TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003725 KeyboardInputMapper& mapper =
3726 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3727 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003728
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003729 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003730 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003731
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003732 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003733 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003734}
3735
Philip Junker4af3b3d2021-12-14 10:36:55 +01003736TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3737 KeyboardInputMapper& mapper =
3738 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3739 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3740
3741 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3742 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3743 << "If a mapping is available, the result is equal to the mapping";
3744
3745 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3746 << "If no mapping is available, the result is the key location";
3747}
3748
Michael Wrightd02c5b62014-02-10 15:10:22 -08003749TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003750 KeyboardInputMapper& mapper =
3751 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3752 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003753
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003754 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003755 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003756
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003757 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003758 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003759}
3760
3761TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003762 KeyboardInputMapper& mapper =
3763 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3764 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003765
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003766 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003767
Michael Wrightd02c5b62014-02-10 15:10:22 -08003768 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003769 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003770 ASSERT_TRUE(flags[0]);
3771 ASSERT_FALSE(flags[1]);
3772}
3773
3774TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003775 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3776 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3777 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3778 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3779 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3780 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003781
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003782 KeyboardInputMapper& mapper =
3783 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3784 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003785 // Initial metastate is AMETA_NONE.
3786 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003787
3788 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003789 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3790 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3791 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003792
3793 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3795 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003796 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3797 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3798 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003799 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003800
3801 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003804 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3805 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3806 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003807 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003808
3809 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003810 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3811 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003812 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3813 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3814 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003815 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003816
3817 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003818 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3819 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003820 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3821 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3822 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003823 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003824
3825 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003826 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3827 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003828 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3829 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3830 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003831 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003832
3833 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003834 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3835 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003836 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3837 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3838 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003839 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003840}
3841
Chris Yea52ade12020-08-27 16:49:20 -07003842TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3843 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3844 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3845 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3846 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3847
3848 KeyboardInputMapper& mapper =
3849 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3850 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3851
Chris Yea52ade12020-08-27 16:49:20 -07003852 // Meta state should be AMETA_NONE after reset
3853 mapper.reset(ARBITRARY_TIME);
3854 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3855 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3856 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3857 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3858
3859 NotifyKeyArgs args;
3860 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003861 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3863 ASSERT_EQ(AMETA_NONE, args.metaState);
3864 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3865 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3866 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3867
3868 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003869 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3871 ASSERT_EQ(AMETA_NONE, args.metaState);
3872 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3873 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3874 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3875}
3876
Arthur Hung2c9a3342019-07-23 14:18:59 +08003877TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3878 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003879 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3880 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3881 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3882 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003883
3884 // keyboard 2.
3885 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003886 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003887 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003888 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003889 std::shared_ptr<InputDevice> device2 =
3890 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003891 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003892
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003893 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3894 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3895 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3896 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003897
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003898 KeyboardInputMapper& mapper =
3899 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3900 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003901
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003902 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003903 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003904 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003905 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3906 device2->reset(ARBITRARY_TIME);
3907
3908 // Prepared displays and associated info.
3909 constexpr uint8_t hdmi1 = 0;
3910 constexpr uint8_t hdmi2 = 1;
3911 const std::string SECONDARY_UNIQUE_ID = "local:1";
3912
3913 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3914 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3915
3916 // No associated display viewport found, should disable the device.
3917 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3918 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3919 ASSERT_FALSE(device2->isEnabled());
3920
3921 // Prepare second display.
3922 constexpr int32_t newDisplayId = 2;
3923 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003924 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003925 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003926 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003927 // Default device will reconfigure above, need additional reconfiguration for another device.
3928 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3929 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3930
3931 // Device should be enabled after the associated display is found.
3932 ASSERT_TRUE(mDevice->isEnabled());
3933 ASSERT_TRUE(device2->isEnabled());
3934
3935 // Test pad key events
3936 ASSERT_NO_FATAL_FAILURE(
3937 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3938 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3939 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3940 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3941 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3942 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3943 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3944
3945 ASSERT_NO_FATAL_FAILURE(
3946 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3947 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3948 AKEYCODE_DPAD_RIGHT, newDisplayId));
3949 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3950 AKEYCODE_DPAD_DOWN, newDisplayId));
3951 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3952 AKEYCODE_DPAD_LEFT, newDisplayId));
3953}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003954
arthurhungc903df12020-08-11 15:08:42 +08003955TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3956 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3957 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3958 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3959 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3960 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3961 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3962
3963 KeyboardInputMapper& mapper =
3964 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3965 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003966 // Initial metastate is AMETA_NONE.
3967 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003968
3969 // Initialization should have turned all of the lights off.
3970 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3971 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3972 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3973
3974 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003975 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3976 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003977 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3978 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3979
3980 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003981 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3982 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003983 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3984 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3985
3986 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003987 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3988 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003989 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3990 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3991
3992 mFakeEventHub->removeDevice(EVENTHUB_ID);
3993 mReader->loopOnce();
3994
3995 // keyboard 2 should default toggle keys.
3996 const std::string USB2 = "USB2";
3997 const std::string DEVICE_NAME2 = "KEYBOARD2";
3998 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3999 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4000 std::shared_ptr<InputDevice> device2 =
4001 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004002 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08004003 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4004 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4005 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4006 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4007 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4008 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4009
arthurhung6fe95782020-10-05 22:41:16 +08004010 KeyboardInputMapper& mapper2 =
4011 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4012 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08004013 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4014 device2->reset(ARBITRARY_TIME);
4015
4016 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4017 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4018 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004019 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4020 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004021}
4022
Arthur Hungcb40a002021-08-03 14:31:01 +00004023TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4024 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4025 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4026 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4027
4028 // Suppose we have two mappers. (DPAD + KEYBOARD)
4029 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4030 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4031 KeyboardInputMapper& mapper =
4032 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4033 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004034 // Initial metastate is AMETA_NONE.
4035 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004036
4037 mReader->toggleCapsLockState(DEVICE_ID);
4038 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4039}
4040
Arthur Hungfb3cc112022-04-13 07:39:50 +00004041TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4042 // keyboard 1.
4043 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4044 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4045 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4046 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4047 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4048 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4049
4050 KeyboardInputMapper& mapper1 =
4051 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4052 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4053
4054 // keyboard 2.
4055 const std::string USB2 = "USB2";
4056 const std::string DEVICE_NAME2 = "KEYBOARD2";
4057 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4058 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4059 std::shared_ptr<InputDevice> device2 =
4060 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4061 ftl::Flags<InputDeviceClass>(0));
4062 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4063 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4064 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4065 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4066 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4067 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4068
4069 KeyboardInputMapper& mapper2 =
4070 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4071 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4072 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4073 device2->reset(ARBITRARY_TIME);
4074
Arthur Hung95f68612022-04-07 14:08:22 +08004075 // Initial metastate is AMETA_NONE.
4076 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4077 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4078
4079 // Toggle num lock on and off.
4080 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4081 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004082 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4083 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4084 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4085
4086 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4087 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4088 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4089 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4090 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4091
4092 // Toggle caps lock on and off.
4093 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4094 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4095 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4096 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4097 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4098
4099 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4100 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4101 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4102 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4103 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4104
4105 // Toggle scroll lock on and off.
4106 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4107 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4108 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4109 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4110 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4111
4112 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4113 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4114 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4115 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4116 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4117}
4118
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004119// --- KeyboardInputMapperTest_ExternalDevice ---
4120
4121class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4122protected:
Chris Yea52ade12020-08-27 16:49:20 -07004123 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004124};
4125
4126TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004127 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4128 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004129
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004130 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4131 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4132 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4133 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004134
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004135 KeyboardInputMapper& mapper =
4136 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4137 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004138
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004140 NotifyKeyArgs args;
4141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4142 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4143
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004144 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4146 ASSERT_EQ(uint32_t(0), args.policyFlags);
4147
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004148 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4150 ASSERT_EQ(uint32_t(0), args.policyFlags);
4151
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004152 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4154 ASSERT_EQ(uint32_t(0), args.policyFlags);
4155
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004156 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4158 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4159
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004160 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4162 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4163}
4164
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004165TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004166 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004167
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004168 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4169 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4170 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004171
Powei Fengd041c5d2019-05-03 17:11:33 -07004172 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004173 KeyboardInputMapper& mapper =
4174 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4175 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004176
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004177 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004178 NotifyKeyArgs args;
4179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4180 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4181
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004182 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4184 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4185
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004186 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4188 ASSERT_EQ(uint32_t(0), args.policyFlags);
4189
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004190 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4192 ASSERT_EQ(uint32_t(0), args.policyFlags);
4193
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004194 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4196 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4197
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004198 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4200 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4201}
4202
Michael Wrightd02c5b62014-02-10 15:10:22 -08004203// --- CursorInputMapperTest ---
4204
4205class CursorInputMapperTest : public InputMapperTest {
4206protected:
4207 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4208
Michael Wright17db18e2020-06-26 20:51:44 +01004209 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004210
Chris Yea52ade12020-08-27 16:49:20 -07004211 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004212 InputMapperTest::SetUp();
4213
Michael Wright17db18e2020-06-26 20:51:44 +01004214 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004215 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004216 }
4217
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004218 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4219 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004220
4221 void prepareDisplay(int32_t orientation) {
4222 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004223 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004224 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4225 orientation, uniqueId, NO_PORT, viewportType);
4226 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004227
4228 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4229 float pressure) {
4230 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4231 0.0f, 0.0f, 0.0f, EPSILON));
4232 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004233};
4234
4235const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4236
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004237void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4238 int32_t originalY, int32_t rotatedX,
4239 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004240 NotifyMotionArgs args;
4241
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004242 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4243 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4244 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4246 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004247 ASSERT_NO_FATAL_FAILURE(
4248 assertCursorPointerCoords(args.pointerCoords[0],
4249 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4250 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004251}
4252
4253TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004254 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004255 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004256
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004257 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004258}
4259
4260TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004261 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004262 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004263
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004264 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004265}
4266
4267TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004268 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004269 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004270
4271 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004272 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004273
4274 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004275 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4276 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004277 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4278 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4279
4280 // When the bounds are set, then there should be a valid motion range.
4281 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4282
4283 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004284 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004285
4286 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4287 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4288 1, 800 - 1, 0.0f, 0.0f));
4289 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4290 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4291 2, 480 - 1, 0.0f, 0.0f));
4292 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4293 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4294 0.0f, 1.0f, 0.0f, 0.0f));
4295}
4296
4297TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004298 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004299 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004300
4301 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004302 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004303
4304 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4305 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4306 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4307 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4308 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4309 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4310 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4311 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4312 0.0f, 1.0f, 0.0f, 0.0f));
4313}
4314
4315TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004316 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004317 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004318
arthurhungdcef2dc2020-08-11 14:47:50 +08004319 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004320
4321 NotifyMotionArgs args;
4322
4323 // Button press.
4324 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004325 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4326 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4328 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4329 ASSERT_EQ(DEVICE_ID, args.deviceId);
4330 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4331 ASSERT_EQ(uint32_t(0), args.policyFlags);
4332 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4333 ASSERT_EQ(0, args.flags);
4334 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4335 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4336 ASSERT_EQ(0, args.edgeFlags);
4337 ASSERT_EQ(uint32_t(1), args.pointerCount);
4338 ASSERT_EQ(0, args.pointerProperties[0].id);
4339 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004340 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004341 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4342 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4343 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4344
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4346 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4347 ASSERT_EQ(DEVICE_ID, args.deviceId);
4348 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4349 ASSERT_EQ(uint32_t(0), args.policyFlags);
4350 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4351 ASSERT_EQ(0, args.flags);
4352 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4353 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4354 ASSERT_EQ(0, args.edgeFlags);
4355 ASSERT_EQ(uint32_t(1), args.pointerCount);
4356 ASSERT_EQ(0, args.pointerProperties[0].id);
4357 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004358 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004359 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4360 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4361 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4362
Michael Wrightd02c5b62014-02-10 15:10:22 -08004363 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004364 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4365 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4367 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4368 ASSERT_EQ(DEVICE_ID, args.deviceId);
4369 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4370 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004371 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4372 ASSERT_EQ(0, args.flags);
4373 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4374 ASSERT_EQ(0, args.buttonState);
4375 ASSERT_EQ(0, args.edgeFlags);
4376 ASSERT_EQ(uint32_t(1), args.pointerCount);
4377 ASSERT_EQ(0, args.pointerProperties[0].id);
4378 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004379 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004380 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4381 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4382 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4383
4384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4385 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4386 ASSERT_EQ(DEVICE_ID, args.deviceId);
4387 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4388 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004389 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4390 ASSERT_EQ(0, args.flags);
4391 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4392 ASSERT_EQ(0, args.buttonState);
4393 ASSERT_EQ(0, args.edgeFlags);
4394 ASSERT_EQ(uint32_t(1), args.pointerCount);
4395 ASSERT_EQ(0, args.pointerProperties[0].id);
4396 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004397 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004398 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4399 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4400 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4401}
4402
4403TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004404 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004405 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004406
4407 NotifyMotionArgs args;
4408
4409 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004410 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4411 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4413 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004414 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4415 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4416 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004417
4418 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004419 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4420 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4422 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004423 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4424 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004425}
4426
4427TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004428 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004429 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004430
4431 NotifyMotionArgs args;
4432
4433 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004434 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4435 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4437 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004438 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004439
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4441 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004442 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004443
Michael Wrightd02c5b62014-02-10 15:10:22 -08004444 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004445 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004448 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004449 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004450
4451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004452 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004453 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004454}
4455
4456TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004457 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004458 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004459
4460 NotifyMotionArgs args;
4461
4462 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004463 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4464 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4465 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4466 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4468 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004469 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4470 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4471 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004472
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4474 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004475 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4476 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4477 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004478
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004480 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4481 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4482 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4484 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004485 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4486 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4487 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488
4489 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004490 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4491 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004493 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004494 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004495
4496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004497 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004498 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004499}
4500
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004501TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004502 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004503 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4504 // need to be rotated.
4505 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004506 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004507
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004508 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004509 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4510 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4511 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4512 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4513 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4514 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4515 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4516 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4517}
4518
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004519TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004520 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004521 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4522 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004523 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004524
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004525 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004526 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4527 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4528 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4529 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4530 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4531 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4532 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4533 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4534
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004535 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004536 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4537 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4538 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4539 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4540 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4541 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4542 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4543 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004544
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004545 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004546 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4547 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4548 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4549 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4550 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4551 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4552 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4553 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4554
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004555 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004556 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4557 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4558 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4559 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4560 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4561 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4562 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4563 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004564}
4565
4566TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004567 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004568 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004569
4570 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4571 mFakePointerController->setPosition(100, 200);
4572 mFakePointerController->setButtonState(0);
4573
4574 NotifyMotionArgs motionArgs;
4575 NotifyKeyArgs keyArgs;
4576
4577 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004578 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4579 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4581 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4582 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4583 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004584 ASSERT_NO_FATAL_FAILURE(
4585 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004586
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4588 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4589 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4590 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004591 ASSERT_NO_FATAL_FAILURE(
4592 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004593
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004594 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4595 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004597 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004598 ASSERT_EQ(0, motionArgs.buttonState);
4599 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004600 ASSERT_NO_FATAL_FAILURE(
4601 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004602
4603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004604 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004605 ASSERT_EQ(0, motionArgs.buttonState);
4606 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004607 ASSERT_NO_FATAL_FAILURE(
4608 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004609
4610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004611 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004612 ASSERT_EQ(0, motionArgs.buttonState);
4613 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004614 ASSERT_NO_FATAL_FAILURE(
4615 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004616
4617 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004618 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4619 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4620 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4622 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4623 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4624 motionArgs.buttonState);
4625 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4626 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004627 ASSERT_NO_FATAL_FAILURE(
4628 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004629
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4631 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4632 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4633 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4634 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004635 ASSERT_NO_FATAL_FAILURE(
4636 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004637
4638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4639 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4640 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4641 motionArgs.buttonState);
4642 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4643 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004644 ASSERT_NO_FATAL_FAILURE(
4645 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004646
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004647 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4648 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004650 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004651 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4652 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004653 ASSERT_NO_FATAL_FAILURE(
4654 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004655
4656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004657 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004658 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4659 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004660 ASSERT_NO_FATAL_FAILURE(
4661 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004662
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004663 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4664 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004666 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4667 ASSERT_EQ(0, motionArgs.buttonState);
4668 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004669 ASSERT_NO_FATAL_FAILURE(
4670 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004671 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4672 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004673
4674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004675 ASSERT_EQ(0, motionArgs.buttonState);
4676 ASSERT_EQ(0, mFakePointerController->getButtonState());
4677 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004678 ASSERT_NO_FATAL_FAILURE(
4679 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004680
Michael Wrightd02c5b62014-02-10 15:10:22 -08004681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4682 ASSERT_EQ(0, motionArgs.buttonState);
4683 ASSERT_EQ(0, mFakePointerController->getButtonState());
4684 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004685 ASSERT_NO_FATAL_FAILURE(
4686 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004687
4688 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004689 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4690 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4692 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4693 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004694
Michael Wrightd02c5b62014-02-10 15:10:22 -08004695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004696 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004697 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4698 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004699 ASSERT_NO_FATAL_FAILURE(
4700 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004701
4702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4703 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4704 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4705 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004706 ASSERT_NO_FATAL_FAILURE(
4707 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004708
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004709 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4710 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004712 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004713 ASSERT_EQ(0, motionArgs.buttonState);
4714 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004715 ASSERT_NO_FATAL_FAILURE(
4716 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004717
4718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004719 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004720 ASSERT_EQ(0, motionArgs.buttonState);
4721 ASSERT_EQ(0, mFakePointerController->getButtonState());
4722
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004723 ASSERT_NO_FATAL_FAILURE(
4724 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4726 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4727 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4728
4729 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004730 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4731 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4733 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4734 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004735
Michael Wrightd02c5b62014-02-10 15:10:22 -08004736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004737 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004738 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4739 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004740 ASSERT_NO_FATAL_FAILURE(
4741 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004742
4743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4744 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4745 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4746 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004747 ASSERT_NO_FATAL_FAILURE(
4748 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004749
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004750 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4751 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004753 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004754 ASSERT_EQ(0, motionArgs.buttonState);
4755 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004756 ASSERT_NO_FATAL_FAILURE(
4757 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004758
4759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4760 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4761 ASSERT_EQ(0, motionArgs.buttonState);
4762 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004763 ASSERT_NO_FATAL_FAILURE(
4764 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004765
Michael Wrightd02c5b62014-02-10 15:10:22 -08004766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4767 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4768 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4769
4770 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004771 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 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_FORWARD, 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_FORWARD, motionArgs.buttonState);
4780 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, 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_FORWARD, motionArgs.buttonState);
4787 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, 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_FORWARD, 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));
4801 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4802 ASSERT_EQ(0, motionArgs.buttonState);
4803 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004804 ASSERT_NO_FATAL_FAILURE(
4805 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004806
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_FORWARD, keyArgs.keyCode);
4810
4811 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004812 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 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_FORWARD, 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_FORWARD, motionArgs.buttonState);
4821 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, 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_FORWARD, motionArgs.buttonState);
4828 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, 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_EXTRA, 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_FORWARD, keyArgs.keyCode);
4851}
4852
4853TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004854 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004855 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004856
4857 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4858 mFakePointerController->setPosition(100, 200);
4859 mFakePointerController->setButtonState(0);
4860
4861 NotifyMotionArgs args;
4862
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004863 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4864 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4865 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004867 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4868 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4869 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4870 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 +01004871 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004872}
4873
4874TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004875 addConfigurationProperty("cursor.mode", "pointer");
4876 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004877 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004878
4879 NotifyDeviceResetArgs resetArgs;
4880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4881 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4882 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4883
4884 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4885 mFakePointerController->setPosition(100, 200);
4886 mFakePointerController->setButtonState(0);
4887
4888 NotifyMotionArgs args;
4889
4890 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004891 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4892 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4893 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4895 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4896 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4897 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4898 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 +01004899 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004900
4901 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004902 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4903 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4905 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4906 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4907 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4908 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4910 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4911 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4912 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4913 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4914
4915 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004916 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4917 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4919 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4920 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4921 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4922 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4924 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4925 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4926 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4927 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4928
4929 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004930 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4931 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4932 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4934 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4935 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4936 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4937 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 +01004938 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004939
4940 // Disable pointer capture and check that the device generation got bumped
4941 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004942 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004943 mFakePolicy->setPointerCapture(false);
4944 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004945 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004946
4947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004948 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4949
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4951 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4952 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4954 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004955 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4956 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4957 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 +01004958 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004959}
4960
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004961/**
4962 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4963 * pointer acceleration or speed processing should not be applied.
4964 */
4965TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4966 addConfigurationProperty("cursor.mode", "pointer");
4967 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4968 100.f /*high threshold*/, 10.f /*acceleration*/);
4969 mFakePolicy->setVelocityControlParams(testParams);
4970 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4971
4972 NotifyDeviceResetArgs resetArgs;
4973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4974 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4975 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4976
4977 NotifyMotionArgs args;
4978
4979 // Move and verify scale is applied.
4980 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4981 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4982 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4984 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4985 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4986 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4987 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4988 ASSERT_GT(relX, 10);
4989 ASSERT_GT(relY, 20);
4990
4991 // Enable Pointer Capture
4992 mFakePolicy->setPointerCapture(true);
4993 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4994 NotifyPointerCaptureChangedArgs captureArgs;
4995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4996 ASSERT_TRUE(captureArgs.request.enable);
4997
4998 // Move and verify scale is not applied.
4999 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5000 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5001 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5003 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5004 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5005 ASSERT_EQ(10, args.pointerCoords[0].getX());
5006 ASSERT_EQ(20, args.pointerCoords[0].getY());
5007}
5008
Prabir Pradhan208360b2022-06-24 18:37:04 +00005009TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5010 addConfigurationProperty("cursor.mode", "pointer");
5011 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5012
5013 NotifyDeviceResetArgs resetArgs;
5014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5015 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5016 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5017
5018 // Ensure the display is rotated.
5019 prepareDisplay(DISPLAY_ORIENTATION_90);
5020
5021 NotifyMotionArgs args;
5022
5023 // Verify that the coordinates are rotated.
5024 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5025 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5026 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5028 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5029 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5030 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5031 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5032
5033 // Enable Pointer Capture.
5034 mFakePolicy->setPointerCapture(true);
5035 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5036 NotifyPointerCaptureChangedArgs captureArgs;
5037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5038 ASSERT_TRUE(captureArgs.request.enable);
5039
5040 // Move and verify rotation is not applied.
5041 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5042 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5043 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5045 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5046 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5047 ASSERT_EQ(10, args.pointerCoords[0].getX());
5048 ASSERT_EQ(20, args.pointerCoords[0].getY());
5049}
5050
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005051TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005052 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005053
Garfield Tan888a6a42020-01-09 11:39:16 -08005054 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005055 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08005056 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
5057 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00005058 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
5059 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08005060 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
5061 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5062
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005063 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5064 mFakePointerController->setPosition(100, 200);
5065 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005066
5067 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005068 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5069 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5070 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5072 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5073 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5074 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5075 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 +01005076 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005077 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
5078}
5079
Michael Wrightd02c5b62014-02-10 15:10:22 -08005080// --- TouchInputMapperTest ---
5081
5082class TouchInputMapperTest : public InputMapperTest {
5083protected:
5084 static const int32_t RAW_X_MIN;
5085 static const int32_t RAW_X_MAX;
5086 static const int32_t RAW_Y_MIN;
5087 static const int32_t RAW_Y_MAX;
5088 static const int32_t RAW_TOUCH_MIN;
5089 static const int32_t RAW_TOUCH_MAX;
5090 static const int32_t RAW_TOOL_MIN;
5091 static const int32_t RAW_TOOL_MAX;
5092 static const int32_t RAW_PRESSURE_MIN;
5093 static const int32_t RAW_PRESSURE_MAX;
5094 static const int32_t RAW_ORIENTATION_MIN;
5095 static const int32_t RAW_ORIENTATION_MAX;
5096 static const int32_t RAW_DISTANCE_MIN;
5097 static const int32_t RAW_DISTANCE_MAX;
5098 static const int32_t RAW_TILT_MIN;
5099 static const int32_t RAW_TILT_MAX;
5100 static const int32_t RAW_ID_MIN;
5101 static const int32_t RAW_ID_MAX;
5102 static const int32_t RAW_SLOT_MIN;
5103 static const int32_t RAW_SLOT_MAX;
5104 static const float X_PRECISION;
5105 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005106 static const float X_PRECISION_VIRTUAL;
5107 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005108
5109 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005110 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005111
5112 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5113
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005114 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005115 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005116
Michael Wrightd02c5b62014-02-10 15:10:22 -08005117 enum Axes {
5118 POSITION = 1 << 0,
5119 TOUCH = 1 << 1,
5120 TOOL = 1 << 2,
5121 PRESSURE = 1 << 3,
5122 ORIENTATION = 1 << 4,
5123 MINOR = 1 << 5,
5124 ID = 1 << 6,
5125 DISTANCE = 1 << 7,
5126 TILT = 1 << 8,
5127 SLOT = 1 << 9,
5128 TOOL_TYPE = 1 << 10,
5129 };
5130
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005131 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5132 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005133 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005134 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005135 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005136 int32_t toRawX(float displayX);
5137 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005138 int32_t toRotatedRawX(float displayX);
5139 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005140 float toCookedX(float rawX, float rawY);
5141 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005142 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005143 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005144 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005145 float toDisplayY(int32_t rawY, int32_t displayHeight);
5146
Michael Wrightd02c5b62014-02-10 15:10:22 -08005147};
5148
5149const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5150const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5151const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5152const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5153const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5154const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5155const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5156const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005157const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5158const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005159const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5160const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5161const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5162const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5163const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5164const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5165const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5166const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5167const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5168const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5169const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5170const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005171const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5172 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5173const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5174 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005175const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5176 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005177
5178const float TouchInputMapperTest::GEOMETRIC_SCALE =
5179 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5180 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5181
5182const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5183 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5184 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5185};
5186
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005187void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005188 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5189 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005190}
5191
5192void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5193 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5194 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005195}
5196
Santos Cordonfa5cf462017-04-05 10:37:00 -07005197void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005198 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5199 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5200 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005201}
5202
Michael Wrightd02c5b62014-02-10 15:10:22 -08005203void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005204 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5205 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5206 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5207 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005208}
5209
Jason Gerecke489fda82012-09-07 17:19:40 -07005210void TouchInputMapperTest::prepareLocationCalibration() {
5211 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5212}
5213
Michael Wrightd02c5b62014-02-10 15:10:22 -08005214int32_t TouchInputMapperTest::toRawX(float displayX) {
5215 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5216}
5217
5218int32_t TouchInputMapperTest::toRawY(float displayY) {
5219 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5220}
5221
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005222int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5223 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5224}
5225
5226int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5227 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5228}
5229
Jason Gerecke489fda82012-09-07 17:19:40 -07005230float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5231 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5232 return rawX;
5233}
5234
5235float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5236 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5237 return rawY;
5238}
5239
Michael Wrightd02c5b62014-02-10 15:10:22 -08005240float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005241 return toDisplayX(rawX, DISPLAY_WIDTH);
5242}
5243
5244float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5245 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005246}
5247
5248float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005249 return toDisplayY(rawY, DISPLAY_HEIGHT);
5250}
5251
5252float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5253 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005254}
5255
5256
5257// --- SingleTouchInputMapperTest ---
5258
5259class SingleTouchInputMapperTest : public TouchInputMapperTest {
5260protected:
5261 void prepareButtons();
5262 void prepareAxes(int axes);
5263
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005264 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5265 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5266 void processUp(SingleTouchInputMapper& mappery);
5267 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5268 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5269 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5270 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5271 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5272 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005273};
5274
5275void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005276 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005277}
5278
5279void SingleTouchInputMapperTest::prepareAxes(int axes) {
5280 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005281 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5282 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005283 }
5284 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005285 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5286 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005287 }
5288 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005289 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5290 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005291 }
5292 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005293 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5294 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005295 }
5296 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005297 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5298 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005299 }
5300}
5301
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005302void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005303 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5304 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5305 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005306}
5307
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005308void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005309 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5310 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005311}
5312
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005313void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005314 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005315}
5316
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005317void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005318 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005319}
5320
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005321void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5322 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005323 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005324}
5325
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005326void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005327 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005328}
5329
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005330void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5331 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005332 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5333 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005334}
5335
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005336void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5337 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005338 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005339}
5340
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005341void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005342 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005343}
5344
Michael Wrightd02c5b62014-02-10 15:10:22 -08005345TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005346 prepareButtons();
5347 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005348 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005349
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005350 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005351}
5352
Michael Wrightd02c5b62014-02-10 15:10:22 -08005353TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005354 prepareButtons();
5355 prepareAxes(POSITION);
5356 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005357 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005358
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005359 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005360}
5361
5362TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005363 addConfigurationProperty("touch.deviceType", "touchScreen");
5364 prepareDisplay(DISPLAY_ORIENTATION_0);
5365 prepareButtons();
5366 prepareAxes(POSITION);
5367 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005368 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005369
5370 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005371 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005372
5373 // Virtual key is down.
5374 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5375 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5376 processDown(mapper, x, y);
5377 processSync(mapper);
5378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5379
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005380 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005381
5382 // Virtual key is up.
5383 processUp(mapper);
5384 processSync(mapper);
5385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5386
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005387 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005388}
5389
5390TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005391 addConfigurationProperty("touch.deviceType", "touchScreen");
5392 prepareDisplay(DISPLAY_ORIENTATION_0);
5393 prepareButtons();
5394 prepareAxes(POSITION);
5395 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005396 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005397
5398 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005399 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005400
5401 // Virtual key is down.
5402 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5403 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5404 processDown(mapper, x, y);
5405 processSync(mapper);
5406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5407
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005408 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005409
5410 // Virtual key is up.
5411 processUp(mapper);
5412 processSync(mapper);
5413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5414
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005415 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005416}
5417
5418TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005419 addConfigurationProperty("touch.deviceType", "touchScreen");
5420 prepareDisplay(DISPLAY_ORIENTATION_0);
5421 prepareButtons();
5422 prepareAxes(POSITION);
5423 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005424 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005425
Michael Wrightd02c5b62014-02-10 15:10:22 -08005426 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005427 ASSERT_TRUE(
5428 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005429 ASSERT_TRUE(flags[0]);
5430 ASSERT_FALSE(flags[1]);
5431}
5432
5433TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005434 addConfigurationProperty("touch.deviceType", "touchScreen");
5435 prepareDisplay(DISPLAY_ORIENTATION_0);
5436 prepareButtons();
5437 prepareAxes(POSITION);
5438 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005439 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005440
arthurhungdcef2dc2020-08-11 14:47:50 +08005441 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005442
5443 NotifyKeyArgs args;
5444
5445 // Press virtual key.
5446 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5447 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5448 processDown(mapper, x, y);
5449 processSync(mapper);
5450
5451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5452 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5453 ASSERT_EQ(DEVICE_ID, args.deviceId);
5454 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5455 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5456 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5457 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5458 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5459 ASSERT_EQ(KEY_HOME, args.scanCode);
5460 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5461 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5462
5463 // Release virtual key.
5464 processUp(mapper);
5465 processSync(mapper);
5466
5467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5468 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5469 ASSERT_EQ(DEVICE_ID, args.deviceId);
5470 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5471 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5472 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5473 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5474 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5475 ASSERT_EQ(KEY_HOME, args.scanCode);
5476 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5477 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5478
5479 // Should not have sent any motions.
5480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5481}
5482
5483TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005484 addConfigurationProperty("touch.deviceType", "touchScreen");
5485 prepareDisplay(DISPLAY_ORIENTATION_0);
5486 prepareButtons();
5487 prepareAxes(POSITION);
5488 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005489 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005490
arthurhungdcef2dc2020-08-11 14:47:50 +08005491 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005492
5493 NotifyKeyArgs keyArgs;
5494
5495 // Press virtual key.
5496 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5497 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5498 processDown(mapper, x, y);
5499 processSync(mapper);
5500
5501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5502 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5503 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5504 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5505 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5506 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5507 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5508 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5509 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5510 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5511 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5512
5513 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5514 // into the display area.
5515 y -= 100;
5516 processMove(mapper, x, y);
5517 processSync(mapper);
5518
5519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5520 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5521 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5522 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5523 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5524 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5525 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5526 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5527 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5528 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5529 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5530 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5531
5532 NotifyMotionArgs motionArgs;
5533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5534 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5535 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5536 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5537 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5538 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5539 ASSERT_EQ(0, motionArgs.flags);
5540 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5541 ASSERT_EQ(0, motionArgs.buttonState);
5542 ASSERT_EQ(0, motionArgs.edgeFlags);
5543 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5544 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5545 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5546 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5547 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5548 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5549 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5550 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5551
5552 // Keep moving out of bounds. Should generate a pointer move.
5553 y -= 50;
5554 processMove(mapper, x, y);
5555 processSync(mapper);
5556
5557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5558 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5559 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5560 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5561 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5562 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5563 ASSERT_EQ(0, motionArgs.flags);
5564 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5565 ASSERT_EQ(0, motionArgs.buttonState);
5566 ASSERT_EQ(0, motionArgs.edgeFlags);
5567 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5568 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5569 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5571 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5572 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5573 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5574 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5575
5576 // Release out of bounds. Should generate a pointer up.
5577 processUp(mapper);
5578 processSync(mapper);
5579
5580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5581 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5582 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5583 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5584 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5585 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5586 ASSERT_EQ(0, motionArgs.flags);
5587 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5588 ASSERT_EQ(0, motionArgs.buttonState);
5589 ASSERT_EQ(0, motionArgs.edgeFlags);
5590 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5591 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5592 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5593 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5594 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5595 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5596 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5597 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5598
5599 // Should not have sent any more keys or motions.
5600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5602}
5603
5604TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005605 addConfigurationProperty("touch.deviceType", "touchScreen");
5606 prepareDisplay(DISPLAY_ORIENTATION_0);
5607 prepareButtons();
5608 prepareAxes(POSITION);
5609 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005610 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005611
arthurhungdcef2dc2020-08-11 14:47:50 +08005612 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005613
5614 NotifyMotionArgs motionArgs;
5615
5616 // Initially go down out of bounds.
5617 int32_t x = -10;
5618 int32_t y = -10;
5619 processDown(mapper, x, y);
5620 processSync(mapper);
5621
5622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5623
5624 // Move into the display area. Should generate a pointer down.
5625 x = 50;
5626 y = 75;
5627 processMove(mapper, x, y);
5628 processSync(mapper);
5629
5630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5631 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5632 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5633 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5634 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5635 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5636 ASSERT_EQ(0, motionArgs.flags);
5637 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5638 ASSERT_EQ(0, motionArgs.buttonState);
5639 ASSERT_EQ(0, motionArgs.edgeFlags);
5640 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5641 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5642 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5643 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5644 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5645 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5646 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5647 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5648
5649 // Release. Should generate a pointer up.
5650 processUp(mapper);
5651 processSync(mapper);
5652
5653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5654 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5655 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5656 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5657 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5658 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5659 ASSERT_EQ(0, motionArgs.flags);
5660 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5661 ASSERT_EQ(0, motionArgs.buttonState);
5662 ASSERT_EQ(0, motionArgs.edgeFlags);
5663 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5664 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5665 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5666 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5667 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5668 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5669 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5670 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5671
5672 // Should not have sent any more keys or motions.
5673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5675}
5676
Santos Cordonfa5cf462017-04-05 10:37:00 -07005677TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005678 addConfigurationProperty("touch.deviceType", "touchScreen");
5679 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5680
5681 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5682 prepareButtons();
5683 prepareAxes(POSITION);
5684 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005685 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005686
arthurhungdcef2dc2020-08-11 14:47:50 +08005687 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005688
5689 NotifyMotionArgs motionArgs;
5690
5691 // Down.
5692 int32_t x = 100;
5693 int32_t y = 125;
5694 processDown(mapper, x, y);
5695 processSync(mapper);
5696
5697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5698 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5699 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5700 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5701 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5702 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5703 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5704 ASSERT_EQ(0, motionArgs.flags);
5705 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5706 ASSERT_EQ(0, motionArgs.buttonState);
5707 ASSERT_EQ(0, motionArgs.edgeFlags);
5708 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5709 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5710 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5711 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5712 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5713 1, 0, 0, 0, 0, 0, 0, 0));
5714 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5715 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5716 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5717
5718 // Move.
5719 x += 50;
5720 y += 75;
5721 processMove(mapper, x, y);
5722 processSync(mapper);
5723
5724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5725 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5726 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5727 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5728 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5729 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5730 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5731 ASSERT_EQ(0, motionArgs.flags);
5732 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5733 ASSERT_EQ(0, motionArgs.buttonState);
5734 ASSERT_EQ(0, motionArgs.edgeFlags);
5735 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5736 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5737 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5738 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5739 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5740 1, 0, 0, 0, 0, 0, 0, 0));
5741 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5742 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5743 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5744
5745 // Up.
5746 processUp(mapper);
5747 processSync(mapper);
5748
5749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5750 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5751 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5752 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5753 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5754 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5755 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5756 ASSERT_EQ(0, motionArgs.flags);
5757 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5758 ASSERT_EQ(0, motionArgs.buttonState);
5759 ASSERT_EQ(0, motionArgs.edgeFlags);
5760 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5761 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5762 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5763 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5764 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5765 1, 0, 0, 0, 0, 0, 0, 0));
5766 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5767 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5768 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5769
5770 // Should not have sent any more keys or motions.
5771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5773}
5774
Michael Wrightd02c5b62014-02-10 15:10:22 -08005775TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005776 addConfigurationProperty("touch.deviceType", "touchScreen");
5777 prepareDisplay(DISPLAY_ORIENTATION_0);
5778 prepareButtons();
5779 prepareAxes(POSITION);
5780 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005781 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005782
arthurhungdcef2dc2020-08-11 14:47:50 +08005783 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005784
5785 NotifyMotionArgs motionArgs;
5786
5787 // Down.
5788 int32_t x = 100;
5789 int32_t y = 125;
5790 processDown(mapper, x, y);
5791 processSync(mapper);
5792
5793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5794 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5795 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5796 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5797 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5798 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5799 ASSERT_EQ(0, motionArgs.flags);
5800 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5801 ASSERT_EQ(0, motionArgs.buttonState);
5802 ASSERT_EQ(0, motionArgs.edgeFlags);
5803 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5804 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5805 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5806 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5807 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5808 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5809 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5810 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5811
5812 // Move.
5813 x += 50;
5814 y += 75;
5815 processMove(mapper, x, y);
5816 processSync(mapper);
5817
5818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5819 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5820 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5821 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5822 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5823 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5824 ASSERT_EQ(0, motionArgs.flags);
5825 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5826 ASSERT_EQ(0, motionArgs.buttonState);
5827 ASSERT_EQ(0, motionArgs.edgeFlags);
5828 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5829 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5830 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5831 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5832 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5833 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5834 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5835 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5836
5837 // Up.
5838 processUp(mapper);
5839 processSync(mapper);
5840
5841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5842 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5843 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5844 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5845 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5846 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5847 ASSERT_EQ(0, motionArgs.flags);
5848 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5849 ASSERT_EQ(0, motionArgs.buttonState);
5850 ASSERT_EQ(0, motionArgs.edgeFlags);
5851 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5852 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5853 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5854 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5855 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5856 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5857 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5858 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5859
5860 // Should not have sent any more keys or motions.
5861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5863}
5864
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005865TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005866 addConfigurationProperty("touch.deviceType", "touchScreen");
5867 prepareButtons();
5868 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005869 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5870 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005871 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005872
5873 NotifyMotionArgs args;
5874
5875 // Rotation 90.
5876 prepareDisplay(DISPLAY_ORIENTATION_90);
5877 processDown(mapper, toRawX(50), toRawY(75));
5878 processSync(mapper);
5879
5880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5881 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5882 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5883
5884 processUp(mapper);
5885 processSync(mapper);
5886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5887}
5888
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005889TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005890 addConfigurationProperty("touch.deviceType", "touchScreen");
5891 prepareButtons();
5892 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005893 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5894 // orientation-aware are affected by display rotation.
5895 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005896 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005897
5898 NotifyMotionArgs args;
5899
5900 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005901 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005902 prepareDisplay(DISPLAY_ORIENTATION_0);
5903 processDown(mapper, toRawX(50), toRawY(75));
5904 processSync(mapper);
5905
5906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5907 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5908 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5909
5910 processUp(mapper);
5911 processSync(mapper);
5912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5913
5914 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005915 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005916 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005917 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005918 processSync(mapper);
5919
5920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5921 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5922 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5923
5924 processUp(mapper);
5925 processSync(mapper);
5926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5927
5928 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005929 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005930 prepareDisplay(DISPLAY_ORIENTATION_180);
5931 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5932 processSync(mapper);
5933
5934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5935 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5936 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5937
5938 processUp(mapper);
5939 processSync(mapper);
5940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5941
5942 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005943 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005944 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005945 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005946 processSync(mapper);
5947
5948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5949 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5950 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5951
5952 processUp(mapper);
5953 processSync(mapper);
5954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5955}
5956
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005957TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5958 addConfigurationProperty("touch.deviceType", "touchScreen");
5959 prepareButtons();
5960 prepareAxes(POSITION);
5961 addConfigurationProperty("touch.orientationAware", "1");
5962 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5963 clearViewports();
5964 prepareDisplay(DISPLAY_ORIENTATION_0);
5965 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5966 NotifyMotionArgs args;
5967
5968 // Orientation 0.
5969 processDown(mapper, toRawX(50), toRawY(75));
5970 processSync(mapper);
5971
5972 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5973 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5974 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5975
5976 processUp(mapper);
5977 processSync(mapper);
5978 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5979}
5980
5981TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5982 addConfigurationProperty("touch.deviceType", "touchScreen");
5983 prepareButtons();
5984 prepareAxes(POSITION);
5985 addConfigurationProperty("touch.orientationAware", "1");
5986 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5987 clearViewports();
5988 prepareDisplay(DISPLAY_ORIENTATION_0);
5989 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5990 NotifyMotionArgs args;
5991
5992 // Orientation 90.
5993 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5994 processSync(mapper);
5995
5996 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5997 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5998 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5999
6000 processUp(mapper);
6001 processSync(mapper);
6002 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6003}
6004
6005TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6006 addConfigurationProperty("touch.deviceType", "touchScreen");
6007 prepareButtons();
6008 prepareAxes(POSITION);
6009 addConfigurationProperty("touch.orientationAware", "1");
6010 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6011 clearViewports();
6012 prepareDisplay(DISPLAY_ORIENTATION_0);
6013 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6014 NotifyMotionArgs args;
6015
6016 // Orientation 180.
6017 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6018 processSync(mapper);
6019
6020 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6021 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6022 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6023
6024 processUp(mapper);
6025 processSync(mapper);
6026 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6027}
6028
6029TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6030 addConfigurationProperty("touch.deviceType", "touchScreen");
6031 prepareButtons();
6032 prepareAxes(POSITION);
6033 addConfigurationProperty("touch.orientationAware", "1");
6034 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6035 clearViewports();
6036 prepareDisplay(DISPLAY_ORIENTATION_0);
6037 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6038 NotifyMotionArgs args;
6039
6040 // Orientation 270.
6041 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6042 processSync(mapper);
6043
6044 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6045 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6046 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6047
6048 processUp(mapper);
6049 processSync(mapper);
6050 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6051}
6052
6053TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6054 addConfigurationProperty("touch.deviceType", "touchScreen");
6055 prepareButtons();
6056 prepareAxes(POSITION);
6057 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6058 // orientation-aware are affected by display rotation.
6059 addConfigurationProperty("touch.orientationAware", "0");
6060 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6061 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6062
6063 NotifyMotionArgs args;
6064
6065 // Orientation 90, Rotation 0.
6066 clearViewports();
6067 prepareDisplay(DISPLAY_ORIENTATION_0);
6068 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6069 processSync(mapper);
6070
6071 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6072 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6073 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6074
6075 processUp(mapper);
6076 processSync(mapper);
6077 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6078
6079 // Orientation 90, Rotation 90.
6080 clearViewports();
6081 prepareDisplay(DISPLAY_ORIENTATION_90);
6082 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6083 processSync(mapper);
6084
6085 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6086 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6087 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6088
6089 processUp(mapper);
6090 processSync(mapper);
6091 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6092
6093 // Orientation 90, Rotation 180.
6094 clearViewports();
6095 prepareDisplay(DISPLAY_ORIENTATION_180);
6096 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
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 // Orientation 90, Rotation 270.
6108 clearViewports();
6109 prepareDisplay(DISPLAY_ORIENTATION_270);
6110 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6111 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6112 processSync(mapper);
6113
6114 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6115 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6116 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6117
6118 processUp(mapper);
6119 processSync(mapper);
6120 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6121}
6122
Michael Wrightd02c5b62014-02-10 15:10:22 -08006123TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006124 addConfigurationProperty("touch.deviceType", "touchScreen");
6125 prepareDisplay(DISPLAY_ORIENTATION_0);
6126 prepareButtons();
6127 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006128 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006129
6130 // These calculations are based on the input device calibration documentation.
6131 int32_t rawX = 100;
6132 int32_t rawY = 200;
6133 int32_t rawPressure = 10;
6134 int32_t rawToolMajor = 12;
6135 int32_t rawDistance = 2;
6136 int32_t rawTiltX = 30;
6137 int32_t rawTiltY = 110;
6138
6139 float x = toDisplayX(rawX);
6140 float y = toDisplayY(rawY);
6141 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6142 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6143 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6144 float distance = float(rawDistance);
6145
6146 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6147 float tiltScale = M_PI / 180;
6148 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6149 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6150 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6151 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6152
6153 processDown(mapper, rawX, rawY);
6154 processPressure(mapper, rawPressure);
6155 processToolMajor(mapper, rawToolMajor);
6156 processDistance(mapper, rawDistance);
6157 processTilt(mapper, rawTiltX, rawTiltY);
6158 processSync(mapper);
6159
6160 NotifyMotionArgs args;
6161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6162 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6163 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6164 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6165}
6166
Jason Gerecke489fda82012-09-07 17:19:40 -07006167TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006168 addConfigurationProperty("touch.deviceType", "touchScreen");
6169 prepareDisplay(DISPLAY_ORIENTATION_0);
6170 prepareLocationCalibration();
6171 prepareButtons();
6172 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006173 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006174
6175 int32_t rawX = 100;
6176 int32_t rawY = 200;
6177
6178 float x = toDisplayX(toCookedX(rawX, rawY));
6179 float y = toDisplayY(toCookedY(rawX, rawY));
6180
6181 processDown(mapper, rawX, rawY);
6182 processSync(mapper);
6183
6184 NotifyMotionArgs args;
6185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6186 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6187 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6188}
6189
Michael Wrightd02c5b62014-02-10 15:10:22 -08006190TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006191 addConfigurationProperty("touch.deviceType", "touchScreen");
6192 prepareDisplay(DISPLAY_ORIENTATION_0);
6193 prepareButtons();
6194 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006195 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006196
6197 NotifyMotionArgs motionArgs;
6198 NotifyKeyArgs keyArgs;
6199
6200 processDown(mapper, 100, 200);
6201 processSync(mapper);
6202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6203 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6204 ASSERT_EQ(0, motionArgs.buttonState);
6205
6206 // press BTN_LEFT, release BTN_LEFT
6207 processKey(mapper, BTN_LEFT, 1);
6208 processSync(mapper);
6209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6210 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6211 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6212
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6214 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6215 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6216
Michael Wrightd02c5b62014-02-10 15:10:22 -08006217 processKey(mapper, BTN_LEFT, 0);
6218 processSync(mapper);
6219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006220 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006221 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006222
6223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006224 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006225 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006226
6227 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6228 processKey(mapper, BTN_RIGHT, 1);
6229 processKey(mapper, BTN_MIDDLE, 1);
6230 processSync(mapper);
6231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6232 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6233 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6234 motionArgs.buttonState);
6235
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6237 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6238 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6239
6240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6241 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6242 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6243 motionArgs.buttonState);
6244
Michael Wrightd02c5b62014-02-10 15:10:22 -08006245 processKey(mapper, BTN_RIGHT, 0);
6246 processSync(mapper);
6247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006248 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006249 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006250
6251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006252 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006253 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006254
6255 processKey(mapper, BTN_MIDDLE, 0);
6256 processSync(mapper);
6257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006258 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006259 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006260
6261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006262 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006263 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006264
6265 // press BTN_BACK, release BTN_BACK
6266 processKey(mapper, BTN_BACK, 1);
6267 processSync(mapper);
6268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6269 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6270 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006271
Michael Wrightd02c5b62014-02-10 15:10:22 -08006272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006273 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006274 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6275
6276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6277 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6278 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006279
6280 processKey(mapper, BTN_BACK, 0);
6281 processSync(mapper);
6282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006283 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006284 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006285
6286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006287 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006288 ASSERT_EQ(0, motionArgs.buttonState);
6289
Michael Wrightd02c5b62014-02-10 15:10:22 -08006290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6291 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6292 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6293
6294 // press BTN_SIDE, release BTN_SIDE
6295 processKey(mapper, BTN_SIDE, 1);
6296 processSync(mapper);
6297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6298 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6299 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006300
Michael Wrightd02c5b62014-02-10 15:10:22 -08006301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006302 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006303 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6304
6305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6306 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6307 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006308
6309 processKey(mapper, BTN_SIDE, 0);
6310 processSync(mapper);
6311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006312 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006313 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006314
6315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006316 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006317 ASSERT_EQ(0, motionArgs.buttonState);
6318
Michael Wrightd02c5b62014-02-10 15:10:22 -08006319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6320 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6321 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6322
6323 // press BTN_FORWARD, release BTN_FORWARD
6324 processKey(mapper, BTN_FORWARD, 1);
6325 processSync(mapper);
6326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6327 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6328 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006329
Michael Wrightd02c5b62014-02-10 15:10:22 -08006330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006331 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006332 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6333
6334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6335 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6336 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006337
6338 processKey(mapper, BTN_FORWARD, 0);
6339 processSync(mapper);
6340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006341 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006342 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006343
6344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006345 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006346 ASSERT_EQ(0, motionArgs.buttonState);
6347
Michael Wrightd02c5b62014-02-10 15:10:22 -08006348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6349 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6350 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6351
6352 // press BTN_EXTRA, release BTN_EXTRA
6353 processKey(mapper, BTN_EXTRA, 1);
6354 processSync(mapper);
6355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6356 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6357 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006358
Michael Wrightd02c5b62014-02-10 15:10:22 -08006359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006360 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006361 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6362
6363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6364 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6365 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006366
6367 processKey(mapper, BTN_EXTRA, 0);
6368 processSync(mapper);
6369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006370 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006371 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006372
6373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006374 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006375 ASSERT_EQ(0, motionArgs.buttonState);
6376
Michael Wrightd02c5b62014-02-10 15:10:22 -08006377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6378 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6379 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6380
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6382
Michael Wrightd02c5b62014-02-10 15:10:22 -08006383 // press BTN_STYLUS, release BTN_STYLUS
6384 processKey(mapper, BTN_STYLUS, 1);
6385 processSync(mapper);
6386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6387 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006388 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6389
6390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6391 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6392 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006393
6394 processKey(mapper, BTN_STYLUS, 0);
6395 processSync(mapper);
6396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006397 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006398 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006399
6400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006401 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006402 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006403
6404 // press BTN_STYLUS2, release BTN_STYLUS2
6405 processKey(mapper, BTN_STYLUS2, 1);
6406 processSync(mapper);
6407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6408 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006409 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6410
6411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6412 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6413 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006414
6415 processKey(mapper, BTN_STYLUS2, 0);
6416 processSync(mapper);
6417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006418 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006419 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006420
6421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006422 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006423 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006424
6425 // release touch
6426 processUp(mapper);
6427 processSync(mapper);
6428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6429 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6430 ASSERT_EQ(0, motionArgs.buttonState);
6431}
6432
6433TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006434 addConfigurationProperty("touch.deviceType", "touchScreen");
6435 prepareDisplay(DISPLAY_ORIENTATION_0);
6436 prepareButtons();
6437 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006438 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006439
6440 NotifyMotionArgs motionArgs;
6441
6442 // default tool type is finger
6443 processDown(mapper, 100, 200);
6444 processSync(mapper);
6445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6446 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6447 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6448
6449 // eraser
6450 processKey(mapper, BTN_TOOL_RUBBER, 1);
6451 processSync(mapper);
6452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6453 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6454 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6455
6456 // stylus
6457 processKey(mapper, BTN_TOOL_RUBBER, 0);
6458 processKey(mapper, BTN_TOOL_PEN, 1);
6459 processSync(mapper);
6460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6461 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6462 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6463
6464 // brush
6465 processKey(mapper, BTN_TOOL_PEN, 0);
6466 processKey(mapper, BTN_TOOL_BRUSH, 1);
6467 processSync(mapper);
6468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6469 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6470 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6471
6472 // pencil
6473 processKey(mapper, BTN_TOOL_BRUSH, 0);
6474 processKey(mapper, BTN_TOOL_PENCIL, 1);
6475 processSync(mapper);
6476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6477 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6478 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6479
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006480 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006481 processKey(mapper, BTN_TOOL_PENCIL, 0);
6482 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6483 processSync(mapper);
6484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6485 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6486 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6487
6488 // mouse
6489 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6490 processKey(mapper, BTN_TOOL_MOUSE, 1);
6491 processSync(mapper);
6492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6493 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6494 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6495
6496 // lens
6497 processKey(mapper, BTN_TOOL_MOUSE, 0);
6498 processKey(mapper, BTN_TOOL_LENS, 1);
6499 processSync(mapper);
6500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6501 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6502 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6503
6504 // double-tap
6505 processKey(mapper, BTN_TOOL_LENS, 0);
6506 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6507 processSync(mapper);
6508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6509 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6510 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6511
6512 // triple-tap
6513 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6514 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6515 processSync(mapper);
6516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6517 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6518 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6519
6520 // quad-tap
6521 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6522 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6523 processSync(mapper);
6524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6525 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6526 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6527
6528 // finger
6529 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6530 processKey(mapper, BTN_TOOL_FINGER, 1);
6531 processSync(mapper);
6532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6533 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6534 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6535
6536 // stylus trumps finger
6537 processKey(mapper, BTN_TOOL_PEN, 1);
6538 processSync(mapper);
6539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6540 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6541 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6542
6543 // eraser trumps stylus
6544 processKey(mapper, BTN_TOOL_RUBBER, 1);
6545 processSync(mapper);
6546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6547 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6548 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6549
6550 // mouse trumps eraser
6551 processKey(mapper, BTN_TOOL_MOUSE, 1);
6552 processSync(mapper);
6553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6554 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6555 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6556
6557 // back to default tool type
6558 processKey(mapper, BTN_TOOL_MOUSE, 0);
6559 processKey(mapper, BTN_TOOL_RUBBER, 0);
6560 processKey(mapper, BTN_TOOL_PEN, 0);
6561 processKey(mapper, BTN_TOOL_FINGER, 0);
6562 processSync(mapper);
6563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6564 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6565 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6566}
6567
6568TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006569 addConfigurationProperty("touch.deviceType", "touchScreen");
6570 prepareDisplay(DISPLAY_ORIENTATION_0);
6571 prepareButtons();
6572 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006573 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006574 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006575
6576 NotifyMotionArgs motionArgs;
6577
6578 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6579 processKey(mapper, BTN_TOOL_FINGER, 1);
6580 processMove(mapper, 100, 200);
6581 processSync(mapper);
6582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6583 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6585 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6586
6587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6588 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6589 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6590 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6591
6592 // move a little
6593 processMove(mapper, 150, 250);
6594 processSync(mapper);
6595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6596 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6597 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6598 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6599
6600 // down when BTN_TOUCH is pressed, pressure defaults to 1
6601 processKey(mapper, BTN_TOUCH, 1);
6602 processSync(mapper);
6603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6604 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6606 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6607
6608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6609 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6611 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6612
6613 // up when BTN_TOUCH is released, hover restored
6614 processKey(mapper, BTN_TOUCH, 0);
6615 processSync(mapper);
6616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6617 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6619 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6620
6621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6622 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6623 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6624 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6625
6626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6627 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6628 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6629 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6630
6631 // exit hover when pointer goes away
6632 processKey(mapper, BTN_TOOL_FINGER, 0);
6633 processSync(mapper);
6634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6635 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6636 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6637 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6638}
6639
6640TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006641 addConfigurationProperty("touch.deviceType", "touchScreen");
6642 prepareDisplay(DISPLAY_ORIENTATION_0);
6643 prepareButtons();
6644 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006645 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006646
6647 NotifyMotionArgs motionArgs;
6648
6649 // initially hovering because pressure is 0
6650 processDown(mapper, 100, 200);
6651 processPressure(mapper, 0);
6652 processSync(mapper);
6653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6654 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6655 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6656 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6657
6658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6659 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6661 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6662
6663 // move a little
6664 processMove(mapper, 150, 250);
6665 processSync(mapper);
6666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6667 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6669 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6670
6671 // down when pressure is non-zero
6672 processPressure(mapper, RAW_PRESSURE_MAX);
6673 processSync(mapper);
6674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6675 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6677 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6678
6679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6680 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6681 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6682 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6683
6684 // up when pressure becomes 0, hover restored
6685 processPressure(mapper, 0);
6686 processSync(mapper);
6687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6688 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6689 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6690 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6691
6692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6693 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6694 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6695 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6696
6697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6698 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6699 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6700 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6701
6702 // exit hover when pointer goes away
6703 processUp(mapper);
6704 processSync(mapper);
6705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6706 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6707 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6708 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6709}
6710
lilinnan687e58f2022-07-19 16:00:50 +08006711TEST_F(SingleTouchInputMapperTest,
6712 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6713 addConfigurationProperty("touch.deviceType", "touchScreen");
6714 prepareDisplay(DISPLAY_ORIENTATION_0);
6715 prepareButtons();
6716 prepareAxes(POSITION);
6717 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6718 NotifyMotionArgs motionArgs;
6719
6720 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006721 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006722 processSync(mapper);
6723
6724 // We should receive a down event
6725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6726 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6727
6728 // Change display id
6729 clearViewports();
6730 prepareSecondaryDisplay(ViewportType::INTERNAL);
6731
6732 // We should receive a cancel event
6733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6734 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6735 // Then receive reset called
6736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6737}
6738
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006739TEST_F(SingleTouchInputMapperTest,
6740 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6741 addConfigurationProperty("touch.deviceType", "touchScreen");
6742 prepareDisplay(DISPLAY_ORIENTATION_0);
6743 prepareButtons();
6744 prepareAxes(POSITION);
6745 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6747 NotifyMotionArgs motionArgs;
6748
6749 // Start a new gesture.
6750 processDown(mapper, 100, 200);
6751 processSync(mapper);
6752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6753 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6754
6755 // Make the viewport inactive. This will put the device in disabled mode.
6756 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6757 viewport->isActive = false;
6758 mFakePolicy->updateViewport(*viewport);
6759 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6760
6761 // We should receive a cancel event for the ongoing gesture.
6762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6763 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6764 // Then we should be notified that the device was reset.
6765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6766
6767 // No events are generated while the viewport is inactive.
6768 processMove(mapper, 101, 201);
6769 processSync(mapper);
6770 processDown(mapper, 102, 202);
6771 processSync(mapper);
6772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6773
6774 // Make the viewport active again. The device should resume processing events.
6775 viewport->isActive = true;
6776 mFakePolicy->updateViewport(*viewport);
6777 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6778
6779 // The device is reset because it changes back to direct mode, without generating any events.
6780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6782
6783 // Start a new gesture.
6784 processDown(mapper, 100, 200);
6785 processSync(mapper);
6786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6787 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6788
6789 // No more events.
6790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6792}
6793
Prabir Pradhan5632d622021-09-06 07:57:20 -07006794// --- TouchDisplayProjectionTest ---
6795
6796class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6797public:
6798 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6799 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6800 // rotated equivalent of the given un-rotated physical display bounds.
6801 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6802 uint32_t inverseRotationFlags;
6803 auto width = DISPLAY_WIDTH;
6804 auto height = DISPLAY_HEIGHT;
6805 switch (orientation) {
6806 case DISPLAY_ORIENTATION_90:
6807 inverseRotationFlags = ui::Transform::ROT_270;
6808 std::swap(width, height);
6809 break;
6810 case DISPLAY_ORIENTATION_180:
6811 inverseRotationFlags = ui::Transform::ROT_180;
6812 break;
6813 case DISPLAY_ORIENTATION_270:
6814 inverseRotationFlags = ui::Transform::ROT_90;
6815 std::swap(width, height);
6816 break;
6817 case DISPLAY_ORIENTATION_0:
6818 inverseRotationFlags = ui::Transform::ROT_0;
6819 break;
6820 default:
6821 FAIL() << "Invalid orientation: " << orientation;
6822 }
6823
6824 const ui::Transform rotation(inverseRotationFlags, width, height);
6825 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6826
6827 std::optional<DisplayViewport> internalViewport =
6828 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6829 DisplayViewport& v = *internalViewport;
6830 v.displayId = DISPLAY_ID;
6831 v.orientation = orientation;
6832
6833 v.logicalLeft = 0;
6834 v.logicalTop = 0;
6835 v.logicalRight = 100;
6836 v.logicalBottom = 100;
6837
6838 v.physicalLeft = rotatedPhysicalDisplay.left;
6839 v.physicalTop = rotatedPhysicalDisplay.top;
6840 v.physicalRight = rotatedPhysicalDisplay.right;
6841 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6842
6843 v.deviceWidth = width;
6844 v.deviceHeight = height;
6845
6846 v.isActive = true;
6847 v.uniqueId = UNIQUE_ID;
6848 v.type = ViewportType::INTERNAL;
6849 mFakePolicy->updateViewport(v);
6850 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6851 }
6852
6853 void assertReceivedMove(const Point& point) {
6854 NotifyMotionArgs motionArgs;
6855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6856 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6857 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6858 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6859 1, 0, 0, 0, 0, 0, 0, 0));
6860 }
6861};
6862
6863TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6864 addConfigurationProperty("touch.deviceType", "touchScreen");
6865 prepareDisplay(DISPLAY_ORIENTATION_0);
6866
6867 prepareButtons();
6868 prepareAxes(POSITION);
6869 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6870
6871 NotifyMotionArgs motionArgs;
6872
6873 // Configure the DisplayViewport such that the logical display maps to a subsection of
6874 // the display panel called the physical display. Here, the physical display is bounded by the
6875 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6876 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6877 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6878 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6879
6880 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6881 DISPLAY_ORIENTATION_270}) {
6882 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6883
6884 // Touches outside the physical display should be ignored, and should not generate any
6885 // events. Ensure touches at the following points that lie outside of the physical display
6886 // area do not generate any events.
6887 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6888 processDown(mapper, toRawX(point.x), toRawY(point.y));
6889 processSync(mapper);
6890 processUp(mapper);
6891 processSync(mapper);
6892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6893 << "Unexpected event generated for touch outside physical display at point: "
6894 << point.x << ", " << point.y;
6895 }
6896 }
6897}
6898
6899TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6900 addConfigurationProperty("touch.deviceType", "touchScreen");
6901 prepareDisplay(DISPLAY_ORIENTATION_0);
6902
6903 prepareButtons();
6904 prepareAxes(POSITION);
6905 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6906
6907 NotifyMotionArgs motionArgs;
6908
6909 // Configure the DisplayViewport such that the logical display maps to a subsection of
6910 // the display panel called the physical display. Here, the physical display is bounded by the
6911 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6912 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6913
6914 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6915 DISPLAY_ORIENTATION_270}) {
6916 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6917
6918 // Touches that start outside the physical display should be ignored until it enters the
6919 // physical display bounds, at which point it should generate a down event. Start a touch at
6920 // the point (5, 100), which is outside the physical display bounds.
6921 static const Point kOutsidePoint{5, 100};
6922 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6923 processSync(mapper);
6924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6925
6926 // Move the touch into the physical display area. This should generate a pointer down.
6927 processMove(mapper, toRawX(11), toRawY(21));
6928 processSync(mapper);
6929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6930 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6931 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6932 ASSERT_NO_FATAL_FAILURE(
6933 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6934
6935 // Move the touch inside the physical display area. This should generate a pointer move.
6936 processMove(mapper, toRawX(69), toRawY(159));
6937 processSync(mapper);
6938 assertReceivedMove({69, 159});
6939
6940 // Move outside the physical display area. Since the pointer is already down, this should
6941 // now continue generating events.
6942 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6943 processSync(mapper);
6944 assertReceivedMove(kOutsidePoint);
6945
6946 // Release. This should generate a pointer up.
6947 processUp(mapper);
6948 processSync(mapper);
6949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6950 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6951 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6952 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6953
6954 // Ensure no more events were generated.
6955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6957 }
6958}
6959
Michael Wrightd02c5b62014-02-10 15:10:22 -08006960// --- MultiTouchInputMapperTest ---
6961
6962class MultiTouchInputMapperTest : public TouchInputMapperTest {
6963protected:
6964 void prepareAxes(int axes);
6965
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006966 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6967 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6968 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6969 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6970 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6971 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6972 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6973 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6974 void processId(MultiTouchInputMapper& mapper, int32_t id);
6975 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6976 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6977 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6978 void processMTSync(MultiTouchInputMapper& mapper);
6979 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006980};
6981
6982void MultiTouchInputMapperTest::prepareAxes(int axes) {
6983 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006984 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6985 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006986 }
6987 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006988 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6989 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006990 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006991 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6992 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006993 }
6994 }
6995 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006996 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6997 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006998 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08006999 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007000 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007001 }
7002 }
7003 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007004 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7005 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007006 }
7007 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007008 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7009 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007010 }
7011 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007012 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7013 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007014 }
7015 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007016 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7017 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007018 }
7019 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007020 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7021 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007022 }
7023 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007024 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007025 }
7026}
7027
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007028void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7029 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007030 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7031 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007032}
7033
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007034void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7035 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007036 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007037}
7038
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007039void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7040 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007041 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007042}
7043
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007044void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007045 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007046}
7047
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007048void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007049 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007050}
7051
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007052void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7053 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007054 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007055}
7056
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007057void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007058 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007059}
7060
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007061void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007062 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007063}
7064
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007065void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007066 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007067}
7068
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007069void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007070 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007071}
7072
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007073void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007074 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007075}
7076
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007077void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7078 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007079 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007080}
7081
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007082void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007083 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007084}
7085
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007086void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007087 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007088}
7089
Michael Wrightd02c5b62014-02-10 15:10:22 -08007090TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007091 addConfigurationProperty("touch.deviceType", "touchScreen");
7092 prepareDisplay(DISPLAY_ORIENTATION_0);
7093 prepareAxes(POSITION);
7094 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007095 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007096
arthurhungdcef2dc2020-08-11 14:47:50 +08007097 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007098
7099 NotifyMotionArgs motionArgs;
7100
7101 // Two fingers down at once.
7102 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7103 processPosition(mapper, x1, y1);
7104 processMTSync(mapper);
7105 processPosition(mapper, x2, y2);
7106 processMTSync(mapper);
7107 processSync(mapper);
7108
7109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7110 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7111 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7112 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7113 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7114 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7115 ASSERT_EQ(0, motionArgs.flags);
7116 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7117 ASSERT_EQ(0, motionArgs.buttonState);
7118 ASSERT_EQ(0, motionArgs.edgeFlags);
7119 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7120 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7121 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7122 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7123 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7124 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7125 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7126 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7127
7128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7129 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7130 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7131 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7132 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007133 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007134 ASSERT_EQ(0, motionArgs.flags);
7135 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7136 ASSERT_EQ(0, motionArgs.buttonState);
7137 ASSERT_EQ(0, motionArgs.edgeFlags);
7138 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7139 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7140 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7141 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7142 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7143 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7144 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7145 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7146 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7147 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7148 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7149 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7150
7151 // Move.
7152 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7153 processPosition(mapper, x1, y1);
7154 processMTSync(mapper);
7155 processPosition(mapper, x2, y2);
7156 processMTSync(mapper);
7157 processSync(mapper);
7158
7159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7160 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7161 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7162 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7163 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7164 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7165 ASSERT_EQ(0, motionArgs.flags);
7166 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7167 ASSERT_EQ(0, motionArgs.buttonState);
7168 ASSERT_EQ(0, motionArgs.edgeFlags);
7169 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7170 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7171 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7172 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7173 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7174 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7175 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7176 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7177 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7178 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7179 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7180 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7181
7182 // First finger up.
7183 x2 += 15; y2 -= 20;
7184 processPosition(mapper, x2, y2);
7185 processMTSync(mapper);
7186 processSync(mapper);
7187
7188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7189 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7190 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7191 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7192 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007193 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007194 ASSERT_EQ(0, motionArgs.flags);
7195 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7196 ASSERT_EQ(0, motionArgs.buttonState);
7197 ASSERT_EQ(0, motionArgs.edgeFlags);
7198 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7199 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7200 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7201 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7202 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7203 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7204 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7205 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7206 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7207 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7208 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7209 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7210
7211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7212 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7213 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7214 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7215 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7216 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7217 ASSERT_EQ(0, motionArgs.flags);
7218 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7219 ASSERT_EQ(0, motionArgs.buttonState);
7220 ASSERT_EQ(0, motionArgs.edgeFlags);
7221 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7222 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7223 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7224 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7225 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7226 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7227 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7228 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7229
7230 // Move.
7231 x2 += 20; y2 -= 25;
7232 processPosition(mapper, x2, y2);
7233 processMTSync(mapper);
7234 processSync(mapper);
7235
7236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7237 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7238 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7239 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7240 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7241 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7242 ASSERT_EQ(0, motionArgs.flags);
7243 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7244 ASSERT_EQ(0, motionArgs.buttonState);
7245 ASSERT_EQ(0, motionArgs.edgeFlags);
7246 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7247 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7248 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7249 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7250 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7251 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7252 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7253 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7254
7255 // New finger down.
7256 int32_t x3 = 700, y3 = 300;
7257 processPosition(mapper, x2, y2);
7258 processMTSync(mapper);
7259 processPosition(mapper, x3, y3);
7260 processMTSync(mapper);
7261 processSync(mapper);
7262
7263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7264 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7265 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7266 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7267 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007268 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007269 ASSERT_EQ(0, motionArgs.flags);
7270 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7271 ASSERT_EQ(0, motionArgs.buttonState);
7272 ASSERT_EQ(0, motionArgs.edgeFlags);
7273 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7274 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7275 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7276 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7277 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7278 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7279 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7280 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7281 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7282 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7283 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7284 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7285
7286 // Second finger up.
7287 x3 += 30; y3 -= 20;
7288 processPosition(mapper, x3, y3);
7289 processMTSync(mapper);
7290 processSync(mapper);
7291
7292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7293 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7294 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7295 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7296 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007297 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007298 ASSERT_EQ(0, motionArgs.flags);
7299 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7300 ASSERT_EQ(0, motionArgs.buttonState);
7301 ASSERT_EQ(0, motionArgs.edgeFlags);
7302 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7303 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7304 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7305 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7306 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7308 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7310 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7311 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7312 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7313 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7314
7315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7316 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7317 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7318 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7319 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7320 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7321 ASSERT_EQ(0, motionArgs.flags);
7322 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7323 ASSERT_EQ(0, motionArgs.buttonState);
7324 ASSERT_EQ(0, motionArgs.edgeFlags);
7325 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7326 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7327 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7328 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7329 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7330 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7331 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7332 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7333
7334 // Last finger up.
7335 processMTSync(mapper);
7336 processSync(mapper);
7337
7338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7339 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7340 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7341 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7342 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7343 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7344 ASSERT_EQ(0, motionArgs.flags);
7345 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7346 ASSERT_EQ(0, motionArgs.buttonState);
7347 ASSERT_EQ(0, motionArgs.edgeFlags);
7348 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7349 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7350 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7351 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7352 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7353 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7354 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7355 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7356
7357 // Should not have sent any more keys or motions.
7358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7360}
7361
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007362TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7363 addConfigurationProperty("touch.deviceType", "touchScreen");
7364 prepareDisplay(DISPLAY_ORIENTATION_0);
7365
7366 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7367 /*fuzz*/ 0, /*resolution*/ 10);
7368 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7369 /*fuzz*/ 0, /*resolution*/ 11);
7370 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7371 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7372 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7373 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7374 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7375 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7376 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7377 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7378
7379 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7380
7381 // X and Y axes
7382 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7383 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7384 // Touch major and minor
7385 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7386 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7387 // Tool major and minor
7388 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7389 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7390}
7391
7392TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7393 addConfigurationProperty("touch.deviceType", "touchScreen");
7394 prepareDisplay(DISPLAY_ORIENTATION_0);
7395
7396 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7397 /*fuzz*/ 0, /*resolution*/ 10);
7398 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7399 /*fuzz*/ 0, /*resolution*/ 11);
7400
7401 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7402
7403 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7404
7405 // Touch major and minor
7406 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7407 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7408 // Tool major and minor
7409 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7410 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7411}
7412
Michael Wrightd02c5b62014-02-10 15:10:22 -08007413TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007414 addConfigurationProperty("touch.deviceType", "touchScreen");
7415 prepareDisplay(DISPLAY_ORIENTATION_0);
7416 prepareAxes(POSITION | ID);
7417 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007418 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007419
arthurhungdcef2dc2020-08-11 14:47:50 +08007420 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007421
7422 NotifyMotionArgs motionArgs;
7423
7424 // Two fingers down at once.
7425 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7426 processPosition(mapper, x1, y1);
7427 processId(mapper, 1);
7428 processMTSync(mapper);
7429 processPosition(mapper, x2, y2);
7430 processId(mapper, 2);
7431 processMTSync(mapper);
7432 processSync(mapper);
7433
7434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7435 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7436 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7437 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7438 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7439 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7440 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7441
7442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007443 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007444 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7445 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7446 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7447 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7448 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7449 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7450 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7451 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7452 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7453
7454 // Move.
7455 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7456 processPosition(mapper, x1, y1);
7457 processId(mapper, 1);
7458 processMTSync(mapper);
7459 processPosition(mapper, x2, y2);
7460 processId(mapper, 2);
7461 processMTSync(mapper);
7462 processSync(mapper);
7463
7464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7465 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7466 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7467 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7468 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7469 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7470 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7471 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7472 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7473 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7474 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7475
7476 // First finger up.
7477 x2 += 15; y2 -= 20;
7478 processPosition(mapper, x2, y2);
7479 processId(mapper, 2);
7480 processMTSync(mapper);
7481 processSync(mapper);
7482
7483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007484 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007485 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7486 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7487 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7488 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7489 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7490 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7491 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7493 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7494
7495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7496 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7497 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7498 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7499 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7500 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7501 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7502
7503 // Move.
7504 x2 += 20; y2 -= 25;
7505 processPosition(mapper, x2, y2);
7506 processId(mapper, 2);
7507 processMTSync(mapper);
7508 processSync(mapper);
7509
7510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7511 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7512 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7513 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7514 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7516 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7517
7518 // New finger down.
7519 int32_t x3 = 700, y3 = 300;
7520 processPosition(mapper, x2, y2);
7521 processId(mapper, 2);
7522 processMTSync(mapper);
7523 processPosition(mapper, x3, y3);
7524 processId(mapper, 3);
7525 processMTSync(mapper);
7526 processSync(mapper);
7527
7528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007529 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007530 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7531 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7532 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7533 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7534 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7535 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7536 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7537 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7538 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7539
7540 // Second finger up.
7541 x3 += 30; y3 -= 20;
7542 processPosition(mapper, x3, y3);
7543 processId(mapper, 3);
7544 processMTSync(mapper);
7545 processSync(mapper);
7546
7547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007548 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007549 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7550 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7551 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7552 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7553 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7554 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7555 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7557 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7558
7559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7560 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7561 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7562 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7563 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7564 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7565 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7566
7567 // Last finger up.
7568 processMTSync(mapper);
7569 processSync(mapper);
7570
7571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7572 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7573 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7574 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7575 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7576 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7577 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7578
7579 // Should not have sent any more keys or motions.
7580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7582}
7583
7584TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007585 addConfigurationProperty("touch.deviceType", "touchScreen");
7586 prepareDisplay(DISPLAY_ORIENTATION_0);
7587 prepareAxes(POSITION | ID | SLOT);
7588 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007589 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007590
arthurhungdcef2dc2020-08-11 14:47:50 +08007591 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007592
7593 NotifyMotionArgs motionArgs;
7594
7595 // Two fingers down at once.
7596 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7597 processPosition(mapper, x1, y1);
7598 processId(mapper, 1);
7599 processSlot(mapper, 1);
7600 processPosition(mapper, x2, y2);
7601 processId(mapper, 2);
7602 processSync(mapper);
7603
7604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7605 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7606 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7607 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7608 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7609 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7610 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7611
7612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007613 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007614 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7615 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7616 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7617 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7618 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7619 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7620 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7622 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7623
7624 // Move.
7625 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7626 processSlot(mapper, 0);
7627 processPosition(mapper, x1, y1);
7628 processSlot(mapper, 1);
7629 processPosition(mapper, x2, y2);
7630 processSync(mapper);
7631
7632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7633 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7634 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7635 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7636 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7637 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7638 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7640 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7641 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7642 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7643
7644 // First finger up.
7645 x2 += 15; y2 -= 20;
7646 processSlot(mapper, 0);
7647 processId(mapper, -1);
7648 processSlot(mapper, 1);
7649 processPosition(mapper, x2, y2);
7650 processSync(mapper);
7651
7652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007653 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007654 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7655 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7656 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7657 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7658 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7660 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7662 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7663
7664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7665 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7666 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7667 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7668 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7669 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7670 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7671
7672 // Move.
7673 x2 += 20; y2 -= 25;
7674 processPosition(mapper, x2, y2);
7675 processSync(mapper);
7676
7677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7678 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7679 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7680 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7681 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7682 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7683 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7684
7685 // New finger down.
7686 int32_t x3 = 700, y3 = 300;
7687 processPosition(mapper, x2, y2);
7688 processSlot(mapper, 0);
7689 processId(mapper, 3);
7690 processPosition(mapper, x3, y3);
7691 processSync(mapper);
7692
7693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007694 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007695 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7696 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7697 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7698 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7699 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7701 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7703 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7704
7705 // Second finger up.
7706 x3 += 30; y3 -= 20;
7707 processSlot(mapper, 1);
7708 processId(mapper, -1);
7709 processSlot(mapper, 0);
7710 processPosition(mapper, x3, y3);
7711 processSync(mapper);
7712
7713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007714 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007715 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7716 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7717 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7718 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7719 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7720 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7721 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7722 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7723 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7724
7725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7726 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7727 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7728 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7729 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7730 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7731 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7732
7733 // Last finger up.
7734 processId(mapper, -1);
7735 processSync(mapper);
7736
7737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7738 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7739 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7740 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7741 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7742 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7743 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7744
7745 // Should not have sent any more keys or motions.
7746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7748}
7749
7750TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007751 addConfigurationProperty("touch.deviceType", "touchScreen");
7752 prepareDisplay(DISPLAY_ORIENTATION_0);
7753 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007754 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007755
7756 // These calculations are based on the input device calibration documentation.
7757 int32_t rawX = 100;
7758 int32_t rawY = 200;
7759 int32_t rawTouchMajor = 7;
7760 int32_t rawTouchMinor = 6;
7761 int32_t rawToolMajor = 9;
7762 int32_t rawToolMinor = 8;
7763 int32_t rawPressure = 11;
7764 int32_t rawDistance = 0;
7765 int32_t rawOrientation = 3;
7766 int32_t id = 5;
7767
7768 float x = toDisplayX(rawX);
7769 float y = toDisplayY(rawY);
7770 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7771 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7772 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7773 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7774 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7775 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7776 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7777 float distance = float(rawDistance);
7778
7779 processPosition(mapper, rawX, rawY);
7780 processTouchMajor(mapper, rawTouchMajor);
7781 processTouchMinor(mapper, rawTouchMinor);
7782 processToolMajor(mapper, rawToolMajor);
7783 processToolMinor(mapper, rawToolMinor);
7784 processPressure(mapper, rawPressure);
7785 processOrientation(mapper, rawOrientation);
7786 processDistance(mapper, rawDistance);
7787 processId(mapper, id);
7788 processMTSync(mapper);
7789 processSync(mapper);
7790
7791 NotifyMotionArgs args;
7792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7793 ASSERT_EQ(0, args.pointerProperties[0].id);
7794 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7795 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7796 orientation, distance));
7797}
7798
7799TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007800 addConfigurationProperty("touch.deviceType", "touchScreen");
7801 prepareDisplay(DISPLAY_ORIENTATION_0);
7802 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7803 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007804 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007805
7806 // These calculations are based on the input device calibration documentation.
7807 int32_t rawX = 100;
7808 int32_t rawY = 200;
7809 int32_t rawTouchMajor = 140;
7810 int32_t rawTouchMinor = 120;
7811 int32_t rawToolMajor = 180;
7812 int32_t rawToolMinor = 160;
7813
7814 float x = toDisplayX(rawX);
7815 float y = toDisplayY(rawY);
7816 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7817 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7818 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7819 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7820 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7821
7822 processPosition(mapper, rawX, rawY);
7823 processTouchMajor(mapper, rawTouchMajor);
7824 processTouchMinor(mapper, rawTouchMinor);
7825 processToolMajor(mapper, rawToolMajor);
7826 processToolMinor(mapper, rawToolMinor);
7827 processMTSync(mapper);
7828 processSync(mapper);
7829
7830 NotifyMotionArgs args;
7831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7832 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7833 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7834}
7835
7836TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007837 addConfigurationProperty("touch.deviceType", "touchScreen");
7838 prepareDisplay(DISPLAY_ORIENTATION_0);
7839 prepareAxes(POSITION | TOUCH | TOOL);
7840 addConfigurationProperty("touch.size.calibration", "diameter");
7841 addConfigurationProperty("touch.size.scale", "10");
7842 addConfigurationProperty("touch.size.bias", "160");
7843 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007844 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007845
7846 // These calculations are based on the input device calibration documentation.
7847 // Note: We only provide a single common touch/tool value because the device is assumed
7848 // not to emit separate values for each pointer (isSummed = 1).
7849 int32_t rawX = 100;
7850 int32_t rawY = 200;
7851 int32_t rawX2 = 150;
7852 int32_t rawY2 = 250;
7853 int32_t rawTouchMajor = 5;
7854 int32_t rawToolMajor = 8;
7855
7856 float x = toDisplayX(rawX);
7857 float y = toDisplayY(rawY);
7858 float x2 = toDisplayX(rawX2);
7859 float y2 = toDisplayY(rawY2);
7860 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7861 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7862 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7863
7864 processPosition(mapper, rawX, rawY);
7865 processTouchMajor(mapper, rawTouchMajor);
7866 processToolMajor(mapper, rawToolMajor);
7867 processMTSync(mapper);
7868 processPosition(mapper, rawX2, rawY2);
7869 processTouchMajor(mapper, rawTouchMajor);
7870 processToolMajor(mapper, rawToolMajor);
7871 processMTSync(mapper);
7872 processSync(mapper);
7873
7874 NotifyMotionArgs args;
7875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7876 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7877
7878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007879 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007880 ASSERT_EQ(size_t(2), args.pointerCount);
7881 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7882 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7883 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7884 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7885}
7886
7887TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007888 addConfigurationProperty("touch.deviceType", "touchScreen");
7889 prepareDisplay(DISPLAY_ORIENTATION_0);
7890 prepareAxes(POSITION | TOUCH | TOOL);
7891 addConfigurationProperty("touch.size.calibration", "area");
7892 addConfigurationProperty("touch.size.scale", "43");
7893 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007894 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007895
7896 // These calculations are based on the input device calibration documentation.
7897 int32_t rawX = 100;
7898 int32_t rawY = 200;
7899 int32_t rawTouchMajor = 5;
7900 int32_t rawToolMajor = 8;
7901
7902 float x = toDisplayX(rawX);
7903 float y = toDisplayY(rawY);
7904 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7905 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7906 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7907
7908 processPosition(mapper, rawX, rawY);
7909 processTouchMajor(mapper, rawTouchMajor);
7910 processToolMajor(mapper, rawToolMajor);
7911 processMTSync(mapper);
7912 processSync(mapper);
7913
7914 NotifyMotionArgs args;
7915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7916 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7917 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7918}
7919
7920TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007921 addConfigurationProperty("touch.deviceType", "touchScreen");
7922 prepareDisplay(DISPLAY_ORIENTATION_0);
7923 prepareAxes(POSITION | PRESSURE);
7924 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7925 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007926 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007927
Michael Wrightaa449c92017-12-13 21:21:43 +00007928 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007929 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007930 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7931 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7932 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7933
Michael Wrightd02c5b62014-02-10 15:10:22 -08007934 // These calculations are based on the input device calibration documentation.
7935 int32_t rawX = 100;
7936 int32_t rawY = 200;
7937 int32_t rawPressure = 60;
7938
7939 float x = toDisplayX(rawX);
7940 float y = toDisplayY(rawY);
7941 float pressure = float(rawPressure) * 0.01f;
7942
7943 processPosition(mapper, rawX, rawY);
7944 processPressure(mapper, rawPressure);
7945 processMTSync(mapper);
7946 processSync(mapper);
7947
7948 NotifyMotionArgs args;
7949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7950 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7951 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7952}
7953
7954TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007955 addConfigurationProperty("touch.deviceType", "touchScreen");
7956 prepareDisplay(DISPLAY_ORIENTATION_0);
7957 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007958 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007959
7960 NotifyMotionArgs motionArgs;
7961 NotifyKeyArgs keyArgs;
7962
7963 processId(mapper, 1);
7964 processPosition(mapper, 100, 200);
7965 processSync(mapper);
7966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7967 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7968 ASSERT_EQ(0, motionArgs.buttonState);
7969
7970 // press BTN_LEFT, release BTN_LEFT
7971 processKey(mapper, BTN_LEFT, 1);
7972 processSync(mapper);
7973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7974 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7975 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7976
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7978 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7979 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7980
Michael Wrightd02c5b62014-02-10 15:10:22 -08007981 processKey(mapper, BTN_LEFT, 0);
7982 processSync(mapper);
7983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007984 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007985 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007986
7987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007988 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007989 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007990
7991 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7992 processKey(mapper, BTN_RIGHT, 1);
7993 processKey(mapper, BTN_MIDDLE, 1);
7994 processSync(mapper);
7995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7996 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7997 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7998 motionArgs.buttonState);
7999
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8001 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8002 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8003
8004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8005 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8006 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8007 motionArgs.buttonState);
8008
Michael Wrightd02c5b62014-02-10 15:10:22 -08008009 processKey(mapper, BTN_RIGHT, 0);
8010 processSync(mapper);
8011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008012 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008013 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008014
8015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008016 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008017 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008018
8019 processKey(mapper, BTN_MIDDLE, 0);
8020 processSync(mapper);
8021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008022 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008023 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008024
8025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008026 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008027 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008028
8029 // press BTN_BACK, release BTN_BACK
8030 processKey(mapper, BTN_BACK, 1);
8031 processSync(mapper);
8032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8033 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8034 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008035
Michael Wrightd02c5b62014-02-10 15:10:22 -08008036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008037 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008038 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8039
8040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8041 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8042 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008043
8044 processKey(mapper, BTN_BACK, 0);
8045 processSync(mapper);
8046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008047 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008048 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008049
8050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008051 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008052 ASSERT_EQ(0, motionArgs.buttonState);
8053
Michael Wrightd02c5b62014-02-10 15:10:22 -08008054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8055 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8056 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8057
8058 // press BTN_SIDE, release BTN_SIDE
8059 processKey(mapper, BTN_SIDE, 1);
8060 processSync(mapper);
8061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8062 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8063 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008064
Michael Wrightd02c5b62014-02-10 15:10:22 -08008065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008066 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008067 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8068
8069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8070 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8071 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008072
8073 processKey(mapper, BTN_SIDE, 0);
8074 processSync(mapper);
8075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008076 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008077 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008078
8079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008080 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008081 ASSERT_EQ(0, motionArgs.buttonState);
8082
Michael Wrightd02c5b62014-02-10 15:10:22 -08008083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8084 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8085 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8086
8087 // press BTN_FORWARD, release BTN_FORWARD
8088 processKey(mapper, BTN_FORWARD, 1);
8089 processSync(mapper);
8090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8091 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8092 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008093
Michael Wrightd02c5b62014-02-10 15:10:22 -08008094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008095 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008096 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8097
8098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8099 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8100 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008101
8102 processKey(mapper, BTN_FORWARD, 0);
8103 processSync(mapper);
8104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008105 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008106 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008107
8108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008109 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008110 ASSERT_EQ(0, motionArgs.buttonState);
8111
Michael Wrightd02c5b62014-02-10 15:10:22 -08008112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8113 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8114 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8115
8116 // press BTN_EXTRA, release BTN_EXTRA
8117 processKey(mapper, BTN_EXTRA, 1);
8118 processSync(mapper);
8119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8120 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8121 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008122
Michael Wrightd02c5b62014-02-10 15:10:22 -08008123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008124 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008125 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8126
8127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8128 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8129 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008130
8131 processKey(mapper, BTN_EXTRA, 0);
8132 processSync(mapper);
8133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008134 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008135 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008136
8137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008138 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008139 ASSERT_EQ(0, motionArgs.buttonState);
8140
Michael Wrightd02c5b62014-02-10 15:10:22 -08008141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8142 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8143 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8144
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8146
Michael Wrightd02c5b62014-02-10 15:10:22 -08008147 // press BTN_STYLUS, release BTN_STYLUS
8148 processKey(mapper, BTN_STYLUS, 1);
8149 processSync(mapper);
8150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8151 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008152 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8153
8154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8155 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8156 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008157
8158 processKey(mapper, BTN_STYLUS, 0);
8159 processSync(mapper);
8160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008161 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008162 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008163
8164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008165 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008166 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008167
8168 // press BTN_STYLUS2, release BTN_STYLUS2
8169 processKey(mapper, BTN_STYLUS2, 1);
8170 processSync(mapper);
8171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8172 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008173 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8174
8175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8176 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8177 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008178
8179 processKey(mapper, BTN_STYLUS2, 0);
8180 processSync(mapper);
8181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008182 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008183 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008184
8185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008186 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008187 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008188
8189 // release touch
8190 processId(mapper, -1);
8191 processSync(mapper);
8192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8193 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8194 ASSERT_EQ(0, motionArgs.buttonState);
8195}
8196
8197TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008198 addConfigurationProperty("touch.deviceType", "touchScreen");
8199 prepareDisplay(DISPLAY_ORIENTATION_0);
8200 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008201 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008202
8203 NotifyMotionArgs motionArgs;
8204
8205 // default tool type is finger
8206 processId(mapper, 1);
8207 processPosition(mapper, 100, 200);
8208 processSync(mapper);
8209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8210 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8211 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8212
8213 // eraser
8214 processKey(mapper, BTN_TOOL_RUBBER, 1);
8215 processSync(mapper);
8216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8217 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8218 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8219
8220 // stylus
8221 processKey(mapper, BTN_TOOL_RUBBER, 0);
8222 processKey(mapper, BTN_TOOL_PEN, 1);
8223 processSync(mapper);
8224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8225 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8226 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8227
8228 // brush
8229 processKey(mapper, BTN_TOOL_PEN, 0);
8230 processKey(mapper, BTN_TOOL_BRUSH, 1);
8231 processSync(mapper);
8232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8233 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8234 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8235
8236 // pencil
8237 processKey(mapper, BTN_TOOL_BRUSH, 0);
8238 processKey(mapper, BTN_TOOL_PENCIL, 1);
8239 processSync(mapper);
8240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8241 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8242 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8243
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008244 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008245 processKey(mapper, BTN_TOOL_PENCIL, 0);
8246 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8247 processSync(mapper);
8248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8249 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8250 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8251
8252 // mouse
8253 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8254 processKey(mapper, BTN_TOOL_MOUSE, 1);
8255 processSync(mapper);
8256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8257 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8258 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8259
8260 // lens
8261 processKey(mapper, BTN_TOOL_MOUSE, 0);
8262 processKey(mapper, BTN_TOOL_LENS, 1);
8263 processSync(mapper);
8264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8265 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8266 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8267
8268 // double-tap
8269 processKey(mapper, BTN_TOOL_LENS, 0);
8270 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8271 processSync(mapper);
8272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8273 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8274 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8275
8276 // triple-tap
8277 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8278 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8279 processSync(mapper);
8280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8281 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8282 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8283
8284 // quad-tap
8285 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8286 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8287 processSync(mapper);
8288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8289 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8290 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8291
8292 // finger
8293 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8294 processKey(mapper, BTN_TOOL_FINGER, 1);
8295 processSync(mapper);
8296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8298 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8299
8300 // stylus trumps finger
8301 processKey(mapper, BTN_TOOL_PEN, 1);
8302 processSync(mapper);
8303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8304 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8305 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8306
8307 // eraser trumps stylus
8308 processKey(mapper, BTN_TOOL_RUBBER, 1);
8309 processSync(mapper);
8310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8311 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8312 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8313
8314 // mouse trumps eraser
8315 processKey(mapper, BTN_TOOL_MOUSE, 1);
8316 processSync(mapper);
8317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8318 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8319 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8320
8321 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8322 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8323 processSync(mapper);
8324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8325 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8326 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8327
8328 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8329 processToolType(mapper, MT_TOOL_PEN);
8330 processSync(mapper);
8331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8332 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8333 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8334
8335 // back to default tool type
8336 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8337 processKey(mapper, BTN_TOOL_MOUSE, 0);
8338 processKey(mapper, BTN_TOOL_RUBBER, 0);
8339 processKey(mapper, BTN_TOOL_PEN, 0);
8340 processKey(mapper, BTN_TOOL_FINGER, 0);
8341 processSync(mapper);
8342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8343 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8344 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8345}
8346
8347TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008348 addConfigurationProperty("touch.deviceType", "touchScreen");
8349 prepareDisplay(DISPLAY_ORIENTATION_0);
8350 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008351 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008352 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008353
8354 NotifyMotionArgs motionArgs;
8355
8356 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8357 processId(mapper, 1);
8358 processPosition(mapper, 100, 200);
8359 processSync(mapper);
8360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8361 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8363 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8364
8365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8366 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8367 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8368 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8369
8370 // move a little
8371 processPosition(mapper, 150, 250);
8372 processSync(mapper);
8373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8374 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8376 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8377
8378 // down when BTN_TOUCH is pressed, pressure defaults to 1
8379 processKey(mapper, BTN_TOUCH, 1);
8380 processSync(mapper);
8381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8382 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8384 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8385
8386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8387 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8388 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8389 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8390
8391 // up when BTN_TOUCH is released, hover restored
8392 processKey(mapper, BTN_TOUCH, 0);
8393 processSync(mapper);
8394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8395 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8396 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8397 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8398
8399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8400 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8401 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8402 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8403
8404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8405 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8406 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8407 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8408
8409 // exit hover when pointer goes away
8410 processId(mapper, -1);
8411 processSync(mapper);
8412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8413 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8414 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8415 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8416}
8417
8418TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008419 addConfigurationProperty("touch.deviceType", "touchScreen");
8420 prepareDisplay(DISPLAY_ORIENTATION_0);
8421 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008422 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008423
8424 NotifyMotionArgs motionArgs;
8425
8426 // initially hovering because pressure is 0
8427 processId(mapper, 1);
8428 processPosition(mapper, 100, 200);
8429 processPressure(mapper, 0);
8430 processSync(mapper);
8431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8432 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8433 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8434 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8435
8436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8437 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8438 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8439 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8440
8441 // move a little
8442 processPosition(mapper, 150, 250);
8443 processSync(mapper);
8444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8445 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8446 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8447 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8448
8449 // down when pressure becomes non-zero
8450 processPressure(mapper, RAW_PRESSURE_MAX);
8451 processSync(mapper);
8452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8453 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8455 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8456
8457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8458 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8459 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8460 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8461
8462 // up when pressure becomes 0, hover restored
8463 processPressure(mapper, 0);
8464 processSync(mapper);
8465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8466 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8467 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8468 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8469
8470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8471 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8472 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8473 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8474
8475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8476 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8477 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8478 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8479
8480 // exit hover when pointer goes away
8481 processId(mapper, -1);
8482 processSync(mapper);
8483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8484 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8486 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8487}
8488
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008489/**
8490 * Set the input device port <--> display port associations, and check that the
8491 * events are routed to the display that matches the display port.
8492 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8493 */
8494TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008495 const std::string usb2 = "USB2";
8496 const uint8_t hdmi1 = 0;
8497 const uint8_t hdmi2 = 1;
8498 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008499 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008500
8501 addConfigurationProperty("touch.deviceType", "touchScreen");
8502 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008503 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008504
8505 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8506 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8507
8508 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8509 // for this input device is specified, and the matching viewport is not present,
8510 // the input device should be disabled (at the mapper level).
8511
8512 // Add viewport for display 2 on hdmi2
8513 prepareSecondaryDisplay(type, hdmi2);
8514 // Send a touch event
8515 processPosition(mapper, 100, 100);
8516 processSync(mapper);
8517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8518
8519 // Add viewport for display 1 on hdmi1
8520 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8521 // Send a touch event again
8522 processPosition(mapper, 100, 100);
8523 processSync(mapper);
8524
8525 NotifyMotionArgs args;
8526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8527 ASSERT_EQ(DISPLAY_ID, args.displayId);
8528}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008529
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008530TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8531 addConfigurationProperty("touch.deviceType", "touchScreen");
8532 prepareAxes(POSITION);
8533 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8534
8535 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8536
8537 prepareDisplay(DISPLAY_ORIENTATION_0);
8538 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8539
8540 // Send a touch event
8541 processPosition(mapper, 100, 100);
8542 processSync(mapper);
8543
8544 NotifyMotionArgs args;
8545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8546 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8547}
8548
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008549TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008550 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008551 std::shared_ptr<FakePointerController> fakePointerController =
8552 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008553 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008554 fakePointerController->setPosition(100, 200);
8555 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008556 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008557
Garfield Tan888a6a42020-01-09 11:39:16 -08008558 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008559 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008560
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008561 prepareDisplay(DISPLAY_ORIENTATION_0);
8562 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008563 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008564
8565 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008566 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008567
8568 NotifyMotionArgs motionArgs;
8569 processPosition(mapper, 100, 100);
8570 processSync(mapper);
8571
8572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8573 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8574 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8575}
8576
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008577/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008578 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8579 */
8580TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8581 addConfigurationProperty("touch.deviceType", "touchScreen");
8582 prepareAxes(POSITION);
8583 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8584
8585 prepareDisplay(DISPLAY_ORIENTATION_0);
8586 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8587 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8588 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8589 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8590
8591 NotifyMotionArgs args;
8592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8593 ASSERT_EQ(26, args.readTime);
8594
8595 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8596 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8597 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8598
8599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8600 ASSERT_EQ(33, args.readTime);
8601}
8602
8603/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008604 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8605 * events should not be delivered to the listener.
8606 */
8607TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8608 addConfigurationProperty("touch.deviceType", "touchScreen");
8609 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8610 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8611 ViewportType::INTERNAL);
8612 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8613 prepareAxes(POSITION);
8614 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8615
8616 NotifyMotionArgs motionArgs;
8617 processPosition(mapper, 100, 100);
8618 processSync(mapper);
8619
8620 mFakeListener->assertNotifyMotionWasNotCalled();
8621}
8622
Garfield Tanc734e4f2021-01-15 20:01:39 -08008623TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8624 addConfigurationProperty("touch.deviceType", "touchScreen");
8625 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8626 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8627 ViewportType::INTERNAL);
8628 std::optional<DisplayViewport> optionalDisplayViewport =
8629 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8630 ASSERT_TRUE(optionalDisplayViewport.has_value());
8631 DisplayViewport displayViewport = *optionalDisplayViewport;
8632
8633 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8634 prepareAxes(POSITION);
8635 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8636
8637 // Finger down
8638 int32_t x = 100, y = 100;
8639 processPosition(mapper, x, y);
8640 processSync(mapper);
8641
8642 NotifyMotionArgs motionArgs;
8643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8644 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8645
8646 // Deactivate display viewport
8647 displayViewport.isActive = false;
8648 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8649 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8650
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008651 // The ongoing touch should be canceled immediately
8652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8653 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8654
8655 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008656 x += 10, y += 10;
8657 processPosition(mapper, x, y);
8658 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008660
8661 // Reactivate display viewport
8662 displayViewport.isActive = true;
8663 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8664 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8665
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008666 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008667 x += 10, y += 10;
8668 processPosition(mapper, x, y);
8669 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8671 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008672}
8673
Arthur Hung7c645402019-01-25 17:45:42 +08008674TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8675 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008676 prepareAxes(POSITION | ID | SLOT);
8677 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008678 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008679
8680 // Create the second touch screen device, and enable multi fingers.
8681 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008682 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008683 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008684 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008685 std::shared_ptr<InputDevice> device2 =
8686 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008687 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008688
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008689 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8690 0 /*flat*/, 0 /*fuzz*/);
8691 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8692 0 /*flat*/, 0 /*fuzz*/);
8693 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8694 0 /*flat*/, 0 /*fuzz*/);
8695 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8696 0 /*flat*/, 0 /*fuzz*/);
8697 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8698 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8699 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008700
8701 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008702 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008703 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8704 device2->reset(ARBITRARY_TIME);
8705
8706 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008707 std::shared_ptr<FakePointerController> fakePointerController =
8708 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008709 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008710
8711 // Setup policy for associated displays and show touches.
8712 const uint8_t hdmi1 = 0;
8713 const uint8_t hdmi2 = 1;
8714 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8715 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8716 mFakePolicy->setShowTouches(true);
8717
8718 // Create displays.
8719 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008720 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008721
8722 // Default device will reconfigure above, need additional reconfiguration for another device.
8723 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan197e0862022-07-01 14:28:00 +00008724 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8725 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008726
8727 // Two fingers down at default display.
8728 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8729 processPosition(mapper, x1, y1);
8730 processId(mapper, 1);
8731 processSlot(mapper, 1);
8732 processPosition(mapper, x2, y2);
8733 processId(mapper, 2);
8734 processSync(mapper);
8735
8736 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8737 fakePointerController->getSpots().find(DISPLAY_ID);
8738 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8739 ASSERT_EQ(size_t(2), iter->second.size());
8740
8741 // Two fingers down at second display.
8742 processPosition(mapper2, x1, y1);
8743 processId(mapper2, 1);
8744 processSlot(mapper2, 1);
8745 processPosition(mapper2, x2, y2);
8746 processId(mapper2, 2);
8747 processSync(mapper2);
8748
8749 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8750 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8751 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008752
8753 // Disable the show touches configuration and ensure the spots are cleared.
8754 mFakePolicy->setShowTouches(false);
8755 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8756 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
8757
8758 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008759}
8760
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008761TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008762 prepareAxes(POSITION);
8763 addConfigurationProperty("touch.deviceType", "touchScreen");
8764 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008765 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008766
8767 NotifyMotionArgs motionArgs;
8768 // Unrotated video frame
8769 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8770 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008771 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008772 processPosition(mapper, 100, 200);
8773 processSync(mapper);
8774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8775 ASSERT_EQ(frames, motionArgs.videoFrames);
8776
8777 // Subsequent touch events should not have any videoframes
8778 // This is implemented separately in FakeEventHub,
8779 // but that should match the behaviour of TouchVideoDevice.
8780 processPosition(mapper, 200, 200);
8781 processSync(mapper);
8782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8783 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8784}
8785
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008786TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008787 prepareAxes(POSITION);
8788 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008789 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008790 // Unrotated video frame
8791 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8792 NotifyMotionArgs motionArgs;
8793
8794 // Test all 4 orientations
8795 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008796 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8797 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8798 clearViewports();
8799 prepareDisplay(orientation);
8800 std::vector<TouchVideoFrame> frames{frame};
8801 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8802 processPosition(mapper, 100, 200);
8803 processSync(mapper);
8804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8805 ASSERT_EQ(frames, motionArgs.videoFrames);
8806 }
8807}
8808
8809TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8810 prepareAxes(POSITION);
8811 addConfigurationProperty("touch.deviceType", "touchScreen");
8812 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8813 // orientation-aware are affected by display rotation.
8814 addConfigurationProperty("touch.orientationAware", "0");
8815 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8816 // Unrotated video frame
8817 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8818 NotifyMotionArgs motionArgs;
8819
8820 // Test all 4 orientations
8821 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008822 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8823 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8824 clearViewports();
8825 prepareDisplay(orientation);
8826 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008827 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008828 processPosition(mapper, 100, 200);
8829 processSync(mapper);
8830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008831 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8832 // compared to the display. This is so that when the window transform (which contains the
8833 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8834 // window's coordinate space.
8835 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008836 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08008837
8838 // Release finger.
8839 processSync(mapper);
8840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008841 }
8842}
8843
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008844TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008845 prepareAxes(POSITION);
8846 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008847 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008848 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8849 // so mix these.
8850 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8851 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8852 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8853 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8854 NotifyMotionArgs motionArgs;
8855
8856 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008857 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008858 processPosition(mapper, 100, 200);
8859 processSync(mapper);
8860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008861 ASSERT_EQ(frames, motionArgs.videoFrames);
8862}
8863
8864TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8865 prepareAxes(POSITION);
8866 addConfigurationProperty("touch.deviceType", "touchScreen");
8867 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8868 // orientation-aware are affected by display rotation.
8869 addConfigurationProperty("touch.orientationAware", "0");
8870 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8871 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8872 // so mix these.
8873 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8874 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8875 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8876 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8877 NotifyMotionArgs motionArgs;
8878
8879 prepareDisplay(DISPLAY_ORIENTATION_90);
8880 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8881 processPosition(mapper, 100, 200);
8882 processSync(mapper);
8883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8884 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8885 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8886 // compared to the display. This is so that when the window transform (which contains the
8887 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8888 // window's coordinate space.
8889 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8890 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008891 ASSERT_EQ(frames, motionArgs.videoFrames);
8892}
8893
Arthur Hung9da14732019-09-02 16:16:58 +08008894/**
8895 * If we had defined port associations, but the viewport is not ready, the touch device would be
8896 * expected to be disabled, and it should be enabled after the viewport has found.
8897 */
8898TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008899 constexpr uint8_t hdmi2 = 1;
8900 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008901 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008902
8903 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8904
8905 addConfigurationProperty("touch.deviceType", "touchScreen");
8906 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008907 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008908
8909 ASSERT_EQ(mDevice->isEnabled(), false);
8910
8911 // Add display on hdmi2, the device should be enabled and can receive touch event.
8912 prepareSecondaryDisplay(type, hdmi2);
8913 ASSERT_EQ(mDevice->isEnabled(), true);
8914
8915 // Send a touch event.
8916 processPosition(mapper, 100, 100);
8917 processSync(mapper);
8918
8919 NotifyMotionArgs args;
8920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8921 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8922}
8923
Arthur Hung421eb1c2020-01-16 00:09:42 +08008924TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008925 addConfigurationProperty("touch.deviceType", "touchScreen");
8926 prepareDisplay(DISPLAY_ORIENTATION_0);
8927 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008928 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008929
8930 NotifyMotionArgs motionArgs;
8931
8932 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8933 // finger down
8934 processId(mapper, 1);
8935 processPosition(mapper, x1, y1);
8936 processSync(mapper);
8937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8938 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8939 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8940
8941 // finger move
8942 processId(mapper, 1);
8943 processPosition(mapper, x2, y2);
8944 processSync(mapper);
8945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8946 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8947 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8948
8949 // finger up.
8950 processId(mapper, -1);
8951 processSync(mapper);
8952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8953 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8954 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8955
8956 // new finger down
8957 processId(mapper, 1);
8958 processPosition(mapper, x3, y3);
8959 processSync(mapper);
8960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8961 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8962 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8963}
8964
8965/**
arthurhungcc7f9802020-04-30 17:55:40 +08008966 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8967 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008968 */
arthurhungcc7f9802020-04-30 17:55:40 +08008969TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008970 addConfigurationProperty("touch.deviceType", "touchScreen");
8971 prepareDisplay(DISPLAY_ORIENTATION_0);
8972 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008973 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008974
8975 NotifyMotionArgs motionArgs;
8976
8977 // default tool type is finger
8978 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008979 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008980 processPosition(mapper, x1, y1);
8981 processSync(mapper);
8982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8983 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8984 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8985
8986 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8987 processToolType(mapper, MT_TOOL_PALM);
8988 processSync(mapper);
8989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8990 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8991
8992 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008993 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008994 processPosition(mapper, x2, y2);
8995 processSync(mapper);
8996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8997
8998 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008999 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009000 processSync(mapper);
9001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9002
9003 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009004 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009005 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009006 processPosition(mapper, x3, y3);
9007 processSync(mapper);
9008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9009 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9010 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9011}
9012
arthurhungbf89a482020-04-17 17:37:55 +08009013/**
arthurhungcc7f9802020-04-30 17:55:40 +08009014 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9015 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009016 */
arthurhungcc7f9802020-04-30 17:55:40 +08009017TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009018 addConfigurationProperty("touch.deviceType", "touchScreen");
9019 prepareDisplay(DISPLAY_ORIENTATION_0);
9020 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9021 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9022
9023 NotifyMotionArgs motionArgs;
9024
9025 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009026 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9027 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009028 processPosition(mapper, x1, y1);
9029 processSync(mapper);
9030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9031 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9032 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9033
9034 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009035 processSlot(mapper, SECOND_SLOT);
9036 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009037 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009038 processSync(mapper);
9039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009040 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009041 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9042
9043 // If the tool type of the first finger changes to MT_TOOL_PALM,
9044 // we expect to receive ACTION_POINTER_UP with cancel flag.
9045 processSlot(mapper, FIRST_SLOT);
9046 processId(mapper, FIRST_TRACKING_ID);
9047 processToolType(mapper, MT_TOOL_PALM);
9048 processSync(mapper);
9049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009050 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009051 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9052
9053 // The following MOVE events of second finger should be processed.
9054 processSlot(mapper, SECOND_SLOT);
9055 processId(mapper, SECOND_TRACKING_ID);
9056 processPosition(mapper, x2 + 1, y2 + 1);
9057 processSync(mapper);
9058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9060 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9061
9062 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9063 // it. Second finger receive move.
9064 processSlot(mapper, FIRST_SLOT);
9065 processId(mapper, INVALID_TRACKING_ID);
9066 processSync(mapper);
9067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9068 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9069 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9070
9071 // Second finger keeps moving.
9072 processSlot(mapper, SECOND_SLOT);
9073 processId(mapper, SECOND_TRACKING_ID);
9074 processPosition(mapper, x2 + 2, y2 + 2);
9075 processSync(mapper);
9076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9077 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9078 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9079
9080 // Second finger up.
9081 processId(mapper, INVALID_TRACKING_ID);
9082 processSync(mapper);
9083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9084 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9085 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9086}
9087
9088/**
9089 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9090 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9091 */
9092TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9093 addConfigurationProperty("touch.deviceType", "touchScreen");
9094 prepareDisplay(DISPLAY_ORIENTATION_0);
9095 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9096 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9097
9098 NotifyMotionArgs motionArgs;
9099
9100 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9101 // First finger down.
9102 processId(mapper, FIRST_TRACKING_ID);
9103 processPosition(mapper, x1, y1);
9104 processSync(mapper);
9105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9106 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9107 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9108
9109 // Second finger down.
9110 processSlot(mapper, SECOND_SLOT);
9111 processId(mapper, SECOND_TRACKING_ID);
9112 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009113 processSync(mapper);
9114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009115 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009116 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9117
arthurhungcc7f9802020-04-30 17:55:40 +08009118 // If the tool type of the first finger changes to MT_TOOL_PALM,
9119 // we expect to receive ACTION_POINTER_UP with cancel flag.
9120 processSlot(mapper, FIRST_SLOT);
9121 processId(mapper, FIRST_TRACKING_ID);
9122 processToolType(mapper, MT_TOOL_PALM);
9123 processSync(mapper);
9124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009125 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009126 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9127
9128 // Second finger keeps moving.
9129 processSlot(mapper, SECOND_SLOT);
9130 processId(mapper, SECOND_TRACKING_ID);
9131 processPosition(mapper, x2 + 1, y2 + 1);
9132 processSync(mapper);
9133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9134 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9135
9136 // second finger becomes palm, receive cancel due to only 1 finger is active.
9137 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009138 processToolType(mapper, MT_TOOL_PALM);
9139 processSync(mapper);
9140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9141 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9142
arthurhungcc7f9802020-04-30 17:55:40 +08009143 // third finger down.
9144 processSlot(mapper, THIRD_SLOT);
9145 processId(mapper, THIRD_TRACKING_ID);
9146 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009147 processPosition(mapper, x3, y3);
9148 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9150 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9151 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009152 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9153
9154 // third finger move
9155 processId(mapper, THIRD_TRACKING_ID);
9156 processPosition(mapper, x3 + 1, y3 + 1);
9157 processSync(mapper);
9158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9159 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9160
9161 // first finger up, third finger receive move.
9162 processSlot(mapper, FIRST_SLOT);
9163 processId(mapper, INVALID_TRACKING_ID);
9164 processSync(mapper);
9165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9166 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9167 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9168
9169 // second finger up, third finger receive move.
9170 processSlot(mapper, SECOND_SLOT);
9171 processId(mapper, INVALID_TRACKING_ID);
9172 processSync(mapper);
9173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9174 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9175 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9176
9177 // third finger up.
9178 processSlot(mapper, THIRD_SLOT);
9179 processId(mapper, INVALID_TRACKING_ID);
9180 processSync(mapper);
9181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9182 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9183 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9184}
9185
9186/**
9187 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9188 * and the active finger could still be allowed to receive the events
9189 */
9190TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9191 addConfigurationProperty("touch.deviceType", "touchScreen");
9192 prepareDisplay(DISPLAY_ORIENTATION_0);
9193 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9194 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9195
9196 NotifyMotionArgs motionArgs;
9197
9198 // default tool type is finger
9199 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9200 processId(mapper, FIRST_TRACKING_ID);
9201 processPosition(mapper, x1, y1);
9202 processSync(mapper);
9203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9204 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9205 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9206
9207 // Second finger down.
9208 processSlot(mapper, SECOND_SLOT);
9209 processId(mapper, SECOND_TRACKING_ID);
9210 processPosition(mapper, x2, y2);
9211 processSync(mapper);
9212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009213 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009214 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9215
9216 // If the tool type of the second finger changes to MT_TOOL_PALM,
9217 // we expect to receive ACTION_POINTER_UP with cancel flag.
9218 processId(mapper, SECOND_TRACKING_ID);
9219 processToolType(mapper, MT_TOOL_PALM);
9220 processSync(mapper);
9221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009222 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009223 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9224
9225 // The following MOVE event should be processed.
9226 processSlot(mapper, FIRST_SLOT);
9227 processId(mapper, FIRST_TRACKING_ID);
9228 processPosition(mapper, x1 + 1, y1 + 1);
9229 processSync(mapper);
9230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9231 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9232 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9233
9234 // second finger up.
9235 processSlot(mapper, SECOND_SLOT);
9236 processId(mapper, INVALID_TRACKING_ID);
9237 processSync(mapper);
9238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9239 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9240
9241 // first finger keep moving
9242 processSlot(mapper, FIRST_SLOT);
9243 processId(mapper, FIRST_TRACKING_ID);
9244 processPosition(mapper, x1 + 2, y1 + 2);
9245 processSync(mapper);
9246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9247 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9248
9249 // first finger up.
9250 processId(mapper, INVALID_TRACKING_ID);
9251 processSync(mapper);
9252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9253 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9254 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009255}
9256
Arthur Hung9ad18942021-06-19 02:04:46 +00009257/**
9258 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9259 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9260 * cause slot be valid again.
9261 */
9262TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9263 addConfigurationProperty("touch.deviceType", "touchScreen");
9264 prepareDisplay(DISPLAY_ORIENTATION_0);
9265 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9266 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9267
9268 NotifyMotionArgs motionArgs;
9269
9270 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9271 // First finger down.
9272 processId(mapper, FIRST_TRACKING_ID);
9273 processPosition(mapper, x1, y1);
9274 processPressure(mapper, RAW_PRESSURE_MAX);
9275 processSync(mapper);
9276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9277 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9278 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9279
9280 // First finger move.
9281 processId(mapper, FIRST_TRACKING_ID);
9282 processPosition(mapper, x1 + 1, y1 + 1);
9283 processPressure(mapper, RAW_PRESSURE_MAX);
9284 processSync(mapper);
9285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9286 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9287 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9288
9289 // Second finger down.
9290 processSlot(mapper, SECOND_SLOT);
9291 processId(mapper, SECOND_TRACKING_ID);
9292 processPosition(mapper, x2, y2);
9293 processPressure(mapper, RAW_PRESSURE_MAX);
9294 processSync(mapper);
9295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009296 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009297 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9298
9299 // second finger up with some unexpected data.
9300 processSlot(mapper, SECOND_SLOT);
9301 processId(mapper, INVALID_TRACKING_ID);
9302 processPosition(mapper, x2, y2);
9303 processSync(mapper);
9304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009305 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009306 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9307
9308 // first finger up with some unexpected data.
9309 processSlot(mapper, FIRST_SLOT);
9310 processId(mapper, INVALID_TRACKING_ID);
9311 processPosition(mapper, x2, y2);
9312 processPressure(mapper, RAW_PRESSURE_MAX);
9313 processSync(mapper);
9314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9315 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9316 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9317}
9318
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009319// --- MultiTouchInputMapperTest_ExternalDevice ---
9320
9321class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9322protected:
Chris Yea52ade12020-08-27 16:49:20 -07009323 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009324};
9325
9326/**
9327 * Expect fallback to internal viewport if device is external and external viewport is not present.
9328 */
9329TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9330 prepareAxes(POSITION);
9331 addConfigurationProperty("touch.deviceType", "touchScreen");
9332 prepareDisplay(DISPLAY_ORIENTATION_0);
9333 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9334
9335 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9336
9337 NotifyMotionArgs motionArgs;
9338
9339 // Expect the event to be sent to the internal viewport,
9340 // because an external viewport is not present.
9341 processPosition(mapper, 100, 100);
9342 processSync(mapper);
9343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9344 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9345
9346 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009347 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009348 processPosition(mapper, 100, 100);
9349 processSync(mapper);
9350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9351 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9352}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009353
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009354TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9355 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9356 std::shared_ptr<FakePointerController> fakePointerController =
9357 std::make_shared<FakePointerController>();
9358 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9359 fakePointerController->setPosition(0, 0);
9360 fakePointerController->setButtonState(0);
9361
9362 // prepare device and capture
9363 prepareDisplay(DISPLAY_ORIENTATION_0);
9364 prepareAxes(POSITION | ID | SLOT);
9365 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9366 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9367 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009368 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009369 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9370
9371 // captured touchpad should be a touchpad source
9372 NotifyDeviceResetArgs resetArgs;
9373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9374 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9375
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009376 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009377
9378 const InputDeviceInfo::MotionRange* relRangeX =
9379 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9380 ASSERT_NE(relRangeX, nullptr);
9381 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9382 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9383 const InputDeviceInfo::MotionRange* relRangeY =
9384 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9385 ASSERT_NE(relRangeY, nullptr);
9386 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9387 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9388
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009389 // run captured pointer tests - note that this is unscaled, so input listener events should be
9390 // identical to what the hardware sends (accounting for any
9391 // calibration).
9392 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009393 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009394 processId(mapper, 1);
9395 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9396 processKey(mapper, BTN_TOUCH, 1);
9397 processSync(mapper);
9398
9399 // expect coord[0] to contain initial location of touch 0
9400 NotifyMotionArgs args;
9401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9402 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9403 ASSERT_EQ(1U, args.pointerCount);
9404 ASSERT_EQ(0, args.pointerProperties[0].id);
9405 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9406 ASSERT_NO_FATAL_FAILURE(
9407 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9408
9409 // FINGER 1 DOWN
9410 processSlot(mapper, 1);
9411 processId(mapper, 2);
9412 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9413 processSync(mapper);
9414
9415 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009417 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009418 ASSERT_EQ(2U, args.pointerCount);
9419 ASSERT_EQ(0, args.pointerProperties[0].id);
9420 ASSERT_EQ(1, args.pointerProperties[1].id);
9421 ASSERT_NO_FATAL_FAILURE(
9422 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9423 ASSERT_NO_FATAL_FAILURE(
9424 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9425
9426 // FINGER 1 MOVE
9427 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9428 processSync(mapper);
9429
9430 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9431 // from move
9432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9433 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9434 ASSERT_NO_FATAL_FAILURE(
9435 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9436 ASSERT_NO_FATAL_FAILURE(
9437 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9438
9439 // FINGER 0 MOVE
9440 processSlot(mapper, 0);
9441 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9442 processSync(mapper);
9443
9444 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9446 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9447 ASSERT_NO_FATAL_FAILURE(
9448 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9449 ASSERT_NO_FATAL_FAILURE(
9450 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9451
9452 // BUTTON DOWN
9453 processKey(mapper, BTN_LEFT, 1);
9454 processSync(mapper);
9455
9456 // touchinputmapper design sends a move before button press
9457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9458 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9460 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9461
9462 // BUTTON UP
9463 processKey(mapper, BTN_LEFT, 0);
9464 processSync(mapper);
9465
9466 // touchinputmapper design sends a move after button release
9467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9468 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9470 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9471
9472 // FINGER 0 UP
9473 processId(mapper, -1);
9474 processSync(mapper);
9475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9476 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9477
9478 // FINGER 1 MOVE
9479 processSlot(mapper, 1);
9480 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9481 processSync(mapper);
9482
9483 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9485 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9486 ASSERT_EQ(1U, args.pointerCount);
9487 ASSERT_EQ(1, args.pointerProperties[0].id);
9488 ASSERT_NO_FATAL_FAILURE(
9489 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9490
9491 // FINGER 1 UP
9492 processId(mapper, -1);
9493 processKey(mapper, BTN_TOUCH, 0);
9494 processSync(mapper);
9495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9496 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9497
9498 // non captured touchpad should be a mouse source
9499 mFakePolicy->setPointerCapture(false);
9500 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9502 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9503}
9504
9505TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9506 std::shared_ptr<FakePointerController> fakePointerController =
9507 std::make_shared<FakePointerController>();
9508 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9509 fakePointerController->setPosition(0, 0);
9510 fakePointerController->setButtonState(0);
9511
9512 // prepare device and capture
9513 prepareDisplay(DISPLAY_ORIENTATION_0);
9514 prepareAxes(POSITION | ID | SLOT);
9515 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9516 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009517 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009518 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9519 // run uncaptured pointer tests - pushes out generic events
9520 // FINGER 0 DOWN
9521 processId(mapper, 3);
9522 processPosition(mapper, 100, 100);
9523 processKey(mapper, BTN_TOUCH, 1);
9524 processSync(mapper);
9525
9526 // start at (100,100), cursor should be at (0,0) * scale
9527 NotifyMotionArgs args;
9528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9529 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9530 ASSERT_NO_FATAL_FAILURE(
9531 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9532
9533 // FINGER 0 MOVE
9534 processPosition(mapper, 200, 200);
9535 processSync(mapper);
9536
9537 // compute scaling to help with touch position checking
9538 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9539 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9540 float scale =
9541 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9542
9543 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9545 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9546 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9547 0, 0, 0, 0, 0, 0, 0));
9548}
9549
9550TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9551 std::shared_ptr<FakePointerController> fakePointerController =
9552 std::make_shared<FakePointerController>();
9553
9554 prepareDisplay(DISPLAY_ORIENTATION_0);
9555 prepareAxes(POSITION | ID | SLOT);
9556 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009557 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009558 mFakePolicy->setPointerCapture(false);
9559 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9560
9561 // uncaptured touchpad should be a pointer device
9562 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9563
9564 // captured touchpad should be a touchpad device
9565 mFakePolicy->setPointerCapture(true);
9566 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9567 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9568}
9569
HQ Liue6983c72022-04-19 22:14:56 +00009570class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9571protected:
9572 float mPointerMovementScale;
9573 float mPointerXZoomScale;
9574 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9575 addConfigurationProperty("touch.deviceType", "pointer");
9576 std::shared_ptr<FakePointerController> fakePointerController =
9577 std::make_shared<FakePointerController>();
9578 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9579 fakePointerController->setPosition(0, 0);
9580 fakePointerController->setButtonState(0);
9581 prepareDisplay(DISPLAY_ORIENTATION_0);
9582
9583 prepareAxes(POSITION);
9584 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9585 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9586 // needs to be disabled, and the pointer gesture needs to be enabled.
9587 mFakePolicy->setPointerCapture(false);
9588 mFakePolicy->setPointerGestureEnabled(true);
9589 mFakePolicy->setPointerController(fakePointerController);
9590
9591 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9592 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9593 mPointerMovementScale =
9594 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9595 mPointerXZoomScale =
9596 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9597 }
9598
9599 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9600 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9601 /*flat*/ 0,
9602 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9603 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9604 /*flat*/ 0,
9605 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9606 }
9607};
9608
9609/**
9610 * Two fingers down on a pointer mode touch pad. The width
9611 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9612 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9613 * be greater than the both value to be freeform gesture, so that after two
9614 * fingers start to move downwards, the gesture should be swipe.
9615 */
9616TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9617 // The min freeform gesture width is 25units/mm x 30mm = 750
9618 // which is greater than fraction of the diagnal length of the touchpad (349).
9619 // Thus, MaxSwipWidth is 750.
9620 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9621 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9622 NotifyMotionArgs motionArgs;
9623
9624 // Two fingers down at once.
9625 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9626 // Pointer's initial position is used the [0,0] coordinate.
9627 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9628
9629 processId(mapper, FIRST_TRACKING_ID);
9630 processPosition(mapper, x1, y1);
9631 processMTSync(mapper);
9632 processId(mapper, SECOND_TRACKING_ID);
9633 processPosition(mapper, x2, y2);
9634 processMTSync(mapper);
9635 processSync(mapper);
9636
9637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9638 ASSERT_EQ(1U, motionArgs.pointerCount);
9639 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9640 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9641 ASSERT_NO_FATAL_FAILURE(
9642 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9643
9644 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9645 // that there should be 1 pointer.
9646 int32_t movingDistance = 200;
9647 y1 += movingDistance;
9648 y2 += movingDistance;
9649
9650 processId(mapper, FIRST_TRACKING_ID);
9651 processPosition(mapper, x1, y1);
9652 processMTSync(mapper);
9653 processId(mapper, SECOND_TRACKING_ID);
9654 processPosition(mapper, x2, y2);
9655 processMTSync(mapper);
9656 processSync(mapper);
9657
9658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9659 ASSERT_EQ(1U, motionArgs.pointerCount);
9660 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9661 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9662 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9663 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9664 0, 0, 0, 0));
9665}
9666
9667/**
9668 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9669 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9670 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9671 * value to be freeform gesture, so that after two fingers start to move downwards,
9672 * the gesture should be swipe.
9673 */
9674TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9675 // The min freeform gesture width is 5units/mm x 30mm = 150
9676 // which is greater than fraction of the diagnal length of the touchpad (349).
9677 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
9678 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
9679 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9680 NotifyMotionArgs motionArgs;
9681
9682 // Two fingers down at once.
9683 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9684 // Pointer's initial position is used the [0,0] coordinate.
9685 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9686
9687 processId(mapper, FIRST_TRACKING_ID);
9688 processPosition(mapper, x1, y1);
9689 processMTSync(mapper);
9690 processId(mapper, SECOND_TRACKING_ID);
9691 processPosition(mapper, x2, y2);
9692 processMTSync(mapper);
9693 processSync(mapper);
9694
9695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9696 ASSERT_EQ(1U, motionArgs.pointerCount);
9697 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9698 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9699 ASSERT_NO_FATAL_FAILURE(
9700 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9701
9702 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9703 // and there should be 1 pointer.
9704 int32_t movingDistance = 200;
9705 y1 += movingDistance;
9706 y2 += movingDistance;
9707
9708 processId(mapper, FIRST_TRACKING_ID);
9709 processPosition(mapper, x1, y1);
9710 processMTSync(mapper);
9711 processId(mapper, SECOND_TRACKING_ID);
9712 processPosition(mapper, x2, y2);
9713 processMTSync(mapper);
9714 processSync(mapper);
9715
9716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9717 ASSERT_EQ(1U, motionArgs.pointerCount);
9718 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9719 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9720 // New coordinate is the scaled relative coordinate from the initial coordinate.
9721 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9722 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9723 0, 0, 0, 0));
9724}
9725
9726/**
9727 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
9728 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
9729 * freeform gestures after two fingers start to move downwards.
9730 */
9731TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
9732 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9733 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9734
9735 NotifyMotionArgs motionArgs;
9736
9737 // Two fingers down at once. Wider than the max swipe width.
9738 // The gesture is expected to be PRESS, then transformed to FREEFORM
9739 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
9740
9741 processId(mapper, FIRST_TRACKING_ID);
9742 processPosition(mapper, x1, y1);
9743 processMTSync(mapper);
9744 processId(mapper, SECOND_TRACKING_ID);
9745 processPosition(mapper, x2, y2);
9746 processMTSync(mapper);
9747 processSync(mapper);
9748
9749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9750 ASSERT_EQ(1U, motionArgs.pointerCount);
9751 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9752 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9753 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
9754 ASSERT_NO_FATAL_FAILURE(
9755 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9756
9757 int32_t movingDistance = 200;
9758
9759 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
9760 // then two down events for two pointers.
9761 y1 += movingDistance;
9762 y2 += movingDistance;
9763
9764 processId(mapper, FIRST_TRACKING_ID);
9765 processPosition(mapper, x1, y1);
9766 processMTSync(mapper);
9767 processId(mapper, SECOND_TRACKING_ID);
9768 processPosition(mapper, x2, y2);
9769 processMTSync(mapper);
9770 processSync(mapper);
9771
9772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9773 // The previous PRESS gesture is cancelled, because it is transformed to freeform
9774 ASSERT_EQ(1U, motionArgs.pointerCount);
9775 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9777 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9778 ASSERT_EQ(1U, motionArgs.pointerCount);
9779 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9781 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9782 ASSERT_EQ(2U, motionArgs.pointerCount);
9783 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
9784 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9785 // Two pointers' scaled relative coordinates from their initial centroid.
9786 // Initial y coordinates are 0 as y1 and y2 have the same value.
9787 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
9788 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
9789 // When pointers move, the new coordinates equal to the initial coordinates plus
9790 // scaled moving distance.
9791 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9792 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9793 0, 0, 0, 0));
9794 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9795 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9796 0, 0, 0, 0));
9797
9798 // Move two fingers down again, expect one MOVE motion event.
9799 y1 += movingDistance;
9800 y2 += movingDistance;
9801
9802 processId(mapper, FIRST_TRACKING_ID);
9803 processPosition(mapper, x1, y1);
9804 processMTSync(mapper);
9805 processId(mapper, SECOND_TRACKING_ID);
9806 processPosition(mapper, x2, y2);
9807 processMTSync(mapper);
9808 processSync(mapper);
9809
9810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9811 ASSERT_EQ(2U, motionArgs.pointerCount);
9812 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9813 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9814 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9815 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9816 0, 0, 0, 0, 0));
9817 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9818 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9819 0, 0, 0, 0, 0));
9820}
9821
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009822// --- JoystickInputMapperTest ---
9823
9824class JoystickInputMapperTest : public InputMapperTest {
9825protected:
9826 static const int32_t RAW_X_MIN;
9827 static const int32_t RAW_X_MAX;
9828 static const int32_t RAW_Y_MIN;
9829 static const int32_t RAW_Y_MAX;
9830
9831 void SetUp() override {
9832 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9833 }
9834 void prepareAxes() {
9835 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9836 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9837 }
9838
9839 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9840 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9841 }
9842
9843 void processSync(JoystickInputMapper& mapper) {
9844 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9845 }
9846
9847 void prepareVirtualDisplay(int32_t orientation) {
9848 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9849 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9850 NO_PORT, ViewportType::VIRTUAL);
9851 }
9852};
9853
9854const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9855const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9856const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9857const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9858
9859TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9860 prepareAxes();
9861 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9862
9863 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9864
9865 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9866
9867 // Send an axis event
9868 processAxis(mapper, ABS_X, 100);
9869 processSync(mapper);
9870
9871 NotifyMotionArgs args;
9872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9873 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9874
9875 // Send another axis event
9876 processAxis(mapper, ABS_Y, 100);
9877 processSync(mapper);
9878
9879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9880 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9881}
9882
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009883// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009884
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009885class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009886protected:
9887 static const char* DEVICE_NAME;
9888 static const char* DEVICE_LOCATION;
9889 static const int32_t DEVICE_ID;
9890 static const int32_t DEVICE_GENERATION;
9891 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009892 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009893 static const int32_t EVENTHUB_ID;
9894
9895 std::shared_ptr<FakeEventHub> mFakeEventHub;
9896 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009897 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009898 std::unique_ptr<InstrumentedInputReader> mReader;
9899 std::shared_ptr<InputDevice> mDevice;
9900
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009901 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009902 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009903 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009904 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009905 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009906 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009907 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9908 }
9909
9910 void SetUp() override { SetUp(DEVICE_CLASSES); }
9911
9912 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009913 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009914 mFakePolicy.clear();
9915 }
9916
9917 void configureDevice(uint32_t changes) {
9918 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9919 mReader->requestRefreshConfiguration(changes);
9920 mReader->loopOnce();
9921 }
9922 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9923 }
9924
9925 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9926 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009927 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009928 InputDeviceIdentifier identifier;
9929 identifier.name = name;
9930 identifier.location = location;
9931 std::shared_ptr<InputDevice> device =
9932 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9933 identifier);
9934 mReader->pushNextDevice(device);
9935 mFakeEventHub->addDevice(eventHubId, name, classes);
9936 mReader->loopOnce();
9937 return device;
9938 }
9939
9940 template <class T, typename... Args>
9941 T& addControllerAndConfigure(Args... args) {
9942 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9943
9944 return controller;
9945 }
9946};
9947
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009948const char* PeripheralControllerTest::DEVICE_NAME = "device";
9949const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9950const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9951const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9952const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009953const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
9954 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009955const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009956
9957// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009958class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009959protected:
9960 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009961 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009962 }
9963};
9964
9965TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009966 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009967
9968 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9969 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9970}
9971
9972TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009973 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009974
9975 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9976 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9977}
9978
9979// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009980class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009981protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009982 void SetUp() override {
9983 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9984 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009985};
9986
Chris Ye85758332021-05-16 23:05:17 -07009987TEST_F(LightControllerTest, MonoLight) {
9988 RawLightInfo infoMono = {.id = 1,
9989 .name = "Mono",
9990 .maxBrightness = 255,
9991 .flags = InputLightClass::BRIGHTNESS,
9992 .path = ""};
9993 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009994
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009995 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009996 InputDeviceInfo info;
9997 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009998 std::vector<InputDeviceLightInfo> lights = info.getLights();
9999 ASSERT_EQ(1U, lights.size());
10000 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010001
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010002 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10003 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010004}
10005
10006TEST_F(LightControllerTest, RGBLight) {
10007 RawLightInfo infoRed = {.id = 1,
10008 .name = "red",
10009 .maxBrightness = 255,
10010 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10011 .path = ""};
10012 RawLightInfo infoGreen = {.id = 2,
10013 .name = "green",
10014 .maxBrightness = 255,
10015 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10016 .path = ""};
10017 RawLightInfo infoBlue = {.id = 3,
10018 .name = "blue",
10019 .maxBrightness = 255,
10020 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10021 .path = ""};
10022 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10023 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10024 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10025
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010026 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010027 InputDeviceInfo info;
10028 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010029 std::vector<InputDeviceLightInfo> lights = info.getLights();
10030 ASSERT_EQ(1U, lights.size());
10031 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010032
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010033 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10034 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010035}
10036
10037TEST_F(LightControllerTest, MultiColorRGBLight) {
10038 RawLightInfo infoColor = {.id = 1,
10039 .name = "red",
10040 .maxBrightness = 255,
10041 .flags = InputLightClass::BRIGHTNESS |
10042 InputLightClass::MULTI_INTENSITY |
10043 InputLightClass::MULTI_INDEX,
10044 .path = ""};
10045
10046 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10047
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010048 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010049 InputDeviceInfo info;
10050 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010051 std::vector<InputDeviceLightInfo> lights = info.getLights();
10052 ASSERT_EQ(1U, lights.size());
10053 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010054
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010055 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10056 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010057}
10058
10059TEST_F(LightControllerTest, PlayerIdLight) {
10060 RawLightInfo info1 = {.id = 1,
10061 .name = "player1",
10062 .maxBrightness = 255,
10063 .flags = InputLightClass::BRIGHTNESS,
10064 .path = ""};
10065 RawLightInfo info2 = {.id = 2,
10066 .name = "player2",
10067 .maxBrightness = 255,
10068 .flags = InputLightClass::BRIGHTNESS,
10069 .path = ""};
10070 RawLightInfo info3 = {.id = 3,
10071 .name = "player3",
10072 .maxBrightness = 255,
10073 .flags = InputLightClass::BRIGHTNESS,
10074 .path = ""};
10075 RawLightInfo info4 = {.id = 4,
10076 .name = "player4",
10077 .maxBrightness = 255,
10078 .flags = InputLightClass::BRIGHTNESS,
10079 .path = ""};
10080 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10081 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10082 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10083 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10084
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010085 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010086 InputDeviceInfo info;
10087 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010088 std::vector<InputDeviceLightInfo> lights = info.getLights();
10089 ASSERT_EQ(1U, lights.size());
10090 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010091
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010092 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10093 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10094 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010095}
10096
Michael Wrightd02c5b62014-02-10 15:10:22 -080010097} // namespace android