blob: 851043d8908d2b68e3aab6abe73f708cd695e3cb [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
Andy Chenf9f1a022022-08-29 20:07:10 -04001011 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override {
1012 return {DEFAULT_BATTERY};
1013 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001014
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001015 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
1016 int32_t batteryId) const override {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001017 return std::nullopt;
1018 }
Kim Low03ea0352020-11-06 12:45:07 -08001019
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001020 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001021 std::vector<int32_t> ids;
1022 for (const auto& [rawId, info] : mRawLightInfos) {
1023 ids.push_back(rawId);
1024 }
1025 return ids;
1026 }
1027
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001028 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001029 auto it = mRawLightInfos.find(lightId);
1030 if (it == mRawLightInfos.end()) {
1031 return std::nullopt;
1032 }
1033 return it->second;
1034 }
1035
1036 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1037 mLightBrightness.emplace(lightId, brightness);
1038 }
1039
1040 void setLightIntensities(int32_t deviceId, int32_t lightId,
1041 std::unordered_map<LightColor, int32_t> intensities) override {
1042 mLightIntensities.emplace(lightId, intensities);
1043 };
1044
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001045 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001046 auto lightIt = mLightBrightness.find(lightId);
1047 if (lightIt == mLightBrightness.end()) {
1048 return std::nullopt;
1049 }
1050 return lightIt->second;
1051 }
1052
1053 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001054 int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001055 auto lightIt = mLightIntensities.find(lightId);
1056 if (lightIt == mLightIntensities.end()) {
1057 return std::nullopt;
1058 }
1059 return lightIt->second;
1060 };
1061
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001062 void dump(std::string&) const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001063
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001064 void monitor() const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001065
Chris Yea52ade12020-08-27 16:49:20 -07001066 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001067
Chris Yea52ade12020-08-27 16:49:20 -07001068 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001069};
1070
Michael Wrightd02c5b62014-02-10 15:10:22 -08001071// --- FakeInputMapper ---
1072
1073class FakeInputMapper : public InputMapper {
1074 uint32_t mSources;
1075 int32_t mKeyboardType;
1076 int32_t mMetaState;
1077 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1078 KeyedVector<int32_t, int32_t> mScanCodeStates;
1079 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001080 // fake mapping which would normally come from keyCharacterMap
1081 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001082 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001083
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001084 std::mutex mLock;
1085 std::condition_variable mStateChangedCondition;
1086 bool mConfigureWasCalled GUARDED_BY(mLock);
1087 bool mResetWasCalled GUARDED_BY(mLock);
1088 bool mProcessWasCalled GUARDED_BY(mLock);
1089 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001090
Arthur Hungc23540e2018-11-29 20:42:11 +08001091 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001092public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001093 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1094 : InputMapper(deviceContext),
1095 mSources(sources),
1096 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001097 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001098 mConfigureWasCalled(false),
1099 mResetWasCalled(false),
1100 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001101
Chris Yea52ade12020-08-27 16:49:20 -07001102 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001103
1104 void setKeyboardType(int32_t keyboardType) {
1105 mKeyboardType = keyboardType;
1106 }
1107
1108 void setMetaState(int32_t metaState) {
1109 mMetaState = metaState;
1110 }
1111
1112 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001113 std::unique_lock<std::mutex> lock(mLock);
1114 base::ScopedLockAssertion assumeLocked(mLock);
1115 const bool configureCalled =
1116 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1117 return mConfigureWasCalled;
1118 });
1119 if (!configureCalled) {
1120 FAIL() << "Expected configure() to have been called.";
1121 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001122 mConfigureWasCalled = false;
1123 }
1124
1125 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001126 std::unique_lock<std::mutex> lock(mLock);
1127 base::ScopedLockAssertion assumeLocked(mLock);
1128 const bool resetCalled =
1129 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1130 return mResetWasCalled;
1131 });
1132 if (!resetCalled) {
1133 FAIL() << "Expected reset() to have been called.";
1134 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001135 mResetWasCalled = false;
1136 }
1137
Yi Kong9b14ac62018-07-17 13:48:38 -07001138 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001139 std::unique_lock<std::mutex> lock(mLock);
1140 base::ScopedLockAssertion assumeLocked(mLock);
1141 const bool processCalled =
1142 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1143 return mProcessWasCalled;
1144 });
1145 if (!processCalled) {
1146 FAIL() << "Expected process() to have been called.";
1147 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001148 if (outLastEvent) {
1149 *outLastEvent = mLastEvent;
1150 }
1151 mProcessWasCalled = false;
1152 }
1153
1154 void setKeyCodeState(int32_t keyCode, int32_t state) {
1155 mKeyCodeStates.replaceValueFor(keyCode, state);
1156 }
1157
1158 void setScanCodeState(int32_t scanCode, int32_t state) {
1159 mScanCodeStates.replaceValueFor(scanCode, state);
1160 }
1161
1162 void setSwitchState(int32_t switchCode, int32_t state) {
1163 mSwitchStates.replaceValueFor(switchCode, state);
1164 }
1165
1166 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001167 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001168 }
1169
Philip Junker4af3b3d2021-12-14 10:36:55 +01001170 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1171 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1172 }
1173
Michael Wrightd02c5b62014-02-10 15:10:22 -08001174private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001175 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001176
Chris Yea52ade12020-08-27 16:49:20 -07001177 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001178 InputMapper::populateDeviceInfo(deviceInfo);
1179
1180 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1181 deviceInfo->setKeyboardType(mKeyboardType);
1182 }
1183 }
1184
Chris Yea52ade12020-08-27 16:49:20 -07001185 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001186 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001187 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001188
1189 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001190 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001191 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1192 mViewport = config->getDisplayViewportByPort(*displayPort);
1193 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001194
1195 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001196 }
1197
Chris Yea52ade12020-08-27 16:49:20 -07001198 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001199 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001200 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001201 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001202 }
1203
Chris Yea52ade12020-08-27 16:49:20 -07001204 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001205 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001206 mLastEvent = *rawEvent;
1207 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001208 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001209 }
1210
Chris Yea52ade12020-08-27 16:49:20 -07001211 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001212 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1213 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1214 }
1215
Philip Junker4af3b3d2021-12-14 10:36:55 +01001216 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1217 auto it = mKeyCodeMapping.find(locationKeyCode);
1218 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1219 }
1220
Chris Yea52ade12020-08-27 16:49:20 -07001221 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001222 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1223 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1224 }
1225
Chris Yea52ade12020-08-27 16:49:20 -07001226 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001227 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1228 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1229 }
1230
Chris Yea52ade12020-08-27 16:49:20 -07001231 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001232 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -07001233 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001234 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001235 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1236 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1237 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001238 }
1239 }
1240 }
Chris Yea52ade12020-08-27 16:49:20 -07001241 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001242 return result;
1243 }
1244
1245 virtual int32_t getMetaState() {
1246 return mMetaState;
1247 }
1248
1249 virtual void fadePointer() {
1250 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001251
1252 virtual std::optional<int32_t> getAssociatedDisplay() {
1253 if (mViewport) {
1254 return std::make_optional(mViewport->displayId);
1255 }
1256 return std::nullopt;
1257 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001258};
1259
1260
1261// --- InstrumentedInputReader ---
1262
1263class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001264 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001265
1266public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001267 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1268 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001269 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001270 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001271
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001272 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001273
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001274 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001275
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001276 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001277 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001278 InputDeviceIdentifier identifier;
1279 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001280 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001281 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001282 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001283 }
1284
Prabir Pradhan28efc192019-11-05 01:10:04 +00001285 // Make the protected loopOnce method accessible to tests.
1286 using InputReader::loopOnce;
1287
Michael Wrightd02c5b62014-02-10 15:10:22 -08001288protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001289 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1290 const InputDeviceIdentifier& identifier)
1291 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001292 if (!mNextDevices.empty()) {
1293 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1294 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001295 return device;
1296 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001297 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001298 }
1299
arthurhungdcef2dc2020-08-11 14:47:50 +08001300 // --- FakeInputReaderContext ---
1301 class FakeInputReaderContext : public ContextImpl {
1302 int32_t mGlobalMetaState;
1303 bool mUpdateGlobalMetaStateWasCalled;
1304 int32_t mGeneration;
1305
1306 public:
1307 FakeInputReaderContext(InputReader* reader)
1308 : ContextImpl(reader),
1309 mGlobalMetaState(0),
1310 mUpdateGlobalMetaStateWasCalled(false),
1311 mGeneration(1) {}
1312
1313 virtual ~FakeInputReaderContext() {}
1314
1315 void assertUpdateGlobalMetaStateWasCalled() {
1316 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1317 << "Expected updateGlobalMetaState() to have been called.";
1318 mUpdateGlobalMetaStateWasCalled = false;
1319 }
1320
1321 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1322
1323 uint32_t getGeneration() { return mGeneration; }
1324
1325 void updateGlobalMetaState() override {
1326 mUpdateGlobalMetaStateWasCalled = true;
1327 ContextImpl::updateGlobalMetaState();
1328 }
1329
1330 int32_t getGlobalMetaState() override {
1331 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1332 }
1333
1334 int32_t bumpGeneration() override {
1335 mGeneration = ContextImpl::bumpGeneration();
1336 return mGeneration;
1337 }
1338 } mFakeContext;
1339
Michael Wrightd02c5b62014-02-10 15:10:22 -08001340 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001341
1342public:
1343 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001344};
1345
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001346// --- InputReaderPolicyTest ---
1347class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001348protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001349 sp<FakeInputReaderPolicy> mFakePolicy;
1350
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001351 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -07001352 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001353};
1354
1355/**
1356 * Check that empty set of viewports is an acceptable configuration.
1357 * Also try to get internal viewport two different ways - by type and by uniqueId.
1358 *
1359 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1360 * Such configuration is not currently allowed.
1361 */
1362TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001363 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001364
1365 // We didn't add any viewports yet, so there shouldn't be any.
1366 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001367 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001368 ASSERT_FALSE(internalViewport);
1369
1370 // Add an internal viewport, then clear it
1371 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001372 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001373 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001374
1375 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001376 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001377 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001378 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001379
1380 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001381 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001382 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001383 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001384
1385 mFakePolicy->clearViewports();
1386 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001387 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001388 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001389 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001390 ASSERT_FALSE(internalViewport);
1391}
1392
1393TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1394 const std::string internalUniqueId = "local:0";
1395 const std::string externalUniqueId = "local:1";
1396 const std::string virtualUniqueId1 = "virtual:2";
1397 const std::string virtualUniqueId2 = "virtual:3";
1398 constexpr int32_t virtualDisplayId1 = 2;
1399 constexpr int32_t virtualDisplayId2 = 3;
1400
1401 // Add an internal viewport
1402 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001403 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1404 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001405 // Add an external viewport
1406 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001407 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1408 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001409 // Add an virtual viewport
1410 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001411 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1412 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001413 // Add another virtual viewport
1414 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001415 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1416 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001417
1418 // Check matching by type for internal
1419 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001420 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001421 ASSERT_TRUE(internalViewport);
1422 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1423
1424 // Check matching by type for external
1425 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001426 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001427 ASSERT_TRUE(externalViewport);
1428 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1429
1430 // Check matching by uniqueId for virtual viewport #1
1431 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001432 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001433 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001434 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001435 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1436 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1437
1438 // Check matching by uniqueId for virtual viewport #2
1439 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001440 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001441 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001442 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001443 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1444 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1445}
1446
1447
1448/**
1449 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1450 * that lookup works by checking display id.
1451 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1452 */
1453TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1454 const std::string uniqueId1 = "uniqueId1";
1455 const std::string uniqueId2 = "uniqueId2";
1456 constexpr int32_t displayId1 = 2;
1457 constexpr int32_t displayId2 = 3;
1458
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001459 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1460 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001461 for (const ViewportType& type : types) {
1462 mFakePolicy->clearViewports();
1463 // Add a viewport
1464 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001465 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1466 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001467 // Add another viewport
1468 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001469 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1470 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001471
1472 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001473 std::optional<DisplayViewport> viewport1 =
1474 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001475 ASSERT_TRUE(viewport1);
1476 ASSERT_EQ(displayId1, viewport1->displayId);
1477 ASSERT_EQ(type, viewport1->type);
1478
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001479 std::optional<DisplayViewport> viewport2 =
1480 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001481 ASSERT_TRUE(viewport2);
1482 ASSERT_EQ(displayId2, viewport2->displayId);
1483 ASSERT_EQ(type, viewport2->type);
1484
1485 // When there are multiple viewports of the same kind, and uniqueId is not specified
1486 // in the call to getDisplayViewport, then that situation is not supported.
1487 // The viewports can be stored in any order, so we cannot rely on the order, since that
1488 // is just implementation detail.
1489 // However, we can check that it still returns *a* viewport, we just cannot assert
1490 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001491 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001492 ASSERT_TRUE(someViewport);
1493 }
1494}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001495
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001496/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001497 * When we have multiple internal displays make sure we always return the default display when
1498 * querying by type.
1499 */
1500TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1501 const std::string uniqueId1 = "uniqueId1";
1502 const std::string uniqueId2 = "uniqueId2";
1503 constexpr int32_t nonDefaultDisplayId = 2;
1504 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1505 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1506
1507 // Add the default display first and ensure it gets returned.
1508 mFakePolicy->clearViewports();
1509 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001510 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001511 ViewportType::INTERNAL);
1512 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001513 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001514 ViewportType::INTERNAL);
1515
1516 std::optional<DisplayViewport> viewport =
1517 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1518 ASSERT_TRUE(viewport);
1519 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1520 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1521
1522 // Add the default display second to make sure order doesn't matter.
1523 mFakePolicy->clearViewports();
1524 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001525 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001526 ViewportType::INTERNAL);
1527 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001528 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001529 ViewportType::INTERNAL);
1530
1531 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1532 ASSERT_TRUE(viewport);
1533 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1534 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1535}
1536
1537/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001538 * Check getDisplayViewportByPort
1539 */
1540TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001541 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001542 const std::string uniqueId1 = "uniqueId1";
1543 const std::string uniqueId2 = "uniqueId2";
1544 constexpr int32_t displayId1 = 1;
1545 constexpr int32_t displayId2 = 2;
1546 const uint8_t hdmi1 = 0;
1547 const uint8_t hdmi2 = 1;
1548 const uint8_t hdmi3 = 2;
1549
1550 mFakePolicy->clearViewports();
1551 // Add a viewport that's associated with some display port that's not of interest.
1552 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001553 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1554 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001555 // Add another viewport, connected to HDMI1 port
1556 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001557 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1558 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001559
1560 // Check that correct display viewport was returned by comparing the display ports.
1561 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1562 ASSERT_TRUE(hdmi1Viewport);
1563 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1564 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1565
1566 // Check that we can still get the same viewport using the uniqueId
1567 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1568 ASSERT_TRUE(hdmi1Viewport);
1569 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1570 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1571 ASSERT_EQ(type, hdmi1Viewport->type);
1572
1573 // Check that we cannot find a port with "HDMI2", because we never added one
1574 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1575 ASSERT_FALSE(hdmi2Viewport);
1576}
1577
Michael Wrightd02c5b62014-02-10 15:10:22 -08001578// --- InputReaderTest ---
1579
1580class InputReaderTest : public testing::Test {
1581protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001582 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001583 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001584 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001585 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001586
Chris Yea52ade12020-08-27 16:49:20 -07001587 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001588 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001589 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001590 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001591
Prabir Pradhan28efc192019-11-05 01:10:04 +00001592 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001593 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001594 }
1595
Chris Yea52ade12020-08-27 16:49:20 -07001596 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001597 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001598 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001599 }
1600
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001601 void addDevice(int32_t eventHubId, const std::string& name,
1602 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001603 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001604
1605 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001606 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001607 }
1608 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001609 mReader->loopOnce();
1610 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001611 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1612 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001613 }
1614
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001615 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001616 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001617 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001618 }
1619
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001620 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001621 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001622 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001623 }
1624
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001625 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001626 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001627 ftl::Flags<InputDeviceClass> classes,
1628 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001629 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001630 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1631 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001632 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001633 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001634 return mapper;
1635 }
1636};
1637
Chris Ye98d3f532020-10-01 21:48:59 -07001638TEST_F(InputReaderTest, PolicyGetInputDevices) {
1639 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001640 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001641 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001642
1643 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001644 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001645 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001646 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001647 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001648 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1649 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001650 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001651}
1652
Chris Yee7310032020-09-22 15:36:28 -07001653TEST_F(InputReaderTest, GetMergedInputDevices) {
1654 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1655 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1656 // Add two subdevices to device
1657 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1658 // Must add at least one mapper or the device will be ignored!
1659 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1660 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1661
1662 // Push same device instance for next device to be added, so they'll have same identifier.
1663 mReader->pushNextDevice(device);
1664 mReader->pushNextDevice(device);
1665 ASSERT_NO_FATAL_FAILURE(
1666 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1667 ASSERT_NO_FATAL_FAILURE(
1668 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1669
1670 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001671 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001672}
1673
Chris Yee14523a2020-12-19 13:46:00 -08001674TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1675 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1676 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1677 // Add two subdevices to device
1678 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1679 // Must add at least one mapper or the device will be ignored!
1680 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1681 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1682
1683 // Push same device instance for next device to be added, so they'll have same identifier.
1684 mReader->pushNextDevice(device);
1685 mReader->pushNextDevice(device);
1686 // Sensor device is initially disabled
1687 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1688 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1689 nullptr));
1690 // Device is disabled because the only sub device is a sensor device and disabled initially.
1691 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1692 ASSERT_FALSE(device->isEnabled());
1693 ASSERT_NO_FATAL_FAILURE(
1694 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1695 // The merged device is enabled if any sub device is enabled
1696 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1697 ASSERT_TRUE(device->isEnabled());
1698}
1699
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001700TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001701 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001702 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001703 constexpr int32_t eventHubId = 1;
1704 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001705 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001706 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001707 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001708 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001709
Yi Kong9b14ac62018-07-17 13:48:38 -07001710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001711
1712 NotifyDeviceResetArgs resetArgs;
1713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001714 ASSERT_EQ(deviceId, resetArgs.deviceId);
1715
1716 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001717 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001718 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001719
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001721 ASSERT_EQ(deviceId, resetArgs.deviceId);
1722 ASSERT_EQ(device->isEnabled(), false);
1723
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001724 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001725 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001728 ASSERT_EQ(device->isEnabled(), false);
1729
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001730 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001731 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001733 ASSERT_EQ(deviceId, resetArgs.deviceId);
1734 ASSERT_EQ(device->isEnabled(), true);
1735}
1736
Michael Wrightd02c5b62014-02-10 15:10:22 -08001737TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001738 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001739 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001740 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001741 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001742 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001743 AINPUT_SOURCE_KEYBOARD, nullptr);
1744 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001745
1746 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1747 AINPUT_SOURCE_ANY, AKEYCODE_A))
1748 << "Should return unknown when the device id is >= 0 but unknown.";
1749
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001750 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1751 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1752 << "Should return unknown when the device id is valid but the sources are not "
1753 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001754
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001755 ASSERT_EQ(AKEY_STATE_DOWN,
1756 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1757 AKEYCODE_A))
1758 << "Should return value provided by mapper when device id is valid and the device "
1759 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001760
1761 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1762 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1763 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1764
1765 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1766 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1767 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1768}
1769
Philip Junker4af3b3d2021-12-14 10:36:55 +01001770TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1771 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1772 constexpr int32_t eventHubId = 1;
1773 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1774 InputDeviceClass::KEYBOARD,
1775 AINPUT_SOURCE_KEYBOARD, nullptr);
1776 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1777
1778 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1779 << "Should return unknown when the device with the specified id is not found.";
1780
1781 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1782 << "Should return correct mapping when device id is valid and mapping exists.";
1783
1784 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1785 << "Should return the location key code when device id is valid and there's no "
1786 "mapping.";
1787}
1788
1789TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1790 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1791 constexpr int32_t eventHubId = 1;
1792 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1793 InputDeviceClass::JOYSTICK,
1794 AINPUT_SOURCE_GAMEPAD, nullptr);
1795 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1796
1797 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1798 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1799}
1800
Michael Wrightd02c5b62014-02-10 15:10:22 -08001801TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001802 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001803 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001804 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001805 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001806 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001807 AINPUT_SOURCE_KEYBOARD, nullptr);
1808 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001809
1810 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1811 AINPUT_SOURCE_ANY, KEY_A))
1812 << "Should return unknown when the device id is >= 0 but unknown.";
1813
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001814 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1815 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1816 << "Should return unknown when the device id is valid but the sources are not "
1817 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001818
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001819 ASSERT_EQ(AKEY_STATE_DOWN,
1820 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1821 KEY_A))
1822 << "Should return value provided by mapper when device id is valid and the device "
1823 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001824
1825 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1826 AINPUT_SOURCE_TRACKBALL, KEY_A))
1827 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1828
1829 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1830 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1831 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1832}
1833
1834TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001835 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001836 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001837 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001838 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001839 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001840 AINPUT_SOURCE_KEYBOARD, nullptr);
1841 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001842
1843 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1844 AINPUT_SOURCE_ANY, SW_LID))
1845 << "Should return unknown when the device id is >= 0 but unknown.";
1846
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001847 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1848 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1849 << "Should return unknown when the device id is valid but the sources are not "
1850 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001851
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001852 ASSERT_EQ(AKEY_STATE_DOWN,
1853 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1854 SW_LID))
1855 << "Should return value provided by mapper when device id is valid and the device "
1856 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001857
1858 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1859 AINPUT_SOURCE_TRACKBALL, SW_LID))
1860 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1861
1862 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1863 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1864 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1865}
1866
1867TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001868 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001869 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001870 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001871 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001872 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001873 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001874
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001875 mapper.addSupportedKeyCode(AKEYCODE_A);
1876 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001877
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001878 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001879 uint8_t flags[4] = { 0, 0, 0, 1 };
1880
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001881 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08001882 << "Should return false when device id is >= 0 but unknown.";
1883 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1884
1885 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001886 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001887 << "Should return false when device id is valid but the sources are not supported by "
1888 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001889 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1890
1891 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001892 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001893 keyCodes, flags))
1894 << "Should return value provided by mapper when device id is valid and the device "
1895 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001896 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1897
1898 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001899 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1900 << "Should return false when the device id is < 0 but the sources are not supported by "
1901 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001902 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1903
1904 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001905 ASSERT_TRUE(
1906 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1907 << "Should return value provided by mapper when device id is < 0 and one of the "
1908 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001909 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1910}
1911
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001912TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001913 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001914 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001915
1916 NotifyConfigurationChangedArgs args;
1917
1918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1919 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1920}
1921
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001922TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001923 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001924 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001925 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001926 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001927 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001928 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001929 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001930 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001931
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001932 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001933 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001934 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1935
1936 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001937 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001938 ASSERT_EQ(when, event.when);
1939 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001940 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001941 ASSERT_EQ(EV_KEY, event.type);
1942 ASSERT_EQ(KEY_A, event.code);
1943 ASSERT_EQ(1, event.value);
1944}
1945
Garfield Tan1c7bc862020-01-28 13:24:04 -08001946TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001947 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001948 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001949 constexpr int32_t eventHubId = 1;
1950 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001951 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001952 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001953 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001954 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001955
1956 NotifyDeviceResetArgs resetArgs;
1957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001958 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001959
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001960 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001961 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001963 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001964 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001965
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001966 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001967 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001969 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001970 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001971
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001972 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001973 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001975 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001976 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001977}
1978
Garfield Tan1c7bc862020-01-28 13:24:04 -08001979TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1980 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001981 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001982 constexpr int32_t eventHubId = 1;
1983 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1984 // Must add at least one mapper or the device will be ignored!
1985 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001986 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001987 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1988
1989 NotifyDeviceResetArgs resetArgs;
1990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1991 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1992}
1993
Arthur Hungc23540e2018-11-29 20:42:11 +08001994TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001995 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001996 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001997 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001998 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001999 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2000 FakeInputMapper& mapper =
2001 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002002 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08002003
2004 const uint8_t hdmi1 = 1;
2005
2006 // Associated touch screen with second display.
2007 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2008
2009 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00002010 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08002011 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002012 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002013 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002014 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002015 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002016 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002017 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002018 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002019
2020 // Add the device, and make sure all of the callbacks are triggered.
2021 // The device is added after the input port associations are processed since
2022 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002023 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002026 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002027
Arthur Hung2c9a3342019-07-23 14:18:59 +08002028 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002029 ASSERT_EQ(deviceId, device->getId());
2030 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2031 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002032
2033 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002034 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002035 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002036 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002037}
2038
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002039TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2040 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002041 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002042 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2043 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2044 // Must add at least one mapper or the device will be ignored!
2045 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2046 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2047 mReader->pushNextDevice(device);
2048 mReader->pushNextDevice(device);
2049 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2050 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2051
2052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2053
2054 NotifyDeviceResetArgs resetArgs;
2055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2056 ASSERT_EQ(deviceId, resetArgs.deviceId);
2057 ASSERT_TRUE(device->isEnabled());
2058 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2059 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2060
2061 disableDevice(deviceId);
2062 mReader->loopOnce();
2063
2064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2065 ASSERT_EQ(deviceId, resetArgs.deviceId);
2066 ASSERT_FALSE(device->isEnabled());
2067 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2068 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2069
2070 enableDevice(deviceId);
2071 mReader->loopOnce();
2072
2073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2074 ASSERT_EQ(deviceId, resetArgs.deviceId);
2075 ASSERT_TRUE(device->isEnabled());
2076 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2077 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2078}
2079
2080TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2081 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002082 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002083 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2084 // Add two subdevices to device
2085 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2086 FakeInputMapper& mapperDevice1 =
2087 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2088 FakeInputMapper& mapperDevice2 =
2089 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2090 mReader->pushNextDevice(device);
2091 mReader->pushNextDevice(device);
2092 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2093 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2094
2095 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2096 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2097
2098 ASSERT_EQ(AKEY_STATE_DOWN,
2099 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2100 ASSERT_EQ(AKEY_STATE_DOWN,
2101 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2102 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2103 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2104}
2105
Prabir Pradhan7e186182020-11-10 13:56:45 -08002106TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2107 NotifyPointerCaptureChangedArgs args;
2108
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002109 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002110 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2111 mReader->loopOnce();
2112 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002113 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2114 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002115
2116 mFakePolicy->setPointerCapture(false);
2117 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2118 mReader->loopOnce();
2119 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002120 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002121
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002122 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002123 // does not change.
2124 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2125 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002126 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002127}
2128
Chris Ye87143712020-11-10 05:05:58 +00002129class FakeVibratorInputMapper : public FakeInputMapper {
2130public:
2131 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2132 : FakeInputMapper(deviceContext, sources) {}
2133
2134 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2135};
2136
2137TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2138 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002139 ftl::Flags<InputDeviceClass> deviceClass =
2140 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002141 constexpr int32_t eventHubId = 1;
2142 const char* DEVICE_LOCATION = "BLUETOOTH";
2143 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2144 FakeVibratorInputMapper& mapper =
2145 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2146 mReader->pushNextDevice(device);
2147
2148 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2149 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2150
2151 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2152 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2153}
2154
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002155// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002156
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002157class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002158public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002159 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002160
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002161 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002162
Andy Chenf9f1a022022-08-29 20:07:10 -04002163 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
2164
Chris Yee2b1e5c2021-03-10 22:45:12 -08002165 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2166
2167 void dump(std::string& dump) override {}
2168
2169 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2170 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002171 }
2172
Chris Yee2b1e5c2021-03-10 22:45:12 -08002173 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2174 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002175 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002176
2177 bool setLightColor(int32_t lightId, int32_t color) override {
2178 getDeviceContext().setLightBrightness(lightId, color >> 24);
2179 return true;
2180 }
2181
2182 std::optional<int32_t> getLightColor(int32_t lightId) override {
2183 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2184 if (!result.has_value()) {
2185 return std::nullopt;
2186 }
2187 return result.value() << 24;
2188 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002189
2190 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2191
2192 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2193
2194private:
2195 InputDeviceContext& mDeviceContext;
2196 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2197 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04002198 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002199};
2200
Chris Yee2b1e5c2021-03-10 22:45:12 -08002201TEST_F(InputReaderTest, BatteryGetCapacity) {
2202 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002203 ftl::Flags<InputDeviceClass> deviceClass =
2204 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002205 constexpr int32_t eventHubId = 1;
2206 const char* DEVICE_LOCATION = "BLUETOOTH";
2207 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002208 FakePeripheralController& controller =
2209 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002210 mReader->pushNextDevice(device);
2211
2212 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2213
2214 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2215 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2216}
2217
2218TEST_F(InputReaderTest, BatteryGetStatus) {
2219 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002220 ftl::Flags<InputDeviceClass> deviceClass =
2221 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002222 constexpr int32_t eventHubId = 1;
2223 const char* DEVICE_LOCATION = "BLUETOOTH";
2224 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002225 FakePeripheralController& controller =
2226 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002227 mReader->pushNextDevice(device);
2228
2229 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2230
2231 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2232 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2233}
2234
Chris Ye3fdbfef2021-01-06 18:45:18 -08002235TEST_F(InputReaderTest, LightGetColor) {
2236 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002237 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002238 constexpr int32_t eventHubId = 1;
2239 const char* DEVICE_LOCATION = "BLUETOOTH";
2240 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002241 FakePeripheralController& controller =
2242 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002243 mReader->pushNextDevice(device);
2244 RawLightInfo info = {.id = 1,
2245 .name = "Mono",
2246 .maxBrightness = 255,
2247 .flags = InputLightClass::BRIGHTNESS,
2248 .path = ""};
2249 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2250 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2251
2252 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002253
Chris Yee2b1e5c2021-03-10 22:45:12 -08002254 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2255 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002256 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2257 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2258}
2259
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002260// --- InputReaderIntegrationTest ---
2261
2262// These tests create and interact with the InputReader only through its interface.
2263// The InputReader is started during SetUp(), which starts its processing in its own
2264// thread. The tests use linux uinput to emulate input devices.
2265// NOTE: Interacting with the physical device while these tests are running may cause
2266// the tests to fail.
2267class InputReaderIntegrationTest : public testing::Test {
2268protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002269 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002270 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002271 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002272
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002273 std::shared_ptr<FakePointerController> mFakePointerController;
2274
Chris Yea52ade12020-08-27 16:49:20 -07002275 void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002276 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002277 mFakePointerController = std::make_shared<FakePointerController>();
2278 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002279 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2280 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002281
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002282 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2283 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002284 ASSERT_EQ(mReader->start(), OK);
2285
2286 // Since this test is run on a real device, all the input devices connected
2287 // to the test device will show up in mReader. We wait for those input devices to
2288 // show up before beginning the tests.
2289 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2290 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2291 }
2292
Chris Yea52ade12020-08-27 16:49:20 -07002293 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002294 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002295 mReader.reset();
2296 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002297 mFakePolicy.clear();
2298 }
2299};
2300
2301TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2302 // An invalid input device that is only used for this test.
2303 class InvalidUinputDevice : public UinputDevice {
2304 public:
2305 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2306
2307 private:
2308 void configureDevice(int fd, uinput_user_dev* device) override {}
2309 };
2310
2311 const size_t numDevices = mFakePolicy->getInputDevices().size();
2312
2313 // UinputDevice does not set any event or key bits, so InputReader should not
2314 // consider it as a valid device.
2315 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2316 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2317 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2318 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2319
2320 invalidDevice.reset();
2321 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2322 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2323 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2324}
2325
2326TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2327 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2328
2329 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2330 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2331 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2332 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2333
2334 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002335 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002336 const auto& it =
2337 std::find_if(inputDevices.begin(), inputDevices.end(),
2338 [&keyboard](const InputDeviceInfo& info) {
2339 return info.getIdentifier().name == keyboard->getName();
2340 });
2341
2342 ASSERT_NE(it, inputDevices.end());
2343 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2344 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2345 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002346
2347 keyboard.reset();
2348 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2349 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2350 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2351}
2352
2353TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2354 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2355 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2356
2357 NotifyConfigurationChangedArgs configChangedArgs;
2358 ASSERT_NO_FATAL_FAILURE(
2359 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002360 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002361 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2362
2363 NotifyKeyArgs keyArgs;
2364 keyboard->pressAndReleaseHomeKey();
2365 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2366 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002367 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002368 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002369 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002370 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002371 prevTimestamp = keyArgs.eventTime;
2372
2373 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2374 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002375 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002376 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002377 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002378}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002379
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002380/**
2381 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2382 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2383 * are passed to the listener.
2384 */
2385static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2386TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2387 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2388 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2389 NotifyKeyArgs keyArgs;
2390
2391 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
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_DOWN, keyArgs.scanCode);
2395
2396 controller->pressAndReleaseKey(BTN_GEAR_UP);
2397 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2398 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2399 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2400}
2401
Arthur Hungaab25622020-01-16 11:22:11 +08002402// --- TouchProcessTest ---
2403class TouchIntegrationTest : public InputReaderIntegrationTest {
2404protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002405 const std::string UNIQUE_ID = "local:0";
2406
Chris Yea52ade12020-08-27 16:49:20 -07002407 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002408 InputReaderIntegrationTest::SetUp();
2409 // At least add an internal display.
2410 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2411 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002412 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002413
2414 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2415 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2416 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2417 }
2418
2419 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2420 int32_t orientation, const std::string& uniqueId,
2421 std::optional<uint8_t> physicalPort,
2422 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002423 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2424 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002425 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2426 }
2427
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002428 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2429 NotifyMotionArgs args;
2430 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2431 EXPECT_EQ(action, args.action);
2432 ASSERT_EQ(points.size(), args.pointerCount);
2433 for (size_t i = 0; i < args.pointerCount; i++) {
2434 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2435 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2436 }
2437 }
2438
Arthur Hungaab25622020-01-16 11:22:11 +08002439 std::unique_ptr<UinputTouchScreen> mDevice;
2440};
2441
2442TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2443 NotifyMotionArgs args;
2444 const Point centerPoint = mDevice->getCenterPoint();
2445
2446 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002447 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002448 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002449 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002450 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2451 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2452
2453 // ACTION_MOVE
2454 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002455 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002456 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2457 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2458
2459 // ACTION_UP
2460 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002461 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002462 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2463 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2464}
2465
2466TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2467 NotifyMotionArgs args;
2468 const Point centerPoint = mDevice->getCenterPoint();
2469
2470 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002471 mDevice->sendSlot(FIRST_SLOT);
2472 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002473 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002474 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002475 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2476 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2477
2478 // ACTION_POINTER_DOWN (Second slot)
2479 const Point secondPoint = centerPoint + Point(100, 100);
2480 mDevice->sendSlot(SECOND_SLOT);
2481 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002482 mDevice->sendDown(secondPoint);
2483 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002484 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002485 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002486
2487 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002488 mDevice->sendMove(secondPoint + Point(1, 1));
2489 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002490 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2491 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2492
2493 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002494 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002495 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002496 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002497 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002498
2499 // ACTION_UP
2500 mDevice->sendSlot(FIRST_SLOT);
2501 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002502 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002503 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2504 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2505}
2506
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002507/**
2508 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2509 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2510 * data?
2511 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2512 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2513 * for Pointer 0 only is generated after.
2514 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2515 * events, we will not miss any information.
2516 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2517 * event generated afterwards that contains the newest movement of pointer 0.
2518 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2519 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2520 * losing information about non-palm pointers.
2521 */
2522TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2523 NotifyMotionArgs args;
2524 const Point centerPoint = mDevice->getCenterPoint();
2525
2526 // ACTION_DOWN
2527 mDevice->sendSlot(FIRST_SLOT);
2528 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2529 mDevice->sendDown(centerPoint);
2530 mDevice->sendSync();
2531 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2532
2533 // ACTION_POINTER_DOWN (Second slot)
2534 const Point secondPoint = centerPoint + Point(100, 100);
2535 mDevice->sendSlot(SECOND_SLOT);
2536 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2537 mDevice->sendDown(secondPoint);
2538 mDevice->sendSync();
2539 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2540
2541 // ACTION_MOVE (First slot)
2542 mDevice->sendSlot(FIRST_SLOT);
2543 mDevice->sendMove(centerPoint + Point(5, 5));
2544 // ACTION_POINTER_UP (Second slot)
2545 mDevice->sendSlot(SECOND_SLOT);
2546 mDevice->sendPointerUp();
2547 // Send a single sync for the above 2 pointer updates
2548 mDevice->sendSync();
2549
2550 // First, we should get POINTER_UP for the second pointer
2551 assertReceivedMotion(ACTION_POINTER_1_UP,
2552 {/*first pointer */ centerPoint + Point(5, 5),
2553 /*second pointer*/ secondPoint});
2554
2555 // Next, the MOVE event for the first pointer
2556 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2557}
2558
2559/**
2560 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2561 * move, and then it will go up, all in the same frame.
2562 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2563 * gets sent to the listener.
2564 */
2565TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2566 NotifyMotionArgs args;
2567 const Point centerPoint = mDevice->getCenterPoint();
2568
2569 // ACTION_DOWN
2570 mDevice->sendSlot(FIRST_SLOT);
2571 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2572 mDevice->sendDown(centerPoint);
2573 mDevice->sendSync();
2574 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2575
2576 // ACTION_POINTER_DOWN (Second slot)
2577 const Point secondPoint = centerPoint + Point(100, 100);
2578 mDevice->sendSlot(SECOND_SLOT);
2579 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2580 mDevice->sendDown(secondPoint);
2581 mDevice->sendSync();
2582 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2583
2584 // ACTION_MOVE (First slot)
2585 mDevice->sendSlot(FIRST_SLOT);
2586 mDevice->sendMove(centerPoint + Point(5, 5));
2587 // ACTION_POINTER_UP (Second slot)
2588 mDevice->sendSlot(SECOND_SLOT);
2589 mDevice->sendMove(secondPoint + Point(6, 6));
2590 mDevice->sendPointerUp();
2591 // Send a single sync for the above 2 pointer updates
2592 mDevice->sendSync();
2593
2594 // First, we should get POINTER_UP for the second pointer
2595 // The movement of the second pointer during the liftoff frame is ignored.
2596 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2597 assertReceivedMotion(ACTION_POINTER_1_UP,
2598 {/*first pointer */ centerPoint + Point(5, 5),
2599 /*second pointer*/ secondPoint});
2600
2601 // Next, the MOVE event for the first pointer
2602 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2603}
2604
Arthur Hungaab25622020-01-16 11:22:11 +08002605TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2606 NotifyMotionArgs args;
2607 const Point centerPoint = mDevice->getCenterPoint();
2608
2609 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002610 mDevice->sendSlot(FIRST_SLOT);
2611 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002612 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002613 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002614 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2615 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2616
arthurhungcc7f9802020-04-30 17:55:40 +08002617 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002618 const Point secondPoint = centerPoint + Point(100, 100);
2619 mDevice->sendSlot(SECOND_SLOT);
2620 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2621 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002622 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002623 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002624 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002625
arthurhungcc7f9802020-04-30 17:55:40 +08002626 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002627 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002628 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002629 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2630 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2631
arthurhungcc7f9802020-04-30 17:55:40 +08002632 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2633 // a palm event.
2634 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002635 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002636 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002637 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002638 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002639 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002640
arthurhungcc7f9802020-04-30 17:55:40 +08002641 // Send up to second slot, expect first slot send moving.
2642 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002643 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002644 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2645 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002646
arthurhungcc7f9802020-04-30 17:55:40 +08002647 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002648 mDevice->sendSlot(FIRST_SLOT);
2649 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002650 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002651
arthurhungcc7f9802020-04-30 17:55:40 +08002652 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2653 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002654}
2655
Michael Wrightd02c5b62014-02-10 15:10:22 -08002656// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002657class InputDeviceTest : public testing::Test {
2658protected:
2659 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002660 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002661 static const int32_t DEVICE_ID;
2662 static const int32_t DEVICE_GENERATION;
2663 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002664 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002665 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002666
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002667 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002668 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002669 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002670 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002671 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002672
Chris Yea52ade12020-08-27 16:49:20 -07002673 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002674 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002675 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002676 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002677 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002678 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002679 InputDeviceIdentifier identifier;
2680 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002681 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002682 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002683 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002684 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002685 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002686 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002687 }
2688
Chris Yea52ade12020-08-27 16:49:20 -07002689 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002690 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002691 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002692 }
2693};
2694
2695const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002696const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002697const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002698const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2699const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002700const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002701 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002702const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002703
2704TEST_F(InputDeviceTest, ImmutableProperties) {
2705 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002706 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002707 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002708}
2709
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002710TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
2711 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
2712
2713 // Configuration
2714 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2715 InputReaderConfiguration config;
2716 mDevice->configure(ARBITRARY_TIME, &config, 0);
2717
2718 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
2719}
2720
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002721TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2722 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002723}
2724
Michael Wrightd02c5b62014-02-10 15:10:22 -08002725TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2726 // Configuration.
2727 InputReaderConfiguration config;
2728 mDevice->configure(ARBITRARY_TIME, &config, 0);
2729
2730 // Reset.
2731 mDevice->reset(ARBITRARY_TIME);
2732
2733 NotifyDeviceResetArgs resetArgs;
2734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2735 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2736 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2737
2738 // Metadata.
2739 ASSERT_TRUE(mDevice->isIgnored());
2740 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2741
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002742 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002743 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002744 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002745 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2746 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2747
2748 // State queries.
2749 ASSERT_EQ(0, mDevice->getMetaState());
2750
2751 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2752 << "Ignored device should return unknown key code state.";
2753 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2754 << "Ignored device should return unknown scan code state.";
2755 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2756 << "Ignored device should return unknown switch state.";
2757
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002758 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002759 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002760 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002761 << "Ignored device should never mark any key codes.";
2762 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2763 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2764}
2765
2766TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2767 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002768 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08002769
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002770 FakeInputMapper& mapper1 =
2771 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002772 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2773 mapper1.setMetaState(AMETA_ALT_ON);
2774 mapper1.addSupportedKeyCode(AKEYCODE_A);
2775 mapper1.addSupportedKeyCode(AKEYCODE_B);
2776 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2777 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2778 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2779 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2780 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002781
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002782 FakeInputMapper& mapper2 =
2783 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002784 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002785
2786 InputReaderConfiguration config;
2787 mDevice->configure(ARBITRARY_TIME, &config, 0);
2788
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002789 std::string propertyValue;
2790 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002791 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002792 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002793
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002794 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2795 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002796
2797 // Reset
2798 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002799 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2800 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002801
2802 NotifyDeviceResetArgs resetArgs;
2803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2804 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2805 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2806
2807 // Metadata.
2808 ASSERT_FALSE(mDevice->isIgnored());
2809 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2810
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002811 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002812 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002813 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002814 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2815 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2816
2817 // State queries.
2818 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2819 << "Should query mappers and combine meta states.";
2820
2821 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2822 << "Should return unknown key code state when source not supported.";
2823 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2824 << "Should return unknown scan code state when source not supported.";
2825 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2826 << "Should return unknown switch state when source not supported.";
2827
2828 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2829 << "Should query mapper when source is supported.";
2830 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2831 << "Should query mapper when source is supported.";
2832 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2833 << "Should query mapper when source is supported.";
2834
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002835 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002836 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002837 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002838 << "Should do nothing when source is unsupported.";
2839 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2840 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2841 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2842 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2843
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002844 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002845 << "Should query mapper when source is supported.";
2846 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2847 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2848 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2849 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2850
2851 // Event handling.
2852 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002853 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002854 mDevice->process(&event, 1);
2855
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002856 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2857 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002858}
2859
Arthur Hung2c9a3342019-07-23 14:18:59 +08002860// A single input device is associated with a specific display. Check that:
2861// 1. Device is disabled if the viewport corresponding to the associated display is not found
2862// 2. Device is disabled when setEnabled API is called
2863TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002864 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002865
2866 // First Configuration.
2867 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2868
2869 // Device should be enabled by default.
2870 ASSERT_TRUE(mDevice->isEnabled());
2871
2872 // Prepare associated info.
2873 constexpr uint8_t hdmi = 1;
2874 const std::string UNIQUE_ID = "local:1";
2875
2876 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2877 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2878 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2879 // Device should be disabled because it is associated with a specific display via
2880 // input port <-> display port association, but the corresponding display is not found
2881 ASSERT_FALSE(mDevice->isEnabled());
2882
2883 // Prepare displays.
2884 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002885 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2886 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002887 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2888 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2889 ASSERT_TRUE(mDevice->isEnabled());
2890
2891 // Device should be disabled after set disable.
2892 mFakePolicy->addDisabledDevice(mDevice->getId());
2893 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2894 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2895 ASSERT_FALSE(mDevice->isEnabled());
2896
2897 // Device should still be disabled even found the associated display.
2898 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2899 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2900 ASSERT_FALSE(mDevice->isEnabled());
2901}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002902
Christine Franks1ba71cc2021-04-07 14:37:42 -07002903TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2904 // Device should be enabled by default.
2905 mFakePolicy->clearViewports();
2906 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2907 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2908 ASSERT_TRUE(mDevice->isEnabled());
2909
2910 // Device should be disabled because it is associated with a specific display, but the
2911 // corresponding display is not found.
2912 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
Christine Franks2a2293c2022-01-18 11:51:16 -08002913 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002914 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2915 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2916 ASSERT_FALSE(mDevice->isEnabled());
2917
2918 // Device should be enabled when a display is found.
2919 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2920 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2921 NO_PORT, ViewportType::INTERNAL);
2922 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2923 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2924 ASSERT_TRUE(mDevice->isEnabled());
2925
2926 // Device should be disabled after set disable.
2927 mFakePolicy->addDisabledDevice(mDevice->getId());
2928 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2929 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2930 ASSERT_FALSE(mDevice->isEnabled());
2931
2932 // Device should still be disabled even found the associated display.
2933 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2934 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2935 ASSERT_FALSE(mDevice->isEnabled());
2936}
2937
Christine Franks2a2293c2022-01-18 11:51:16 -08002938TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2939 mFakePolicy->clearViewports();
2940 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2941 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2942
2943 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2944 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2945 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2946 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2947 NO_PORT, ViewportType::INTERNAL);
2948 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2949 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2950 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2951}
2952
Michael Wrightd02c5b62014-02-10 15:10:22 -08002953// --- InputMapperTest ---
2954
2955class InputMapperTest : public testing::Test {
2956protected:
2957 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002958 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002959 static const int32_t DEVICE_ID;
2960 static const int32_t DEVICE_GENERATION;
2961 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002962 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002963 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002964
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002965 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002966 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002967 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002968 std::unique_ptr<InstrumentedInputReader> mReader;
2969 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002970
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002971 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002972 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002973 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002974 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002975 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002976 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002977 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002978 // Consume the device reset notification generated when adding a new device.
2979 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002980 }
2981
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002982 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002983 SetUp(DEVICE_CLASSES);
2984 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002985
Chris Yea52ade12020-08-27 16:49:20 -07002986 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002987 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002988 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002989 }
2990
2991 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002992 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002993 }
2994
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002995 void configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00002996 if (!changes ||
2997 (changes &
2998 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
2999 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003000 mReader->requestRefreshConfiguration(changes);
3001 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08003002 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003003 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003004 // Loop the reader to flush the input listener queue.
3005 mReader->loopOnce();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003006 }
3007
arthurhungdcef2dc2020-08-11 14:47:50 +08003008 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
3009 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003010 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003011 InputDeviceIdentifier identifier;
3012 identifier.name = name;
3013 identifier.location = location;
3014 std::shared_ptr<InputDevice> device =
3015 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
3016 identifier);
3017 mReader->pushNextDevice(device);
3018 mFakeEventHub->addDevice(eventHubId, name, classes);
3019 mReader->loopOnce();
3020 return device;
3021 }
3022
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003023 template <class T, typename... Args>
3024 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003025 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003026 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003027 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07003028 mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003029 // Loop the reader to flush the input listener queue.
3030 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003031 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003032 }
3033
3034 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003035 int32_t orientation, const std::string& uniqueId,
3036 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003037 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3038 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003039 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3040 }
3041
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003042 void clearViewports() {
3043 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003044 }
3045
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003046 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
3047 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003048 RawEvent event;
3049 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003050 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003051 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003052 event.type = type;
3053 event.code = code;
3054 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003055 mapper.process(&event);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003056 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003057 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003058 }
3059
3060 static void assertMotionRange(const InputDeviceInfo& info,
3061 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3062 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003063 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003064 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3065 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3066 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3067 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3068 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3069 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3070 }
3071
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003072 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3073 float size, float touchMajor, float touchMinor, float toolMajor,
3074 float toolMinor, float orientation, float distance,
3075 float scaledAxisEpsilon = 1.f) {
3076 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3077 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003078 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3079 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003080 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3081 scaledAxisEpsilon);
3082 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3083 scaledAxisEpsilon);
3084 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3085 scaledAxisEpsilon);
3086 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3087 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003088 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3089 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3090 }
3091
Michael Wright17db18e2020-06-26 20:51:44 +01003092 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003093 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003094 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003095 ASSERT_NEAR(x, actualX, 1);
3096 ASSERT_NEAR(y, actualY, 1);
3097 }
3098};
3099
3100const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003101const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003102const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003103const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3104const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003105const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3106 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003107const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003108
3109// --- SwitchInputMapperTest ---
3110
3111class SwitchInputMapperTest : public InputMapperTest {
3112protected:
3113};
3114
3115TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003116 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003117
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003118 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003119}
3120
3121TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003122 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003123
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003124 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003125 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003126
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003127 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003128 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003129}
3130
3131TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003132 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003133
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003134 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3135 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3137 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003138
3139 NotifySwitchArgs args;
3140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3141 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003142 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3143 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003144 args.switchMask);
3145 ASSERT_EQ(uint32_t(0), args.policyFlags);
3146}
3147
Chris Ye87143712020-11-10 05:05:58 +00003148// --- VibratorInputMapperTest ---
3149class VibratorInputMapperTest : public InputMapperTest {
3150protected:
3151 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3152};
3153
3154TEST_F(VibratorInputMapperTest, GetSources) {
3155 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3156
3157 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3158}
3159
3160TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3161 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3162
3163 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3164}
3165
3166TEST_F(VibratorInputMapperTest, Vibrate) {
3167 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003168 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003169 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3170
3171 VibrationElement pattern(2);
3172 VibrationSequence sequence(2);
3173 pattern.duration = std::chrono::milliseconds(200);
3174 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3175 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3176 sequence.addElement(pattern);
3177 pattern.duration = std::chrono::milliseconds(500);
3178 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3179 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3180 sequence.addElement(pattern);
3181
3182 std::vector<int64_t> timings = {0, 1};
3183 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3184
3185 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003186 // Start vibrating
3187 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003188 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003189 // Verify vibrator state listener was notified.
3190 mReader->loopOnce();
3191 NotifyVibratorStateArgs args;
3192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3193 ASSERT_EQ(DEVICE_ID, args.deviceId);
3194 ASSERT_TRUE(args.isOn);
3195 // Stop vibrating
3196 mapper.cancelVibrate(VIBRATION_TOKEN);
3197 ASSERT_FALSE(mapper.isVibrating());
3198 // Verify vibrator state listener was notified.
3199 mReader->loopOnce();
3200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3201 ASSERT_EQ(DEVICE_ID, args.deviceId);
3202 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003203}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003204
Chris Yef59a2f42020-10-16 12:55:26 -07003205// --- SensorInputMapperTest ---
3206
3207class SensorInputMapperTest : public InputMapperTest {
3208protected:
3209 static const int32_t ACCEL_RAW_MIN;
3210 static const int32_t ACCEL_RAW_MAX;
3211 static const int32_t ACCEL_RAW_FUZZ;
3212 static const int32_t ACCEL_RAW_FLAT;
3213 static const int32_t ACCEL_RAW_RESOLUTION;
3214
3215 static const int32_t GYRO_RAW_MIN;
3216 static const int32_t GYRO_RAW_MAX;
3217 static const int32_t GYRO_RAW_FUZZ;
3218 static const int32_t GYRO_RAW_FLAT;
3219 static const int32_t GYRO_RAW_RESOLUTION;
3220
3221 static const float GRAVITY_MS2_UNIT;
3222 static const float DEGREE_RADIAN_UNIT;
3223
3224 void prepareAccelAxes();
3225 void prepareGyroAxes();
3226 void setAccelProperties();
3227 void setGyroProperties();
3228 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3229};
3230
3231const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3232const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3233const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3234const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3235const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3236
3237const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3238const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3239const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3240const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3241const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3242
3243const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3244const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3245
3246void SensorInputMapperTest::prepareAccelAxes() {
3247 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3248 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3249 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3250 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3251 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3252 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3253}
3254
3255void SensorInputMapperTest::prepareGyroAxes() {
3256 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3257 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3258 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3259 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3260 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3261 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3262}
3263
3264void SensorInputMapperTest::setAccelProperties() {
3265 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3266 /* sensorDataIndex */ 0);
3267 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3268 /* sensorDataIndex */ 1);
3269 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3270 /* sensorDataIndex */ 2);
3271 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3272 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3273 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3274 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3275 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3276}
3277
3278void SensorInputMapperTest::setGyroProperties() {
3279 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3280 /* sensorDataIndex */ 0);
3281 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3282 /* sensorDataIndex */ 1);
3283 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3284 /* sensorDataIndex */ 2);
3285 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3286 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3287 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3288 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3289 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3290}
3291
3292TEST_F(SensorInputMapperTest, GetSources) {
3293 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3294
3295 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3296}
3297
3298TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3299 setAccelProperties();
3300 prepareAccelAxes();
3301 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3302
3303 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3304 std::chrono::microseconds(10000),
3305 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003306 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3308 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3309 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3310 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3311 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003312
3313 NotifySensorArgs args;
3314 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3315 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3316 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3317
3318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3319 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3320 ASSERT_EQ(args.deviceId, DEVICE_ID);
3321 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3322 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3323 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3324 ASSERT_EQ(args.values, values);
3325 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3326}
3327
3328TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3329 setGyroProperties();
3330 prepareGyroAxes();
3331 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3332
3333 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3334 std::chrono::microseconds(10000),
3335 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003336 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003337 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3338 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3339 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3340 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3341 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003342
3343 NotifySensorArgs args;
3344 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3345 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3346 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3347
3348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3349 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3350 ASSERT_EQ(args.deviceId, DEVICE_ID);
3351 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3352 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3353 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3354 ASSERT_EQ(args.values, values);
3355 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3356}
3357
Michael Wrightd02c5b62014-02-10 15:10:22 -08003358// --- KeyboardInputMapperTest ---
3359
3360class KeyboardInputMapperTest : public InputMapperTest {
3361protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003362 const std::string UNIQUE_ID = "local:0";
3363
3364 void prepareDisplay(int32_t orientation);
3365
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003366 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003367 int32_t originalKeyCode, int32_t rotatedKeyCode,
3368 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003369};
3370
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003371/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3372 * orientation.
3373 */
3374void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003375 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3376 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003377}
3378
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003379void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003380 int32_t originalScanCode, int32_t originalKeyCode,
3381 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003382 NotifyKeyArgs args;
3383
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003384 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3386 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3387 ASSERT_EQ(originalScanCode, args.scanCode);
3388 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003389 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003390
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003391 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3393 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3394 ASSERT_EQ(originalScanCode, args.scanCode);
3395 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003396 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003397}
3398
Michael Wrightd02c5b62014-02-10 15:10:22 -08003399TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003400 KeyboardInputMapper& mapper =
3401 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3402 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003403
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003404 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003405}
3406
3407TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3408 const int32_t USAGE_A = 0x070004;
3409 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003410 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3411 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003412 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3413 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3414 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003415
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003416 KeyboardInputMapper& mapper =
3417 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3418 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003419 // Initial metastate is AMETA_NONE.
3420 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003421
3422 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003423 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003424 NotifyKeyArgs args;
3425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3426 ASSERT_EQ(DEVICE_ID, args.deviceId);
3427 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3428 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3429 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3430 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3431 ASSERT_EQ(KEY_HOME, args.scanCode);
3432 ASSERT_EQ(AMETA_NONE, args.metaState);
3433 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3434 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3435 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3436
3437 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003438 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3440 ASSERT_EQ(DEVICE_ID, args.deviceId);
3441 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3442 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3443 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3444 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3445 ASSERT_EQ(KEY_HOME, args.scanCode);
3446 ASSERT_EQ(AMETA_NONE, args.metaState);
3447 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3448 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3449 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3450
3451 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003452 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3453 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3455 ASSERT_EQ(DEVICE_ID, args.deviceId);
3456 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3457 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3458 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3459 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3460 ASSERT_EQ(0, args.scanCode);
3461 ASSERT_EQ(AMETA_NONE, args.metaState);
3462 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3463 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3464 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3465
3466 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003467 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3468 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3470 ASSERT_EQ(DEVICE_ID, args.deviceId);
3471 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3472 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3473 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3474 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3475 ASSERT_EQ(0, args.scanCode);
3476 ASSERT_EQ(AMETA_NONE, args.metaState);
3477 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3478 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3479 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3480
3481 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003482 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3483 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3485 ASSERT_EQ(DEVICE_ID, args.deviceId);
3486 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3487 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3488 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3489 ASSERT_EQ(0, args.keyCode);
3490 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3491 ASSERT_EQ(AMETA_NONE, args.metaState);
3492 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3493 ASSERT_EQ(0U, args.policyFlags);
3494 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3495
3496 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003497 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3498 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3500 ASSERT_EQ(DEVICE_ID, args.deviceId);
3501 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3502 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3503 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3504 ASSERT_EQ(0, args.keyCode);
3505 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3506 ASSERT_EQ(AMETA_NONE, args.metaState);
3507 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3508 ASSERT_EQ(0U, args.policyFlags);
3509 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3510}
3511
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003512/**
3513 * Ensure that the readTime is set to the time when the EV_KEY is received.
3514 */
3515TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3516 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3517
3518 KeyboardInputMapper& mapper =
3519 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3520 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3521 NotifyKeyArgs args;
3522
3523 // Key down
3524 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3526 ASSERT_EQ(12, args.readTime);
3527
3528 // Key up
3529 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3531 ASSERT_EQ(15, args.readTime);
3532}
3533
Michael Wrightd02c5b62014-02-10 15:10:22 -08003534TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003535 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3536 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003537 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3538 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3539 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003540
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003541 KeyboardInputMapper& mapper =
3542 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3543 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003544
Arthur Hung95f68612022-04-07 14:08:22 +08003545 // Initial metastate is AMETA_NONE.
3546 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003547
3548 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003549 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003550 NotifyKeyArgs args;
3551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3552 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003553 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003554 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003555
3556 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003557 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3559 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003560 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003561
3562 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003563 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3565 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003566 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003567
3568 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003569 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3571 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003572 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003573 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003574}
3575
3576TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003577 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3578 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3579 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3580 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003581
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003582 KeyboardInputMapper& mapper =
3583 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3584 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003585
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003586 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003587 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3588 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3589 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3590 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3591 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3592 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3593 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3594 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3595}
3596
3597TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003598 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3599 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3600 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3601 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003602
Michael Wrightd02c5b62014-02-10 15:10:22 -08003603 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003604 KeyboardInputMapper& mapper =
3605 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3606 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003607
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003608 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003609 ASSERT_NO_FATAL_FAILURE(
3610 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3611 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3612 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3613 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3614 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3615 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3616 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003617
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003618 clearViewports();
3619 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003620 ASSERT_NO_FATAL_FAILURE(
3621 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3622 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3623 AKEYCODE_DPAD_UP, DISPLAY_ID));
3624 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3625 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3626 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3627 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003628
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003629 clearViewports();
3630 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003631 ASSERT_NO_FATAL_FAILURE(
3632 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3633 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3634 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3635 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3636 AKEYCODE_DPAD_UP, DISPLAY_ID));
3637 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3638 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003639
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003640 clearViewports();
3641 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003642 ASSERT_NO_FATAL_FAILURE(
3643 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3644 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3645 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3646 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3647 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3648 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3649 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003650
3651 // Special case: if orientation changes while key is down, we still emit the same keycode
3652 // in the key up as we did in the key down.
3653 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003654 clearViewports();
3655 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003656 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3658 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3659 ASSERT_EQ(KEY_UP, args.scanCode);
3660 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3661
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003662 clearViewports();
3663 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003664 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3666 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3667 ASSERT_EQ(KEY_UP, args.scanCode);
3668 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3669}
3670
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003671TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3672 // If the keyboard is not orientation aware,
3673 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003674 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003675
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003676 KeyboardInputMapper& mapper =
3677 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3678 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003679 NotifyKeyArgs args;
3680
3681 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003682 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003684 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3686 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3687
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003688 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003689 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003691 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3693 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3694}
3695
3696TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3697 // If the keyboard is orientation aware,
3698 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003699 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003700
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003701 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003702 KeyboardInputMapper& mapper =
3703 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3704 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003705 NotifyKeyArgs args;
3706
3707 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3708 // ^--- already checked by the previous test
3709
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003710 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003711 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003712 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003714 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3716 ASSERT_EQ(DISPLAY_ID, args.displayId);
3717
3718 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003719 clearViewports();
3720 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003721 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003722 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003724 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3726 ASSERT_EQ(newDisplayId, args.displayId);
3727}
3728
Michael Wrightd02c5b62014-02-10 15:10:22 -08003729TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003730 KeyboardInputMapper& mapper =
3731 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3732 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003733
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003734 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003735 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003736
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003737 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003738 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003739}
3740
Philip Junker4af3b3d2021-12-14 10:36:55 +01003741TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3742 KeyboardInputMapper& mapper =
3743 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3744 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3745
3746 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3747 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3748 << "If a mapping is available, the result is equal to the mapping";
3749
3750 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3751 << "If no mapping is available, the result is the key location";
3752}
3753
Michael Wrightd02c5b62014-02-10 15:10:22 -08003754TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003755 KeyboardInputMapper& mapper =
3756 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3757 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003758
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003759 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003760 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003761
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003762 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003763 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003764}
3765
3766TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003767 KeyboardInputMapper& mapper =
3768 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3769 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003770
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003771 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003772
Michael Wrightd02c5b62014-02-10 15:10:22 -08003773 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003774 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003775 ASSERT_TRUE(flags[0]);
3776 ASSERT_FALSE(flags[1]);
3777}
3778
3779TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003780 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3781 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3782 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3783 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3784 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3785 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003786
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003787 KeyboardInputMapper& mapper =
3788 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3789 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003790 // Initial metastate is AMETA_NONE.
3791 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003792
3793 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003794 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3795 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3796 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003797
3798 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3800 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003801 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3802 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3803 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003804 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003805
3806 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3808 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003809 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3810 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3811 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003812 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003813
3814 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003815 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3816 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003817 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3818 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3819 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003820 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003821
3822 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003823 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3824 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003825 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3826 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3827 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003828 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003829
3830 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003833 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3834 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3835 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003836 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003837
3838 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003839 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3840 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003841 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3842 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3843 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003844 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003845}
3846
Chris Yea52ade12020-08-27 16:49:20 -07003847TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3848 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3849 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3850 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3851 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3852
3853 KeyboardInputMapper& mapper =
3854 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3855 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3856
Chris Yea52ade12020-08-27 16:49:20 -07003857 // Meta state should be AMETA_NONE after reset
3858 mapper.reset(ARBITRARY_TIME);
3859 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3860 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3861 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3862 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3863
3864 NotifyKeyArgs args;
3865 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003866 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3868 ASSERT_EQ(AMETA_NONE, args.metaState);
3869 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3870 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3871 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3872
3873 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003874 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3876 ASSERT_EQ(AMETA_NONE, args.metaState);
3877 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3878 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3879 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3880}
3881
Arthur Hung2c9a3342019-07-23 14:18:59 +08003882TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3883 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003884 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3885 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3886 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3887 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003888
3889 // keyboard 2.
3890 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003891 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003892 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003893 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003894 std::shared_ptr<InputDevice> device2 =
3895 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003896 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003897
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003898 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3899 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3900 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3901 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003902
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003903 KeyboardInputMapper& mapper =
3904 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3905 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003906
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003907 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003908 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003909 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003910 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3911 device2->reset(ARBITRARY_TIME);
3912
3913 // Prepared displays and associated info.
3914 constexpr uint8_t hdmi1 = 0;
3915 constexpr uint8_t hdmi2 = 1;
3916 const std::string SECONDARY_UNIQUE_ID = "local:1";
3917
3918 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3919 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3920
3921 // No associated display viewport found, should disable the device.
3922 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3923 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3924 ASSERT_FALSE(device2->isEnabled());
3925
3926 // Prepare second display.
3927 constexpr int32_t newDisplayId = 2;
3928 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003929 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003930 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003931 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003932 // Default device will reconfigure above, need additional reconfiguration for another device.
3933 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3934 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3935
3936 // Device should be enabled after the associated display is found.
3937 ASSERT_TRUE(mDevice->isEnabled());
3938 ASSERT_TRUE(device2->isEnabled());
3939
3940 // Test pad key events
3941 ASSERT_NO_FATAL_FAILURE(
3942 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3943 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3944 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3945 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3946 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3947 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3948 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3949
3950 ASSERT_NO_FATAL_FAILURE(
3951 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3952 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3953 AKEYCODE_DPAD_RIGHT, newDisplayId));
3954 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3955 AKEYCODE_DPAD_DOWN, newDisplayId));
3956 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3957 AKEYCODE_DPAD_LEFT, newDisplayId));
3958}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003959
arthurhungc903df12020-08-11 15:08:42 +08003960TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3961 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3962 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3963 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3964 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3965 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3966 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3967
3968 KeyboardInputMapper& mapper =
3969 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3970 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003971 // Initial metastate is AMETA_NONE.
3972 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003973
3974 // Initialization should have turned all of the lights off.
3975 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3976 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3977 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3978
3979 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003980 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3981 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003982 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3983 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3984
3985 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003986 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3987 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003988 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3989 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3990
3991 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003992 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003994 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3995 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3996
3997 mFakeEventHub->removeDevice(EVENTHUB_ID);
3998 mReader->loopOnce();
3999
4000 // keyboard 2 should default toggle keys.
4001 const std::string USB2 = "USB2";
4002 const std::string DEVICE_NAME2 = "KEYBOARD2";
4003 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4004 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4005 std::shared_ptr<InputDevice> device2 =
4006 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004007 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08004008 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4009 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4010 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4011 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4012 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4013 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4014
arthurhung6fe95782020-10-05 22:41:16 +08004015 KeyboardInputMapper& mapper2 =
4016 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4017 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08004018 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4019 device2->reset(ARBITRARY_TIME);
4020
4021 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4022 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4023 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004024 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4025 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004026}
4027
Arthur Hungcb40a002021-08-03 14:31:01 +00004028TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4029 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4030 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4031 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4032
4033 // Suppose we have two mappers. (DPAD + KEYBOARD)
4034 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4035 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4036 KeyboardInputMapper& mapper =
4037 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4038 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004039 // Initial metastate is AMETA_NONE.
4040 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004041
4042 mReader->toggleCapsLockState(DEVICE_ID);
4043 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4044}
4045
Arthur Hungfb3cc112022-04-13 07:39:50 +00004046TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4047 // keyboard 1.
4048 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4049 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4050 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4051 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4052 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4053 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4054
4055 KeyboardInputMapper& mapper1 =
4056 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4057 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4058
4059 // keyboard 2.
4060 const std::string USB2 = "USB2";
4061 const std::string DEVICE_NAME2 = "KEYBOARD2";
4062 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4063 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4064 std::shared_ptr<InputDevice> device2 =
4065 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4066 ftl::Flags<InputDeviceClass>(0));
4067 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4068 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4069 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4070 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4071 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4072 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4073
4074 KeyboardInputMapper& mapper2 =
4075 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4076 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4077 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4078 device2->reset(ARBITRARY_TIME);
4079
Arthur Hung95f68612022-04-07 14:08:22 +08004080 // Initial metastate is AMETA_NONE.
4081 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4082 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4083
4084 // Toggle num lock on and off.
4085 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4086 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004087 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4088 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4089 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4090
4091 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4092 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4093 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4094 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4095 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4096
4097 // Toggle caps lock on and off.
4098 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4099 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4100 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4101 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4102 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4103
4104 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4105 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4106 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4107 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4108 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4109
4110 // Toggle scroll lock on and off.
4111 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4112 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4113 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4114 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4115 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4116
4117 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4118 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4119 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4120 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4121 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4122}
4123
Arthur Hung2141d542022-08-23 07:45:21 +00004124TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4125 const int32_t USAGE_A = 0x070004;
4126 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4127 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4128
4129 KeyboardInputMapper& mapper =
4130 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4131 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4132 // Key down by scan code.
4133 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4134 NotifyKeyArgs args;
4135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4136 ASSERT_EQ(DEVICE_ID, args.deviceId);
4137 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4138 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4139 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4140 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4141 ASSERT_EQ(KEY_HOME, args.scanCode);
4142 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4143
4144 // Disable device, it should synthesize cancellation events for down events.
4145 mFakePolicy->addDisabledDevice(DEVICE_ID);
4146 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
4147
4148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4149 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4150 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4151 ASSERT_EQ(KEY_HOME, args.scanCode);
4152 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4153}
4154
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004155// --- KeyboardInputMapperTest_ExternalDevice ---
4156
4157class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4158protected:
Chris Yea52ade12020-08-27 16:49:20 -07004159 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004160};
4161
4162TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004163 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4164 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004165
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004166 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4167 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4168 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4169 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004170
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004171 KeyboardInputMapper& mapper =
4172 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4173 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004174
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004175 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004176 NotifyKeyArgs args;
4177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4178 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4179
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004180 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4182 ASSERT_EQ(uint32_t(0), args.policyFlags);
4183
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004184 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4186 ASSERT_EQ(uint32_t(0), args.policyFlags);
4187
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004188 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4190 ASSERT_EQ(uint32_t(0), args.policyFlags);
4191
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004192 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4194 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4195
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004196 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4198 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4199}
4200
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004201TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004202 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004203
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004204 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4205 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4206 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004207
Powei Fengd041c5d2019-05-03 17:11:33 -07004208 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004209 KeyboardInputMapper& mapper =
4210 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4211 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004212
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004213 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004214 NotifyKeyArgs args;
4215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4216 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4217
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004218 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4220 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4221
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004222 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4224 ASSERT_EQ(uint32_t(0), args.policyFlags);
4225
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004226 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4228 ASSERT_EQ(uint32_t(0), args.policyFlags);
4229
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004230 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4232 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4233
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004234 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4236 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4237}
4238
Michael Wrightd02c5b62014-02-10 15:10:22 -08004239// --- CursorInputMapperTest ---
4240
4241class CursorInputMapperTest : public InputMapperTest {
4242protected:
4243 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4244
Michael Wright17db18e2020-06-26 20:51:44 +01004245 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004246
Chris Yea52ade12020-08-27 16:49:20 -07004247 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004248 InputMapperTest::SetUp();
4249
Michael Wright17db18e2020-06-26 20:51:44 +01004250 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004251 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004252 }
4253
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004254 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4255 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004256
4257 void prepareDisplay(int32_t orientation) {
4258 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004259 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004260 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4261 orientation, uniqueId, NO_PORT, viewportType);
4262 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004263
4264 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4265 float pressure) {
4266 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4267 0.0f, 0.0f, 0.0f, EPSILON));
4268 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269};
4270
4271const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4272
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004273void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4274 int32_t originalY, int32_t rotatedX,
4275 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004276 NotifyMotionArgs args;
4277
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004278 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4279 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4280 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4282 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004283 ASSERT_NO_FATAL_FAILURE(
4284 assertCursorPointerCoords(args.pointerCoords[0],
4285 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4286 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004287}
4288
4289TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004290 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004291 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004292
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004293 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004294}
4295
4296TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004297 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004298 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004299
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004300 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004301}
4302
4303TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004304 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004305 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004306
4307 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004308 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004309
4310 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004311 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4312 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004313 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4314 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4315
4316 // When the bounds are set, then there should be a valid motion range.
4317 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4318
4319 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004320 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004321
4322 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4323 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4324 1, 800 - 1, 0.0f, 0.0f));
4325 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4326 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4327 2, 480 - 1, 0.0f, 0.0f));
4328 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4329 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4330 0.0f, 1.0f, 0.0f, 0.0f));
4331}
4332
4333TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004334 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004335 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004336
4337 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004338 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004339
4340 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4341 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4342 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4343 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4344 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4345 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4346 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4347 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4348 0.0f, 1.0f, 0.0f, 0.0f));
4349}
4350
4351TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004352 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004353 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004354
arthurhungdcef2dc2020-08-11 14:47:50 +08004355 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004356
4357 NotifyMotionArgs args;
4358
4359 // Button press.
4360 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004361 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4362 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4364 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4365 ASSERT_EQ(DEVICE_ID, args.deviceId);
4366 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4367 ASSERT_EQ(uint32_t(0), args.policyFlags);
4368 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4369 ASSERT_EQ(0, args.flags);
4370 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4371 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4372 ASSERT_EQ(0, args.edgeFlags);
4373 ASSERT_EQ(uint32_t(1), args.pointerCount);
4374 ASSERT_EQ(0, args.pointerProperties[0].id);
4375 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004376 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004377 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4378 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4379 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4380
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4382 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4383 ASSERT_EQ(DEVICE_ID, args.deviceId);
4384 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4385 ASSERT_EQ(uint32_t(0), args.policyFlags);
4386 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4387 ASSERT_EQ(0, args.flags);
4388 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4389 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4390 ASSERT_EQ(0, args.edgeFlags);
4391 ASSERT_EQ(uint32_t(1), args.pointerCount);
4392 ASSERT_EQ(0, args.pointerProperties[0].id);
4393 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004394 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004395 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4396 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4397 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4398
Michael Wrightd02c5b62014-02-10 15:10:22 -08004399 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004400 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4401 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4403 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4404 ASSERT_EQ(DEVICE_ID, args.deviceId);
4405 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4406 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004407 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4408 ASSERT_EQ(0, args.flags);
4409 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4410 ASSERT_EQ(0, args.buttonState);
4411 ASSERT_EQ(0, args.edgeFlags);
4412 ASSERT_EQ(uint32_t(1), args.pointerCount);
4413 ASSERT_EQ(0, args.pointerProperties[0].id);
4414 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004415 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004416 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4417 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4418 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4419
4420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4421 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4422 ASSERT_EQ(DEVICE_ID, args.deviceId);
4423 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4424 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004425 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4426 ASSERT_EQ(0, args.flags);
4427 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4428 ASSERT_EQ(0, args.buttonState);
4429 ASSERT_EQ(0, args.edgeFlags);
4430 ASSERT_EQ(uint32_t(1), args.pointerCount);
4431 ASSERT_EQ(0, args.pointerProperties[0].id);
4432 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004433 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4435 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4436 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4437}
4438
4439TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004440 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004441 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004442
4443 NotifyMotionArgs args;
4444
4445 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4447 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4449 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004450 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4451 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4452 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004453
4454 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004455 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4456 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4458 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004459 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4460 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004461}
4462
4463TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004465 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466
4467 NotifyMotionArgs args;
4468
4469 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004470 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4471 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4473 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004474 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004475
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4477 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004478 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004479
Michael Wrightd02c5b62014-02-10 15:10:22 -08004480 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004481 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004484 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004485 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004486
4487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004489 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004490}
4491
4492TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004493 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004494 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004495
4496 NotifyMotionArgs args;
4497
4498 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004499 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4500 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4501 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4502 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4504 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004505 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4506 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4507 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004508
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4510 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004511 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4512 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4513 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004514
Michael Wrightd02c5b62014-02-10 15:10:22 -08004515 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004516 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4520 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004521 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4522 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4523 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004524
4525 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004526 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4527 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004529 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004530 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004531
4532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004533 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004534 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004535}
4536
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004537TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004538 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004539 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4540 // need to be rotated.
4541 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004542 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004543
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004544 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004545 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4546 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4547 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4548 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4549 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4550 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4551 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4552 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4553}
4554
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004555TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004556 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004557 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4558 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004559 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004560
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004561 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004562 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4563 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4564 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4565 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4566 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4567 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4568 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4569 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4570
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004571 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004572 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4573 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4574 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4575 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4576 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4577 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4578 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4579 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004580
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004581 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004582 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4583 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4584 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4585 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4586 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4587 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4588 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4589 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4590
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004591 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004592 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4593 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4594 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4595 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4596 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4597 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4598 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4599 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004600}
4601
4602TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004603 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004604 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004605
4606 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4607 mFakePointerController->setPosition(100, 200);
4608 mFakePointerController->setButtonState(0);
4609
4610 NotifyMotionArgs motionArgs;
4611 NotifyKeyArgs keyArgs;
4612
4613 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4615 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4617 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4618 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4619 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004620 ASSERT_NO_FATAL_FAILURE(
4621 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004622
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4624 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4625 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4626 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, 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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004629
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004630 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4631 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004633 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004634 ASSERT_EQ(0, motionArgs.buttonState);
4635 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004636 ASSERT_NO_FATAL_FAILURE(
4637 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004638
4639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004640 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004641 ASSERT_EQ(0, motionArgs.buttonState);
4642 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004643 ASSERT_NO_FATAL_FAILURE(
4644 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004645
4646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004647 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004648 ASSERT_EQ(0, motionArgs.buttonState);
4649 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004650 ASSERT_NO_FATAL_FAILURE(
4651 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004652
4653 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004654 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4655 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4656 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4658 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4659 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4660 motionArgs.buttonState);
4661 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4662 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004663 ASSERT_NO_FATAL_FAILURE(
4664 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004665
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4667 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4668 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4669 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4670 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004671 ASSERT_NO_FATAL_FAILURE(
4672 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004673
4674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4675 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4676 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4677 motionArgs.buttonState);
4678 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4679 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004680 ASSERT_NO_FATAL_FAILURE(
4681 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004682
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004683 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4684 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004686 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004687 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4688 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004689 ASSERT_NO_FATAL_FAILURE(
4690 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004691
4692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004693 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004694 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4695 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004696 ASSERT_NO_FATAL_FAILURE(
4697 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004698
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004699 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4700 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004702 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4703 ASSERT_EQ(0, motionArgs.buttonState);
4704 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004705 ASSERT_NO_FATAL_FAILURE(
4706 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004707 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4708 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004709
4710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004711 ASSERT_EQ(0, motionArgs.buttonState);
4712 ASSERT_EQ(0, mFakePointerController->getButtonState());
4713 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004714 ASSERT_NO_FATAL_FAILURE(
4715 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004716
Michael Wrightd02c5b62014-02-10 15:10:22 -08004717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4718 ASSERT_EQ(0, motionArgs.buttonState);
4719 ASSERT_EQ(0, mFakePointerController->getButtonState());
4720 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004721 ASSERT_NO_FATAL_FAILURE(
4722 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004723
4724 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004725 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4726 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4728 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4729 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004730
Michael Wrightd02c5b62014-02-10 15:10:22 -08004731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004732 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004733 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4734 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004735 ASSERT_NO_FATAL_FAILURE(
4736 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004737
4738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4739 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4740 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4741 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004742 ASSERT_NO_FATAL_FAILURE(
4743 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004744
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004745 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4746 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004748 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004749 ASSERT_EQ(0, motionArgs.buttonState);
4750 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004751 ASSERT_NO_FATAL_FAILURE(
4752 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004753
4754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004755 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004756 ASSERT_EQ(0, motionArgs.buttonState);
4757 ASSERT_EQ(0, mFakePointerController->getButtonState());
4758
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004759 ASSERT_NO_FATAL_FAILURE(
4760 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4762 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4763 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4764
4765 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004766 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4767 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4769 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4770 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004771
Michael Wrightd02c5b62014-02-10 15:10:22 -08004772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004773 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004774 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4775 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004776 ASSERT_NO_FATAL_FAILURE(
4777 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004778
4779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4780 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4781 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4782 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004783 ASSERT_NO_FATAL_FAILURE(
4784 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004785
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004786 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4787 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004789 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004790 ASSERT_EQ(0, motionArgs.buttonState);
4791 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004792 ASSERT_NO_FATAL_FAILURE(
4793 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004794
4795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4796 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4797 ASSERT_EQ(0, motionArgs.buttonState);
4798 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004799 ASSERT_NO_FATAL_FAILURE(
4800 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004801
Michael Wrightd02c5b62014-02-10 15:10:22 -08004802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4803 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4804 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4805
4806 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4808 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4810 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4811 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004812
Michael Wrightd02c5b62014-02-10 15:10:22 -08004813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004814 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004815 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4816 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004817 ASSERT_NO_FATAL_FAILURE(
4818 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004819
4820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4821 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4822 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4823 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004824 ASSERT_NO_FATAL_FAILURE(
4825 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004826
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004827 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4828 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004830 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831 ASSERT_EQ(0, motionArgs.buttonState);
4832 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004833 ASSERT_NO_FATAL_FAILURE(
4834 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004835
4836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4837 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4838 ASSERT_EQ(0, motionArgs.buttonState);
4839 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004840 ASSERT_NO_FATAL_FAILURE(
4841 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004842
Michael Wrightd02c5b62014-02-10 15:10:22 -08004843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4844 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4845 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4846
4847 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004848 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4849 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4851 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4852 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004853
Michael Wrightd02c5b62014-02-10 15:10:22 -08004854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004855 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004856 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4857 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004858 ASSERT_NO_FATAL_FAILURE(
4859 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004860
4861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4862 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4863 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4864 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004865 ASSERT_NO_FATAL_FAILURE(
4866 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004867
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004868 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4869 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004871 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004872 ASSERT_EQ(0, motionArgs.buttonState);
4873 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004874 ASSERT_NO_FATAL_FAILURE(
4875 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004876
4877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4878 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4879 ASSERT_EQ(0, motionArgs.buttonState);
4880 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004881 ASSERT_NO_FATAL_FAILURE(
4882 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004883
Michael Wrightd02c5b62014-02-10 15:10:22 -08004884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4885 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4886 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4887}
4888
4889TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004890 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004891 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004892
4893 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4894 mFakePointerController->setPosition(100, 200);
4895 mFakePointerController->setButtonState(0);
4896
4897 NotifyMotionArgs args;
4898
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004899 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4900 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4901 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004903 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4904 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4905 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4906 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 +01004907 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004908}
4909
4910TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004911 addConfigurationProperty("cursor.mode", "pointer");
4912 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004913 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004914
4915 NotifyDeviceResetArgs resetArgs;
4916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4917 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4918 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4919
4920 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4921 mFakePointerController->setPosition(100, 200);
4922 mFakePointerController->setButtonState(0);
4923
4924 NotifyMotionArgs args;
4925
4926 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004927 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4928 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4929 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4931 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4932 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4933 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4934 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 +01004935 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004936
4937 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004938 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4939 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4941 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4942 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4943 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4944 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4946 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4947 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4948 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4949 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4950
4951 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004952 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4953 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4955 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4956 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4957 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4958 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4960 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4961 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4962 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4963 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4964
4965 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004966 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4967 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4968 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4970 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4971 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4972 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4973 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 +01004974 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004975
4976 // Disable pointer capture and check that the device generation got bumped
4977 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004978 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004979 mFakePolicy->setPointerCapture(false);
4980 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004981 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004982
4983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004984 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4985
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004986 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4987 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4988 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4990 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004991 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4992 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4993 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 +01004994 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004995}
4996
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004997/**
4998 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4999 * pointer acceleration or speed processing should not be applied.
5000 */
5001TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
5002 addConfigurationProperty("cursor.mode", "pointer");
5003 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
5004 100.f /*high threshold*/, 10.f /*acceleration*/);
5005 mFakePolicy->setVelocityControlParams(testParams);
5006 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5007
5008 NotifyDeviceResetArgs resetArgs;
5009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5010 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5011 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5012
5013 NotifyMotionArgs args;
5014
5015 // Move and verify scale is applied.
5016 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5017 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5018 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5020 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5021 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5022 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5023 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5024 ASSERT_GT(relX, 10);
5025 ASSERT_GT(relY, 20);
5026
5027 // Enable Pointer Capture
5028 mFakePolicy->setPointerCapture(true);
5029 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5030 NotifyPointerCaptureChangedArgs captureArgs;
5031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5032 ASSERT_TRUE(captureArgs.request.enable);
5033
5034 // Move and verify scale is not applied.
5035 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5036 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5037 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5039 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5040 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5041 ASSERT_EQ(10, args.pointerCoords[0].getX());
5042 ASSERT_EQ(20, args.pointerCoords[0].getY());
5043}
5044
Prabir Pradhan208360b2022-06-24 18:37:04 +00005045TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5046 addConfigurationProperty("cursor.mode", "pointer");
5047 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5048
5049 NotifyDeviceResetArgs resetArgs;
5050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5051 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5052 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5053
5054 // Ensure the display is rotated.
5055 prepareDisplay(DISPLAY_ORIENTATION_90);
5056
5057 NotifyMotionArgs args;
5058
5059 // Verify that the coordinates are rotated.
5060 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5061 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5062 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5064 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5065 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5066 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5067 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5068
5069 // Enable Pointer Capture.
5070 mFakePolicy->setPointerCapture(true);
5071 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5072 NotifyPointerCaptureChangedArgs captureArgs;
5073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5074 ASSERT_TRUE(captureArgs.request.enable);
5075
5076 // Move and verify rotation is not applied.
5077 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5078 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5079 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5081 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5082 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5083 ASSERT_EQ(10, args.pointerCoords[0].getX());
5084 ASSERT_EQ(20, args.pointerCoords[0].getY());
5085}
5086
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005087TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005088 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005089
Garfield Tan888a6a42020-01-09 11:39:16 -08005090 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005091 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08005092 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
5093 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00005094 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
5095 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08005096 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
5097 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5098
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005099 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5100 mFakePointerController->setPosition(100, 200);
5101 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005102
5103 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005104 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5105 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5106 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5108 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5109 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5110 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5111 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 +01005112 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005113 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
5114}
5115
Michael Wrightd02c5b62014-02-10 15:10:22 -08005116// --- TouchInputMapperTest ---
5117
5118class TouchInputMapperTest : public InputMapperTest {
5119protected:
5120 static const int32_t RAW_X_MIN;
5121 static const int32_t RAW_X_MAX;
5122 static const int32_t RAW_Y_MIN;
5123 static const int32_t RAW_Y_MAX;
5124 static const int32_t RAW_TOUCH_MIN;
5125 static const int32_t RAW_TOUCH_MAX;
5126 static const int32_t RAW_TOOL_MIN;
5127 static const int32_t RAW_TOOL_MAX;
5128 static const int32_t RAW_PRESSURE_MIN;
5129 static const int32_t RAW_PRESSURE_MAX;
5130 static const int32_t RAW_ORIENTATION_MIN;
5131 static const int32_t RAW_ORIENTATION_MAX;
5132 static const int32_t RAW_DISTANCE_MIN;
5133 static const int32_t RAW_DISTANCE_MAX;
5134 static const int32_t RAW_TILT_MIN;
5135 static const int32_t RAW_TILT_MAX;
5136 static const int32_t RAW_ID_MIN;
5137 static const int32_t RAW_ID_MAX;
5138 static const int32_t RAW_SLOT_MIN;
5139 static const int32_t RAW_SLOT_MAX;
5140 static const float X_PRECISION;
5141 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005142 static const float X_PRECISION_VIRTUAL;
5143 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005144
5145 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005146 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005147
5148 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5149
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005150 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005151 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005152
Michael Wrightd02c5b62014-02-10 15:10:22 -08005153 enum Axes {
5154 POSITION = 1 << 0,
5155 TOUCH = 1 << 1,
5156 TOOL = 1 << 2,
5157 PRESSURE = 1 << 3,
5158 ORIENTATION = 1 << 4,
5159 MINOR = 1 << 5,
5160 ID = 1 << 6,
5161 DISTANCE = 1 << 7,
5162 TILT = 1 << 8,
5163 SLOT = 1 << 9,
5164 TOOL_TYPE = 1 << 10,
5165 };
5166
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005167 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5168 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005169 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005170 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005171 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005172 int32_t toRawX(float displayX);
5173 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005174 int32_t toRotatedRawX(float displayX);
5175 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005176 float toCookedX(float rawX, float rawY);
5177 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005178 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005179 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005180 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005181 float toDisplayY(int32_t rawY, int32_t displayHeight);
5182
Michael Wrightd02c5b62014-02-10 15:10:22 -08005183};
5184
5185const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5186const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5187const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5188const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5189const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5190const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5191const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5192const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005193const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5194const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005195const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5196const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5197const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5198const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5199const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5200const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5201const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5202const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5203const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5204const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5205const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5206const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005207const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5208 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5209const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5210 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005211const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5212 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005213
5214const float TouchInputMapperTest::GEOMETRIC_SCALE =
5215 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5216 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5217
5218const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5219 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5220 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5221};
5222
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005223void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005224 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5225 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005226}
5227
5228void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5229 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5230 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005231}
5232
Santos Cordonfa5cf462017-04-05 10:37:00 -07005233void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005234 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5235 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5236 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005237}
5238
Michael Wrightd02c5b62014-02-10 15:10:22 -08005239void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005240 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5241 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5242 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5243 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005244}
5245
Jason Gerecke489fda82012-09-07 17:19:40 -07005246void TouchInputMapperTest::prepareLocationCalibration() {
5247 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5248}
5249
Michael Wrightd02c5b62014-02-10 15:10:22 -08005250int32_t TouchInputMapperTest::toRawX(float displayX) {
5251 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5252}
5253
5254int32_t TouchInputMapperTest::toRawY(float displayY) {
5255 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5256}
5257
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005258int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5259 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5260}
5261
5262int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5263 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5264}
5265
Jason Gerecke489fda82012-09-07 17:19:40 -07005266float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5267 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5268 return rawX;
5269}
5270
5271float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5272 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5273 return rawY;
5274}
5275
Michael Wrightd02c5b62014-02-10 15:10:22 -08005276float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005277 return toDisplayX(rawX, DISPLAY_WIDTH);
5278}
5279
5280float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5281 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005282}
5283
5284float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005285 return toDisplayY(rawY, DISPLAY_HEIGHT);
5286}
5287
5288float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5289 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005290}
5291
5292
5293// --- SingleTouchInputMapperTest ---
5294
5295class SingleTouchInputMapperTest : public TouchInputMapperTest {
5296protected:
5297 void prepareButtons();
5298 void prepareAxes(int axes);
5299
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005300 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5301 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5302 void processUp(SingleTouchInputMapper& mappery);
5303 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5304 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5305 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5306 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5307 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5308 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005309};
5310
5311void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005312 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005313}
5314
5315void SingleTouchInputMapperTest::prepareAxes(int axes) {
5316 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005317 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5318 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005319 }
5320 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005321 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5322 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005323 }
5324 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005325 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5326 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005327 }
5328 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005329 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5330 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005331 }
5332 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005333 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5334 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005335 }
5336}
5337
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005338void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005339 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5340 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5341 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005342}
5343
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005344void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005345 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5346 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005347}
5348
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005349void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005350 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005351}
5352
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005353void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005354 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005355}
5356
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005357void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5358 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005359 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005360}
5361
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005362void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005363 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005364}
5365
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005366void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5367 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005368 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5369 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005370}
5371
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005372void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5373 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005374 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005375}
5376
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005377void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005378 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005379}
5380
Michael Wrightd02c5b62014-02-10 15:10:22 -08005381TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005382 prepareButtons();
5383 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005384 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005385
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005386 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005387}
5388
Michael Wrightd02c5b62014-02-10 15:10:22 -08005389TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005390 prepareButtons();
5391 prepareAxes(POSITION);
5392 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005393 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005394
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005395 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005396}
5397
5398TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005399 addConfigurationProperty("touch.deviceType", "touchScreen");
5400 prepareDisplay(DISPLAY_ORIENTATION_0);
5401 prepareButtons();
5402 prepareAxes(POSITION);
5403 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005404 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005405
5406 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005407 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005408
5409 // Virtual key is down.
5410 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5411 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5412 processDown(mapper, x, y);
5413 processSync(mapper);
5414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5415
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005416 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005417
5418 // Virtual key is up.
5419 processUp(mapper);
5420 processSync(mapper);
5421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5422
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005423 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005424}
5425
5426TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005427 addConfigurationProperty("touch.deviceType", "touchScreen");
5428 prepareDisplay(DISPLAY_ORIENTATION_0);
5429 prepareButtons();
5430 prepareAxes(POSITION);
5431 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005432 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005433
5434 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005435 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005436
5437 // Virtual key is down.
5438 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5439 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5440 processDown(mapper, x, y);
5441 processSync(mapper);
5442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5443
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005444 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005445
5446 // Virtual key is up.
5447 processUp(mapper);
5448 processSync(mapper);
5449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5450
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005451 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005452}
5453
5454TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005455 addConfigurationProperty("touch.deviceType", "touchScreen");
5456 prepareDisplay(DISPLAY_ORIENTATION_0);
5457 prepareButtons();
5458 prepareAxes(POSITION);
5459 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005460 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005461
Michael Wrightd02c5b62014-02-10 15:10:22 -08005462 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005463 ASSERT_TRUE(
5464 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005465 ASSERT_TRUE(flags[0]);
5466 ASSERT_FALSE(flags[1]);
5467}
5468
5469TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005470 addConfigurationProperty("touch.deviceType", "touchScreen");
5471 prepareDisplay(DISPLAY_ORIENTATION_0);
5472 prepareButtons();
5473 prepareAxes(POSITION);
5474 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005475 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005476
arthurhungdcef2dc2020-08-11 14:47:50 +08005477 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005478
5479 NotifyKeyArgs args;
5480
5481 // Press virtual key.
5482 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5483 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5484 processDown(mapper, x, y);
5485 processSync(mapper);
5486
5487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5488 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5489 ASSERT_EQ(DEVICE_ID, args.deviceId);
5490 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5491 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5492 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5493 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5494 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5495 ASSERT_EQ(KEY_HOME, args.scanCode);
5496 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5497 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5498
5499 // Release virtual key.
5500 processUp(mapper);
5501 processSync(mapper);
5502
5503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5504 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5505 ASSERT_EQ(DEVICE_ID, args.deviceId);
5506 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5507 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5508 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5509 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5510 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5511 ASSERT_EQ(KEY_HOME, args.scanCode);
5512 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5513 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5514
5515 // Should not have sent any motions.
5516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5517}
5518
5519TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005520 addConfigurationProperty("touch.deviceType", "touchScreen");
5521 prepareDisplay(DISPLAY_ORIENTATION_0);
5522 prepareButtons();
5523 prepareAxes(POSITION);
5524 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005525 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005526
arthurhungdcef2dc2020-08-11 14:47:50 +08005527 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005528
5529 NotifyKeyArgs keyArgs;
5530
5531 // Press virtual key.
5532 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5533 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5534 processDown(mapper, x, y);
5535 processSync(mapper);
5536
5537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5538 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5539 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5540 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5541 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5542 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5543 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5544 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5545 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5546 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5547 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5548
5549 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5550 // into the display area.
5551 y -= 100;
5552 processMove(mapper, x, y);
5553 processSync(mapper);
5554
5555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5556 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5557 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5558 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5559 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5560 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5561 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5562 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5563 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5564 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5565 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5566 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5567
5568 NotifyMotionArgs motionArgs;
5569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5570 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5571 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5572 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5573 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5574 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5575 ASSERT_EQ(0, motionArgs.flags);
5576 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5577 ASSERT_EQ(0, motionArgs.buttonState);
5578 ASSERT_EQ(0, motionArgs.edgeFlags);
5579 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5580 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5581 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5583 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5584 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5585 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5586 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5587
5588 // Keep moving out of bounds. Should generate a pointer move.
5589 y -= 50;
5590 processMove(mapper, x, y);
5591 processSync(mapper);
5592
5593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5594 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5595 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5596 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5597 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5598 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5599 ASSERT_EQ(0, motionArgs.flags);
5600 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5601 ASSERT_EQ(0, motionArgs.buttonState);
5602 ASSERT_EQ(0, motionArgs.edgeFlags);
5603 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5604 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5605 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5606 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5607 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5608 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5609 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5610 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5611
5612 // Release out of bounds. Should generate a pointer up.
5613 processUp(mapper);
5614 processSync(mapper);
5615
5616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5617 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5618 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5619 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5620 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5621 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5622 ASSERT_EQ(0, motionArgs.flags);
5623 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5624 ASSERT_EQ(0, motionArgs.buttonState);
5625 ASSERT_EQ(0, motionArgs.edgeFlags);
5626 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5627 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5628 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5629 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5630 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5631 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5632 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5633 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5634
5635 // Should not have sent any more keys or motions.
5636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5638}
5639
5640TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005641 addConfigurationProperty("touch.deviceType", "touchScreen");
5642 prepareDisplay(DISPLAY_ORIENTATION_0);
5643 prepareButtons();
5644 prepareAxes(POSITION);
5645 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005646 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005647
arthurhungdcef2dc2020-08-11 14:47:50 +08005648 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005649
5650 NotifyMotionArgs motionArgs;
5651
5652 // Initially go down out of bounds.
5653 int32_t x = -10;
5654 int32_t y = -10;
5655 processDown(mapper, x, y);
5656 processSync(mapper);
5657
5658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5659
5660 // Move into the display area. Should generate a pointer down.
5661 x = 50;
5662 y = 75;
5663 processMove(mapper, x, y);
5664 processSync(mapper);
5665
5666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5667 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5668 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5669 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5670 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5671 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5672 ASSERT_EQ(0, motionArgs.flags);
5673 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5674 ASSERT_EQ(0, motionArgs.buttonState);
5675 ASSERT_EQ(0, motionArgs.edgeFlags);
5676 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5677 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5678 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5679 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5680 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5681 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5682 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5683 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5684
5685 // Release. Should generate a pointer up.
5686 processUp(mapper);
5687 processSync(mapper);
5688
5689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5690 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5691 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5692 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5693 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5694 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5695 ASSERT_EQ(0, motionArgs.flags);
5696 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5697 ASSERT_EQ(0, motionArgs.buttonState);
5698 ASSERT_EQ(0, motionArgs.edgeFlags);
5699 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5700 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5701 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5703 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5704 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5705 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5706 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5707
5708 // Should not have sent any more keys or motions.
5709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5711}
5712
Santos Cordonfa5cf462017-04-05 10:37:00 -07005713TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005714 addConfigurationProperty("touch.deviceType", "touchScreen");
5715 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5716
5717 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5718 prepareButtons();
5719 prepareAxes(POSITION);
5720 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005721 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005722
arthurhungdcef2dc2020-08-11 14:47:50 +08005723 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005724
5725 NotifyMotionArgs motionArgs;
5726
5727 // Down.
5728 int32_t x = 100;
5729 int32_t y = 125;
5730 processDown(mapper, x, y);
5731 processSync(mapper);
5732
5733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5734 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5735 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5736 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5737 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5738 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5739 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5740 ASSERT_EQ(0, motionArgs.flags);
5741 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5742 ASSERT_EQ(0, motionArgs.buttonState);
5743 ASSERT_EQ(0, motionArgs.edgeFlags);
5744 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5745 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5746 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5747 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5748 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5749 1, 0, 0, 0, 0, 0, 0, 0));
5750 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5751 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5752 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5753
5754 // Move.
5755 x += 50;
5756 y += 75;
5757 processMove(mapper, x, y);
5758 processSync(mapper);
5759
5760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5761 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5762 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5763 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5764 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5765 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5766 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5767 ASSERT_EQ(0, motionArgs.flags);
5768 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5769 ASSERT_EQ(0, motionArgs.buttonState);
5770 ASSERT_EQ(0, motionArgs.edgeFlags);
5771 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5772 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5773 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5774 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5775 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5776 1, 0, 0, 0, 0, 0, 0, 0));
5777 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5778 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5779 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5780
5781 // Up.
5782 processUp(mapper);
5783 processSync(mapper);
5784
5785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5786 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5787 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5788 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5789 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5790 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5791 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5792 ASSERT_EQ(0, motionArgs.flags);
5793 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5794 ASSERT_EQ(0, motionArgs.buttonState);
5795 ASSERT_EQ(0, motionArgs.edgeFlags);
5796 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5797 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5798 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5799 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5800 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5801 1, 0, 0, 0, 0, 0, 0, 0));
5802 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5803 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5804 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5805
5806 // Should not have sent any more keys or motions.
5807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5809}
5810
Michael Wrightd02c5b62014-02-10 15:10:22 -08005811TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005812 addConfigurationProperty("touch.deviceType", "touchScreen");
5813 prepareDisplay(DISPLAY_ORIENTATION_0);
5814 prepareButtons();
5815 prepareAxes(POSITION);
5816 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005817 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005818
arthurhungdcef2dc2020-08-11 14:47:50 +08005819 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005820
5821 NotifyMotionArgs motionArgs;
5822
5823 // Down.
5824 int32_t x = 100;
5825 int32_t y = 125;
5826 processDown(mapper, x, y);
5827 processSync(mapper);
5828
5829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5830 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5831 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5832 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5833 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5834 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5835 ASSERT_EQ(0, motionArgs.flags);
5836 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5837 ASSERT_EQ(0, motionArgs.buttonState);
5838 ASSERT_EQ(0, motionArgs.edgeFlags);
5839 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5840 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5841 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5842 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5843 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5844 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5845 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5846 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5847
5848 // Move.
5849 x += 50;
5850 y += 75;
5851 processMove(mapper, x, y);
5852 processSync(mapper);
5853
5854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5855 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5856 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5857 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5858 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5859 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5860 ASSERT_EQ(0, motionArgs.flags);
5861 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5862 ASSERT_EQ(0, motionArgs.buttonState);
5863 ASSERT_EQ(0, motionArgs.edgeFlags);
5864 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5865 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5866 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5867 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5868 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5869 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5870 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5871 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5872
5873 // Up.
5874 processUp(mapper);
5875 processSync(mapper);
5876
5877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5878 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5879 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5880 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5881 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5882 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5883 ASSERT_EQ(0, motionArgs.flags);
5884 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5885 ASSERT_EQ(0, motionArgs.buttonState);
5886 ASSERT_EQ(0, motionArgs.edgeFlags);
5887 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5888 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5889 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5890 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5891 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5892 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5893 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5894 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5895
5896 // Should not have sent any more keys or motions.
5897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5899}
5900
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005901TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005902 addConfigurationProperty("touch.deviceType", "touchScreen");
5903 prepareButtons();
5904 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005905 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5906 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005907 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005908
5909 NotifyMotionArgs args;
5910
5911 // Rotation 90.
5912 prepareDisplay(DISPLAY_ORIENTATION_90);
5913 processDown(mapper, toRawX(50), toRawY(75));
5914 processSync(mapper);
5915
5916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5917 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5918 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5919
5920 processUp(mapper);
5921 processSync(mapper);
5922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5923}
5924
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005925TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005926 addConfigurationProperty("touch.deviceType", "touchScreen");
5927 prepareButtons();
5928 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005929 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5930 // orientation-aware are affected by display rotation.
5931 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005932 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005933
5934 NotifyMotionArgs args;
5935
5936 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005937 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005938 prepareDisplay(DISPLAY_ORIENTATION_0);
5939 processDown(mapper, toRawX(50), toRawY(75));
5940 processSync(mapper);
5941
5942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5943 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5944 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5945
5946 processUp(mapper);
5947 processSync(mapper);
5948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5949
5950 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005951 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005952 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005953 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005954 processSync(mapper);
5955
5956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5957 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5958 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5959
5960 processUp(mapper);
5961 processSync(mapper);
5962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5963
5964 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005965 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005966 prepareDisplay(DISPLAY_ORIENTATION_180);
5967 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5968 processSync(mapper);
5969
5970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5971 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5972 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5973
5974 processUp(mapper);
5975 processSync(mapper);
5976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5977
5978 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005979 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005980 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005981 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005982 processSync(mapper);
5983
5984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5985 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5986 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5987
5988 processUp(mapper);
5989 processSync(mapper);
5990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5991}
5992
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005993TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5994 addConfigurationProperty("touch.deviceType", "touchScreen");
5995 prepareButtons();
5996 prepareAxes(POSITION);
5997 addConfigurationProperty("touch.orientationAware", "1");
5998 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5999 clearViewports();
6000 prepareDisplay(DISPLAY_ORIENTATION_0);
6001 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6002 NotifyMotionArgs args;
6003
6004 // Orientation 0.
6005 processDown(mapper, toRawX(50), toRawY(75));
6006 processSync(mapper);
6007
6008 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6009 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6010 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6011
6012 processUp(mapper);
6013 processSync(mapper);
6014 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6015}
6016
6017TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6018 addConfigurationProperty("touch.deviceType", "touchScreen");
6019 prepareButtons();
6020 prepareAxes(POSITION);
6021 addConfigurationProperty("touch.orientationAware", "1");
6022 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6023 clearViewports();
6024 prepareDisplay(DISPLAY_ORIENTATION_0);
6025 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6026 NotifyMotionArgs args;
6027
6028 // Orientation 90.
6029 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6030 processSync(mapper);
6031
6032 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6033 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6034 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6035
6036 processUp(mapper);
6037 processSync(mapper);
6038 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6039}
6040
6041TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6042 addConfigurationProperty("touch.deviceType", "touchScreen");
6043 prepareButtons();
6044 prepareAxes(POSITION);
6045 addConfigurationProperty("touch.orientationAware", "1");
6046 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6047 clearViewports();
6048 prepareDisplay(DISPLAY_ORIENTATION_0);
6049 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6050 NotifyMotionArgs args;
6051
6052 // Orientation 180.
6053 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6054 processSync(mapper);
6055
6056 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6057 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6058 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6059
6060 processUp(mapper);
6061 processSync(mapper);
6062 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6063}
6064
6065TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6066 addConfigurationProperty("touch.deviceType", "touchScreen");
6067 prepareButtons();
6068 prepareAxes(POSITION);
6069 addConfigurationProperty("touch.orientationAware", "1");
6070 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6071 clearViewports();
6072 prepareDisplay(DISPLAY_ORIENTATION_0);
6073 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6074 NotifyMotionArgs args;
6075
6076 // Orientation 270.
6077 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6078 processSync(mapper);
6079
6080 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6081 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6082 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6083
6084 processUp(mapper);
6085 processSync(mapper);
6086 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6087}
6088
6089TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6090 addConfigurationProperty("touch.deviceType", "touchScreen");
6091 prepareButtons();
6092 prepareAxes(POSITION);
6093 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6094 // orientation-aware are affected by display rotation.
6095 addConfigurationProperty("touch.orientationAware", "0");
6096 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6097 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6098
6099 NotifyMotionArgs args;
6100
6101 // Orientation 90, Rotation 0.
6102 clearViewports();
6103 prepareDisplay(DISPLAY_ORIENTATION_0);
6104 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6105 processSync(mapper);
6106
6107 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6108 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6109 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6110
6111 processUp(mapper);
6112 processSync(mapper);
6113 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6114
6115 // Orientation 90, Rotation 90.
6116 clearViewports();
6117 prepareDisplay(DISPLAY_ORIENTATION_90);
6118 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6119 processSync(mapper);
6120
6121 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6122 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6123 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6124
6125 processUp(mapper);
6126 processSync(mapper);
6127 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6128
6129 // Orientation 90, Rotation 180.
6130 clearViewports();
6131 prepareDisplay(DISPLAY_ORIENTATION_180);
6132 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6133 processSync(mapper);
6134
6135 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6136 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6137 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6138
6139 processUp(mapper);
6140 processSync(mapper);
6141 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6142
6143 // Orientation 90, Rotation 270.
6144 clearViewports();
6145 prepareDisplay(DISPLAY_ORIENTATION_270);
6146 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6147 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6148 processSync(mapper);
6149
6150 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6151 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6152 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6153
6154 processUp(mapper);
6155 processSync(mapper);
6156 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6157}
6158
Michael Wrightd02c5b62014-02-10 15:10:22 -08006159TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006160 addConfigurationProperty("touch.deviceType", "touchScreen");
6161 prepareDisplay(DISPLAY_ORIENTATION_0);
6162 prepareButtons();
6163 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006164 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006165
6166 // These calculations are based on the input device calibration documentation.
6167 int32_t rawX = 100;
6168 int32_t rawY = 200;
6169 int32_t rawPressure = 10;
6170 int32_t rawToolMajor = 12;
6171 int32_t rawDistance = 2;
6172 int32_t rawTiltX = 30;
6173 int32_t rawTiltY = 110;
6174
6175 float x = toDisplayX(rawX);
6176 float y = toDisplayY(rawY);
6177 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6178 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6179 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6180 float distance = float(rawDistance);
6181
6182 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6183 float tiltScale = M_PI / 180;
6184 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6185 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6186 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6187 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6188
6189 processDown(mapper, rawX, rawY);
6190 processPressure(mapper, rawPressure);
6191 processToolMajor(mapper, rawToolMajor);
6192 processDistance(mapper, rawDistance);
6193 processTilt(mapper, rawTiltX, rawTiltY);
6194 processSync(mapper);
6195
6196 NotifyMotionArgs args;
6197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6198 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6199 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6200 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6201}
6202
Jason Gerecke489fda82012-09-07 17:19:40 -07006203TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006204 addConfigurationProperty("touch.deviceType", "touchScreen");
6205 prepareDisplay(DISPLAY_ORIENTATION_0);
6206 prepareLocationCalibration();
6207 prepareButtons();
6208 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006209 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006210
6211 int32_t rawX = 100;
6212 int32_t rawY = 200;
6213
6214 float x = toDisplayX(toCookedX(rawX, rawY));
6215 float y = toDisplayY(toCookedY(rawX, rawY));
6216
6217 processDown(mapper, rawX, rawY);
6218 processSync(mapper);
6219
6220 NotifyMotionArgs args;
6221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6222 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6223 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6224}
6225
Michael Wrightd02c5b62014-02-10 15:10:22 -08006226TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006227 addConfigurationProperty("touch.deviceType", "touchScreen");
6228 prepareDisplay(DISPLAY_ORIENTATION_0);
6229 prepareButtons();
6230 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006231 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006232
6233 NotifyMotionArgs motionArgs;
6234 NotifyKeyArgs keyArgs;
6235
6236 processDown(mapper, 100, 200);
6237 processSync(mapper);
6238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6239 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6240 ASSERT_EQ(0, motionArgs.buttonState);
6241
6242 // press BTN_LEFT, release BTN_LEFT
6243 processKey(mapper, BTN_LEFT, 1);
6244 processSync(mapper);
6245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6246 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6247 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6248
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6250 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6251 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6252
Michael Wrightd02c5b62014-02-10 15:10:22 -08006253 processKey(mapper, BTN_LEFT, 0);
6254 processSync(mapper);
6255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006256 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006257 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006258
6259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006260 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006261 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006262
6263 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6264 processKey(mapper, BTN_RIGHT, 1);
6265 processKey(mapper, BTN_MIDDLE, 1);
6266 processSync(mapper);
6267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6268 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6269 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6270 motionArgs.buttonState);
6271
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6273 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6274 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, 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_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6279 motionArgs.buttonState);
6280
Michael Wrightd02c5b62014-02-10 15:10:22 -08006281 processKey(mapper, BTN_RIGHT, 0);
6282 processSync(mapper);
6283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006284 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006285 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006286
6287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006288 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006289 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006290
6291 processKey(mapper, BTN_MIDDLE, 0);
6292 processSync(mapper);
6293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006294 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006295 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006296
6297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006298 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006299 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006300
6301 // press BTN_BACK, release BTN_BACK
6302 processKey(mapper, BTN_BACK, 1);
6303 processSync(mapper);
6304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6305 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6306 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006307
Michael Wrightd02c5b62014-02-10 15:10:22 -08006308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006309 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006310 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6311
6312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6313 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6314 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006315
6316 processKey(mapper, BTN_BACK, 0);
6317 processSync(mapper);
6318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006319 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006320 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006321
6322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006323 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006324 ASSERT_EQ(0, motionArgs.buttonState);
6325
Michael Wrightd02c5b62014-02-10 15:10:22 -08006326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6327 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6328 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6329
6330 // press BTN_SIDE, release BTN_SIDE
6331 processKey(mapper, BTN_SIDE, 1);
6332 processSync(mapper);
6333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6334 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6335 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006336
Michael Wrightd02c5b62014-02-10 15:10:22 -08006337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006338 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006339 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6340
6341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6342 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6343 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006344
6345 processKey(mapper, BTN_SIDE, 0);
6346 processSync(mapper);
6347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006348 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006349 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006350
6351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006352 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006353 ASSERT_EQ(0, motionArgs.buttonState);
6354
Michael Wrightd02c5b62014-02-10 15:10:22 -08006355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6356 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6357 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6358
6359 // press BTN_FORWARD, release BTN_FORWARD
6360 processKey(mapper, BTN_FORWARD, 1);
6361 processSync(mapper);
6362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6363 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6364 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006365
Michael Wrightd02c5b62014-02-10 15:10:22 -08006366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006367 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006368 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6369
6370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6371 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6372 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006373
6374 processKey(mapper, BTN_FORWARD, 0);
6375 processSync(mapper);
6376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006377 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006378 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006379
6380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006382 ASSERT_EQ(0, motionArgs.buttonState);
6383
Michael Wrightd02c5b62014-02-10 15:10:22 -08006384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6385 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6386 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6387
6388 // press BTN_EXTRA, release BTN_EXTRA
6389 processKey(mapper, BTN_EXTRA, 1);
6390 processSync(mapper);
6391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6392 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6393 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006394
Michael Wrightd02c5b62014-02-10 15:10:22 -08006395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006396 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006397 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6398
6399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6400 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6401 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006402
6403 processKey(mapper, BTN_EXTRA, 0);
6404 processSync(mapper);
6405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006406 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006407 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006408
6409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006411 ASSERT_EQ(0, motionArgs.buttonState);
6412
Michael Wrightd02c5b62014-02-10 15:10:22 -08006413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6414 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6415 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6416
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6418
Michael Wrightd02c5b62014-02-10 15:10:22 -08006419 // press BTN_STYLUS, release BTN_STYLUS
6420 processKey(mapper, BTN_STYLUS, 1);
6421 processSync(mapper);
6422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6423 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006424 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6425
6426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6427 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6428 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006429
6430 processKey(mapper, BTN_STYLUS, 0);
6431 processSync(mapper);
6432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006433 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006434 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006435
6436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006437 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006438 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006439
6440 // press BTN_STYLUS2, release BTN_STYLUS2
6441 processKey(mapper, BTN_STYLUS2, 1);
6442 processSync(mapper);
6443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6444 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006445 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6446
6447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6448 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6449 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006450
6451 processKey(mapper, BTN_STYLUS2, 0);
6452 processSync(mapper);
6453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006454 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006455 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006456
6457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006458 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006459 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006460
6461 // release touch
6462 processUp(mapper);
6463 processSync(mapper);
6464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6465 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6466 ASSERT_EQ(0, motionArgs.buttonState);
6467}
6468
6469TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006470 addConfigurationProperty("touch.deviceType", "touchScreen");
6471 prepareDisplay(DISPLAY_ORIENTATION_0);
6472 prepareButtons();
6473 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006474 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006475
6476 NotifyMotionArgs motionArgs;
6477
6478 // default tool type is finger
6479 processDown(mapper, 100, 200);
6480 processSync(mapper);
6481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6482 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6483 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6484
6485 // eraser
6486 processKey(mapper, BTN_TOOL_RUBBER, 1);
6487 processSync(mapper);
6488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6489 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6490 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6491
6492 // stylus
6493 processKey(mapper, BTN_TOOL_RUBBER, 0);
6494 processKey(mapper, BTN_TOOL_PEN, 1);
6495 processSync(mapper);
6496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6497 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6498 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6499
6500 // brush
6501 processKey(mapper, BTN_TOOL_PEN, 0);
6502 processKey(mapper, BTN_TOOL_BRUSH, 1);
6503 processSync(mapper);
6504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6505 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6506 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6507
6508 // pencil
6509 processKey(mapper, BTN_TOOL_BRUSH, 0);
6510 processKey(mapper, BTN_TOOL_PENCIL, 1);
6511 processSync(mapper);
6512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6513 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6514 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6515
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006516 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006517 processKey(mapper, BTN_TOOL_PENCIL, 0);
6518 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6519 processSync(mapper);
6520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6521 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6522 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6523
6524 // mouse
6525 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6526 processKey(mapper, BTN_TOOL_MOUSE, 1);
6527 processSync(mapper);
6528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6529 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6530 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6531
6532 // lens
6533 processKey(mapper, BTN_TOOL_MOUSE, 0);
6534 processKey(mapper, BTN_TOOL_LENS, 1);
6535 processSync(mapper);
6536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6537 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6538 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6539
6540 // double-tap
6541 processKey(mapper, BTN_TOOL_LENS, 0);
6542 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6543 processSync(mapper);
6544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6545 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6546 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6547
6548 // triple-tap
6549 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6550 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6551 processSync(mapper);
6552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6553 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6554 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6555
6556 // quad-tap
6557 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6558 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6559 processSync(mapper);
6560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6561 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6562 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6563
6564 // finger
6565 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6566 processKey(mapper, BTN_TOOL_FINGER, 1);
6567 processSync(mapper);
6568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6569 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6570 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6571
6572 // stylus trumps finger
6573 processKey(mapper, BTN_TOOL_PEN, 1);
6574 processSync(mapper);
6575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6576 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6577 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6578
6579 // eraser trumps stylus
6580 processKey(mapper, BTN_TOOL_RUBBER, 1);
6581 processSync(mapper);
6582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6583 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6584 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6585
6586 // mouse trumps eraser
6587 processKey(mapper, BTN_TOOL_MOUSE, 1);
6588 processSync(mapper);
6589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6590 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6591 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6592
6593 // back to default tool type
6594 processKey(mapper, BTN_TOOL_MOUSE, 0);
6595 processKey(mapper, BTN_TOOL_RUBBER, 0);
6596 processKey(mapper, BTN_TOOL_PEN, 0);
6597 processKey(mapper, BTN_TOOL_FINGER, 0);
6598 processSync(mapper);
6599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6600 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6601 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6602}
6603
6604TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006605 addConfigurationProperty("touch.deviceType", "touchScreen");
6606 prepareDisplay(DISPLAY_ORIENTATION_0);
6607 prepareButtons();
6608 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006609 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006610 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006611
6612 NotifyMotionArgs motionArgs;
6613
6614 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6615 processKey(mapper, BTN_TOOL_FINGER, 1);
6616 processMove(mapper, 100, 200);
6617 processSync(mapper);
6618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6619 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6621 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6622
6623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6624 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6625 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6626 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6627
6628 // move a little
6629 processMove(mapper, 150, 250);
6630 processSync(mapper);
6631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6632 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6633 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6634 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6635
6636 // down when BTN_TOUCH is pressed, pressure defaults to 1
6637 processKey(mapper, BTN_TOUCH, 1);
6638 processSync(mapper);
6639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6640 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6641 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6642 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6643
6644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6645 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6646 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6647 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6648
6649 // up when BTN_TOUCH is released, hover restored
6650 processKey(mapper, BTN_TOUCH, 0);
6651 processSync(mapper);
6652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6653 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6655 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6656
6657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6658 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6660 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6661
6662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6663 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6664 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6665 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6666
6667 // exit hover when pointer goes away
6668 processKey(mapper, BTN_TOOL_FINGER, 0);
6669 processSync(mapper);
6670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6671 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6672 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6673 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6674}
6675
6676TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006677 addConfigurationProperty("touch.deviceType", "touchScreen");
6678 prepareDisplay(DISPLAY_ORIENTATION_0);
6679 prepareButtons();
6680 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006681 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006682
6683 NotifyMotionArgs motionArgs;
6684
6685 // initially hovering because pressure is 0
6686 processDown(mapper, 100, 200);
6687 processPressure(mapper, 0);
6688 processSync(mapper);
6689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6690 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6691 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6692 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6693
6694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6695 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6697 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6698
6699 // move a little
6700 processMove(mapper, 150, 250);
6701 processSync(mapper);
6702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6703 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6704 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6705 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6706
6707 // down when pressure is non-zero
6708 processPressure(mapper, RAW_PRESSURE_MAX);
6709 processSync(mapper);
6710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6711 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6712 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6713 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6714
6715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6716 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6718 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6719
6720 // up when pressure becomes 0, hover restored
6721 processPressure(mapper, 0);
6722 processSync(mapper);
6723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6724 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6725 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6726 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6727
6728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6729 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6730 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6731 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6732
6733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6734 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6735 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6736 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6737
6738 // exit hover when pointer goes away
6739 processUp(mapper);
6740 processSync(mapper);
6741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6742 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6743 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6744 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6745}
6746
lilinnan687e58f2022-07-19 16:00:50 +08006747TEST_F(SingleTouchInputMapperTest,
6748 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6749 addConfigurationProperty("touch.deviceType", "touchScreen");
6750 prepareDisplay(DISPLAY_ORIENTATION_0);
6751 prepareButtons();
6752 prepareAxes(POSITION);
6753 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6754 NotifyMotionArgs motionArgs;
6755
6756 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006757 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006758 processSync(mapper);
6759
6760 // We should receive a down event
6761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6762 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6763
6764 // Change display id
6765 clearViewports();
6766 prepareSecondaryDisplay(ViewportType::INTERNAL);
6767
6768 // We should receive a cancel event
6769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6770 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6771 // Then receive reset called
6772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6773}
6774
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006775TEST_F(SingleTouchInputMapperTest,
6776 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6777 addConfigurationProperty("touch.deviceType", "touchScreen");
6778 prepareDisplay(DISPLAY_ORIENTATION_0);
6779 prepareButtons();
6780 prepareAxes(POSITION);
6781 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6783 NotifyMotionArgs motionArgs;
6784
6785 // Start a new gesture.
6786 processDown(mapper, 100, 200);
6787 processSync(mapper);
6788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6789 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6790
6791 // Make the viewport inactive. This will put the device in disabled mode.
6792 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6793 viewport->isActive = false;
6794 mFakePolicy->updateViewport(*viewport);
6795 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6796
6797 // We should receive a cancel event for the ongoing gesture.
6798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6799 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6800 // Then we should be notified that the device was reset.
6801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6802
6803 // No events are generated while the viewport is inactive.
6804 processMove(mapper, 101, 201);
6805 processSync(mapper);
6806 processDown(mapper, 102, 202);
6807 processSync(mapper);
6808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6809
6810 // Make the viewport active again. The device should resume processing events.
6811 viewport->isActive = true;
6812 mFakePolicy->updateViewport(*viewport);
6813 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6814
6815 // The device is reset because it changes back to direct mode, without generating any events.
6816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6818
6819 // Start a new gesture.
6820 processDown(mapper, 100, 200);
6821 processSync(mapper);
6822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6823 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6824
6825 // No more events.
6826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6828}
6829
Prabir Pradhan5632d622021-09-06 07:57:20 -07006830// --- TouchDisplayProjectionTest ---
6831
6832class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6833public:
6834 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6835 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6836 // rotated equivalent of the given un-rotated physical display bounds.
6837 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6838 uint32_t inverseRotationFlags;
6839 auto width = DISPLAY_WIDTH;
6840 auto height = DISPLAY_HEIGHT;
6841 switch (orientation) {
6842 case DISPLAY_ORIENTATION_90:
6843 inverseRotationFlags = ui::Transform::ROT_270;
6844 std::swap(width, height);
6845 break;
6846 case DISPLAY_ORIENTATION_180:
6847 inverseRotationFlags = ui::Transform::ROT_180;
6848 break;
6849 case DISPLAY_ORIENTATION_270:
6850 inverseRotationFlags = ui::Transform::ROT_90;
6851 std::swap(width, height);
6852 break;
6853 case DISPLAY_ORIENTATION_0:
6854 inverseRotationFlags = ui::Transform::ROT_0;
6855 break;
6856 default:
6857 FAIL() << "Invalid orientation: " << orientation;
6858 }
6859
6860 const ui::Transform rotation(inverseRotationFlags, width, height);
6861 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6862
6863 std::optional<DisplayViewport> internalViewport =
6864 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6865 DisplayViewport& v = *internalViewport;
6866 v.displayId = DISPLAY_ID;
6867 v.orientation = orientation;
6868
6869 v.logicalLeft = 0;
6870 v.logicalTop = 0;
6871 v.logicalRight = 100;
6872 v.logicalBottom = 100;
6873
6874 v.physicalLeft = rotatedPhysicalDisplay.left;
6875 v.physicalTop = rotatedPhysicalDisplay.top;
6876 v.physicalRight = rotatedPhysicalDisplay.right;
6877 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6878
6879 v.deviceWidth = width;
6880 v.deviceHeight = height;
6881
6882 v.isActive = true;
6883 v.uniqueId = UNIQUE_ID;
6884 v.type = ViewportType::INTERNAL;
6885 mFakePolicy->updateViewport(v);
6886 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6887 }
6888
6889 void assertReceivedMove(const Point& point) {
6890 NotifyMotionArgs motionArgs;
6891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6892 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6893 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6894 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6895 1, 0, 0, 0, 0, 0, 0, 0));
6896 }
6897};
6898
6899TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
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 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6914 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6915
6916 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6917 DISPLAY_ORIENTATION_270}) {
6918 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6919
6920 // Touches outside the physical display should be ignored, and should not generate any
6921 // events. Ensure touches at the following points that lie outside of the physical display
6922 // area do not generate any events.
6923 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6924 processDown(mapper, toRawX(point.x), toRawY(point.y));
6925 processSync(mapper);
6926 processUp(mapper);
6927 processSync(mapper);
6928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6929 << "Unexpected event generated for touch outside physical display at point: "
6930 << point.x << ", " << point.y;
6931 }
6932 }
6933}
6934
6935TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6936 addConfigurationProperty("touch.deviceType", "touchScreen");
6937 prepareDisplay(DISPLAY_ORIENTATION_0);
6938
6939 prepareButtons();
6940 prepareAxes(POSITION);
6941 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6942
6943 NotifyMotionArgs motionArgs;
6944
6945 // Configure the DisplayViewport such that the logical display maps to a subsection of
6946 // the display panel called the physical display. Here, the physical display is bounded by the
6947 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6948 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6949
6950 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6951 DISPLAY_ORIENTATION_270}) {
6952 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6953
6954 // Touches that start outside the physical display should be ignored until it enters the
6955 // physical display bounds, at which point it should generate a down event. Start a touch at
6956 // the point (5, 100), which is outside the physical display bounds.
6957 static const Point kOutsidePoint{5, 100};
6958 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6959 processSync(mapper);
6960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6961
6962 // Move the touch into the physical display area. This should generate a pointer down.
6963 processMove(mapper, toRawX(11), toRawY(21));
6964 processSync(mapper);
6965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6966 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6967 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6968 ASSERT_NO_FATAL_FAILURE(
6969 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6970
6971 // Move the touch inside the physical display area. This should generate a pointer move.
6972 processMove(mapper, toRawX(69), toRawY(159));
6973 processSync(mapper);
6974 assertReceivedMove({69, 159});
6975
6976 // Move outside the physical display area. Since the pointer is already down, this should
6977 // now continue generating events.
6978 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6979 processSync(mapper);
6980 assertReceivedMove(kOutsidePoint);
6981
6982 // Release. This should generate a pointer up.
6983 processUp(mapper);
6984 processSync(mapper);
6985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6986 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6987 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6988 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6989
6990 // Ensure no more events were generated.
6991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6993 }
6994}
6995
Michael Wrightd02c5b62014-02-10 15:10:22 -08006996// --- MultiTouchInputMapperTest ---
6997
6998class MultiTouchInputMapperTest : public TouchInputMapperTest {
6999protected:
7000 void prepareAxes(int axes);
7001
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007002 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7003 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7004 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7005 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7006 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7007 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7008 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7009 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7010 void processId(MultiTouchInputMapper& mapper, int32_t id);
7011 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7012 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7013 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
7014 void processMTSync(MultiTouchInputMapper& mapper);
7015 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007016};
7017
7018void MultiTouchInputMapperTest::prepareAxes(int axes) {
7019 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007020 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7021 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007022 }
7023 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007024 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7025 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007026 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007027 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7028 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007029 }
7030 }
7031 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007032 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7033 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007034 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007035 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007036 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007037 }
7038 }
7039 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007040 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7041 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007042 }
7043 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007044 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7045 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007046 }
7047 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007048 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7049 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007050 }
7051 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007052 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7053 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007054 }
7055 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007056 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7057 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007058 }
7059 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007060 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007061 }
7062}
7063
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007064void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7065 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007066 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7067 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007068}
7069
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007070void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7071 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007072 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007073}
7074
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007075void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7076 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007077 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007078}
7079
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007080void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007081 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007082}
7083
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007084void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007085 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007086}
7087
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007088void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7089 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007090 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007091}
7092
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007093void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007094 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007095}
7096
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007097void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007098 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007099}
7100
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007101void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007102 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007103}
7104
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007105void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007106 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007107}
7108
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007109void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007110 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007111}
7112
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007113void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7114 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007115 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007116}
7117
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007118void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007119 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007120}
7121
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007122void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007124}
7125
Michael Wrightd02c5b62014-02-10 15:10:22 -08007126TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007127 addConfigurationProperty("touch.deviceType", "touchScreen");
7128 prepareDisplay(DISPLAY_ORIENTATION_0);
7129 prepareAxes(POSITION);
7130 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007131 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007132
arthurhungdcef2dc2020-08-11 14:47:50 +08007133 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007134
7135 NotifyMotionArgs motionArgs;
7136
7137 // Two fingers down at once.
7138 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7139 processPosition(mapper, x1, y1);
7140 processMTSync(mapper);
7141 processPosition(mapper, x2, y2);
7142 processMTSync(mapper);
7143 processSync(mapper);
7144
7145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7146 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7147 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7148 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7149 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7150 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7151 ASSERT_EQ(0, motionArgs.flags);
7152 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7153 ASSERT_EQ(0, motionArgs.buttonState);
7154 ASSERT_EQ(0, motionArgs.edgeFlags);
7155 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7156 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7157 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7158 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7159 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7160 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7161 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7162 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7163
7164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7165 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7166 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7167 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7168 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007169 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007170 ASSERT_EQ(0, motionArgs.flags);
7171 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7172 ASSERT_EQ(0, motionArgs.buttonState);
7173 ASSERT_EQ(0, motionArgs.edgeFlags);
7174 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7175 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7176 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7177 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7178 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7180 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7181 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7182 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7183 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7184 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7185 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7186
7187 // Move.
7188 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7189 processPosition(mapper, x1, y1);
7190 processMTSync(mapper);
7191 processPosition(mapper, x2, y2);
7192 processMTSync(mapper);
7193 processSync(mapper);
7194
7195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7196 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7197 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7198 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7199 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7200 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7201 ASSERT_EQ(0, motionArgs.flags);
7202 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7203 ASSERT_EQ(0, motionArgs.buttonState);
7204 ASSERT_EQ(0, motionArgs.edgeFlags);
7205 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7206 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7207 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7208 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7209 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7210 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7211 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7212 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7213 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7214 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7215 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7216 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7217
7218 // First finger up.
7219 x2 += 15; y2 -= 20;
7220 processPosition(mapper, x2, y2);
7221 processMTSync(mapper);
7222 processSync(mapper);
7223
7224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7225 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7226 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7227 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7228 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007229 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007230 ASSERT_EQ(0, motionArgs.flags);
7231 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7232 ASSERT_EQ(0, motionArgs.buttonState);
7233 ASSERT_EQ(0, motionArgs.edgeFlags);
7234 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7235 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7236 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7237 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7238 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7239 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7240 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7242 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7243 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7244 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7245 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7246
7247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7248 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7249 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7250 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7251 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7252 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7253 ASSERT_EQ(0, motionArgs.flags);
7254 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7255 ASSERT_EQ(0, motionArgs.buttonState);
7256 ASSERT_EQ(0, motionArgs.edgeFlags);
7257 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7258 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7259 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7260 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7261 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7262 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7263 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7264 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7265
7266 // Move.
7267 x2 += 20; y2 -= 25;
7268 processPosition(mapper, x2, y2);
7269 processMTSync(mapper);
7270 processSync(mapper);
7271
7272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7273 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7274 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7275 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7276 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7277 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7278 ASSERT_EQ(0, motionArgs.flags);
7279 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7280 ASSERT_EQ(0, motionArgs.buttonState);
7281 ASSERT_EQ(0, motionArgs.edgeFlags);
7282 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7283 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7284 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7285 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7286 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7287 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7288 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7289 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7290
7291 // New finger down.
7292 int32_t x3 = 700, y3 = 300;
7293 processPosition(mapper, x2, y2);
7294 processMTSync(mapper);
7295 processPosition(mapper, x3, y3);
7296 processMTSync(mapper);
7297 processSync(mapper);
7298
7299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7300 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7301 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7302 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7303 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007304 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007305 ASSERT_EQ(0, motionArgs.flags);
7306 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7307 ASSERT_EQ(0, motionArgs.buttonState);
7308 ASSERT_EQ(0, motionArgs.edgeFlags);
7309 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7310 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7311 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7312 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7313 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7314 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7315 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7316 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7317 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7318 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7319 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7320 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7321
7322 // Second finger up.
7323 x3 += 30; y3 -= 20;
7324 processPosition(mapper, x3, y3);
7325 processMTSync(mapper);
7326 processSync(mapper);
7327
7328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7329 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7330 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7331 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7332 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007333 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007334 ASSERT_EQ(0, motionArgs.flags);
7335 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7336 ASSERT_EQ(0, motionArgs.buttonState);
7337 ASSERT_EQ(0, motionArgs.edgeFlags);
7338 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7339 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7340 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7341 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7342 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7343 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7344 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7345 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7346 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7347 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7348 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7349 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7350
7351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7352 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7353 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7354 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7355 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7356 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7357 ASSERT_EQ(0, motionArgs.flags);
7358 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7359 ASSERT_EQ(0, motionArgs.buttonState);
7360 ASSERT_EQ(0, motionArgs.edgeFlags);
7361 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7362 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7363 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7365 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7366 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7367 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7368 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7369
7370 // Last finger up.
7371 processMTSync(mapper);
7372 processSync(mapper);
7373
7374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7375 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7376 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7377 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7378 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7379 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7380 ASSERT_EQ(0, motionArgs.flags);
7381 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7382 ASSERT_EQ(0, motionArgs.buttonState);
7383 ASSERT_EQ(0, motionArgs.edgeFlags);
7384 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7385 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7386 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7387 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7388 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7389 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7390 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7391 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7392
7393 // Should not have sent any more keys or motions.
7394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7396}
7397
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007398TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7399 addConfigurationProperty("touch.deviceType", "touchScreen");
7400 prepareDisplay(DISPLAY_ORIENTATION_0);
7401
7402 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7403 /*fuzz*/ 0, /*resolution*/ 10);
7404 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7405 /*fuzz*/ 0, /*resolution*/ 11);
7406 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7407 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7408 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7409 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7410 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7411 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7412 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7413 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7414
7415 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7416
7417 // X and Y axes
7418 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7419 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7420 // Touch major and minor
7421 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7422 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7423 // Tool major and minor
7424 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7425 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7426}
7427
7428TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7429 addConfigurationProperty("touch.deviceType", "touchScreen");
7430 prepareDisplay(DISPLAY_ORIENTATION_0);
7431
7432 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7433 /*fuzz*/ 0, /*resolution*/ 10);
7434 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7435 /*fuzz*/ 0, /*resolution*/ 11);
7436
7437 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7438
7439 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7440
7441 // Touch major and minor
7442 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7443 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7444 // Tool major and minor
7445 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7446 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7447}
7448
Michael Wrightd02c5b62014-02-10 15:10:22 -08007449TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007450 addConfigurationProperty("touch.deviceType", "touchScreen");
7451 prepareDisplay(DISPLAY_ORIENTATION_0);
7452 prepareAxes(POSITION | ID);
7453 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007454 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007455
arthurhungdcef2dc2020-08-11 14:47:50 +08007456 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007457
7458 NotifyMotionArgs motionArgs;
7459
7460 // Two fingers down at once.
7461 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7462 processPosition(mapper, x1, y1);
7463 processId(mapper, 1);
7464 processMTSync(mapper);
7465 processPosition(mapper, x2, y2);
7466 processId(mapper, 2);
7467 processMTSync(mapper);
7468 processSync(mapper);
7469
7470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7471 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7472 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7473 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7474 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7476 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7477
7478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007479 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007480 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7481 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7482 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7483 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7484 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7486 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7488 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7489
7490 // Move.
7491 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7492 processPosition(mapper, x1, y1);
7493 processId(mapper, 1);
7494 processMTSync(mapper);
7495 processPosition(mapper, x2, y2);
7496 processId(mapper, 2);
7497 processMTSync(mapper);
7498 processSync(mapper);
7499
7500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7501 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7502 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7503 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7504 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7505 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7506 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7508 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7510 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7511
7512 // First finger up.
7513 x2 += 15; y2 -= 20;
7514 processPosition(mapper, x2, y2);
7515 processId(mapper, 2);
7516 processMTSync(mapper);
7517 processSync(mapper);
7518
7519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007520 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007521 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7522 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7523 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7524 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7525 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7526 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7527 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7529 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7530
7531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7532 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7533 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7534 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7535 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7536 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7537 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7538
7539 // Move.
7540 x2 += 20; y2 -= 25;
7541 processPosition(mapper, x2, y2);
7542 processId(mapper, 2);
7543 processMTSync(mapper);
7544 processSync(mapper);
7545
7546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7547 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7548 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7549 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7550 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7551 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7552 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7553
7554 // New finger down.
7555 int32_t x3 = 700, y3 = 300;
7556 processPosition(mapper, x2, y2);
7557 processId(mapper, 2);
7558 processMTSync(mapper);
7559 processPosition(mapper, x3, y3);
7560 processId(mapper, 3);
7561 processMTSync(mapper);
7562 processSync(mapper);
7563
7564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007565 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007566 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7567 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7568 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7569 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7570 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7572 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7574 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7575
7576 // Second finger up.
7577 x3 += 30; y3 -= 20;
7578 processPosition(mapper, x3, y3);
7579 processId(mapper, 3);
7580 processMTSync(mapper);
7581 processSync(mapper);
7582
7583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007584 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007585 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7586 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7587 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7588 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7589 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7590 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7591 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7593 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7594
7595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7596 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7597 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7598 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7599 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7601 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7602
7603 // Last finger up.
7604 processMTSync(mapper);
7605 processSync(mapper);
7606
7607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7608 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7609 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7610 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7611 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7613 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7614
7615 // Should not have sent any more keys or motions.
7616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7618}
7619
7620TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007621 addConfigurationProperty("touch.deviceType", "touchScreen");
7622 prepareDisplay(DISPLAY_ORIENTATION_0);
7623 prepareAxes(POSITION | ID | SLOT);
7624 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007625 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007626
arthurhungdcef2dc2020-08-11 14:47:50 +08007627 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007628
7629 NotifyMotionArgs motionArgs;
7630
7631 // Two fingers down at once.
7632 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7633 processPosition(mapper, x1, y1);
7634 processId(mapper, 1);
7635 processSlot(mapper, 1);
7636 processPosition(mapper, x2, y2);
7637 processId(mapper, 2);
7638 processSync(mapper);
7639
7640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7641 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7642 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7643 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7644 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7645 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7646 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7647
7648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007649 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007650 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7651 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7652 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7653 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7654 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7655 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7656 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7657 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7658 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7659
7660 // Move.
7661 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7662 processSlot(mapper, 0);
7663 processPosition(mapper, x1, y1);
7664 processSlot(mapper, 1);
7665 processPosition(mapper, x2, y2);
7666 processSync(mapper);
7667
7668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7669 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7670 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7671 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7672 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7673 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7674 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7676 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7678 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7679
7680 // First finger up.
7681 x2 += 15; y2 -= 20;
7682 processSlot(mapper, 0);
7683 processId(mapper, -1);
7684 processSlot(mapper, 1);
7685 processPosition(mapper, x2, y2);
7686 processSync(mapper);
7687
7688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007689 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007690 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7691 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7692 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7693 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7694 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7695 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7696 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7697 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7698 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7699
7700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7701 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7702 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7703 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7704 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7705 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7706 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7707
7708 // Move.
7709 x2 += 20; y2 -= 25;
7710 processPosition(mapper, x2, y2);
7711 processSync(mapper);
7712
7713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7714 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7715 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7716 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7717 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7719 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7720
7721 // New finger down.
7722 int32_t x3 = 700, y3 = 300;
7723 processPosition(mapper, x2, y2);
7724 processSlot(mapper, 0);
7725 processId(mapper, 3);
7726 processPosition(mapper, x3, y3);
7727 processSync(mapper);
7728
7729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007730 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007731 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7732 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7733 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7734 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7735 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7737 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7738 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7739 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7740
7741 // Second finger up.
7742 x3 += 30; y3 -= 20;
7743 processSlot(mapper, 1);
7744 processId(mapper, -1);
7745 processSlot(mapper, 0);
7746 processPosition(mapper, x3, y3);
7747 processSync(mapper);
7748
7749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007750 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007751 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7752 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7753 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7754 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7755 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7756 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7757 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7758 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7759 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7760
7761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7762 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7763 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7764 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7765 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7767 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7768
7769 // Last finger up.
7770 processId(mapper, -1);
7771 processSync(mapper);
7772
7773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7774 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7775 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7776 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7777 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7778 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7779 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7780
7781 // Should not have sent any more keys or motions.
7782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7784}
7785
7786TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007787 addConfigurationProperty("touch.deviceType", "touchScreen");
7788 prepareDisplay(DISPLAY_ORIENTATION_0);
7789 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007790 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007791
7792 // These calculations are based on the input device calibration documentation.
7793 int32_t rawX = 100;
7794 int32_t rawY = 200;
7795 int32_t rawTouchMajor = 7;
7796 int32_t rawTouchMinor = 6;
7797 int32_t rawToolMajor = 9;
7798 int32_t rawToolMinor = 8;
7799 int32_t rawPressure = 11;
7800 int32_t rawDistance = 0;
7801 int32_t rawOrientation = 3;
7802 int32_t id = 5;
7803
7804 float x = toDisplayX(rawX);
7805 float y = toDisplayY(rawY);
7806 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7807 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7808 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7809 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7810 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7811 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7812 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7813 float distance = float(rawDistance);
7814
7815 processPosition(mapper, rawX, rawY);
7816 processTouchMajor(mapper, rawTouchMajor);
7817 processTouchMinor(mapper, rawTouchMinor);
7818 processToolMajor(mapper, rawToolMajor);
7819 processToolMinor(mapper, rawToolMinor);
7820 processPressure(mapper, rawPressure);
7821 processOrientation(mapper, rawOrientation);
7822 processDistance(mapper, rawDistance);
7823 processId(mapper, id);
7824 processMTSync(mapper);
7825 processSync(mapper);
7826
7827 NotifyMotionArgs args;
7828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7829 ASSERT_EQ(0, args.pointerProperties[0].id);
7830 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7831 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7832 orientation, distance));
7833}
7834
7835TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007836 addConfigurationProperty("touch.deviceType", "touchScreen");
7837 prepareDisplay(DISPLAY_ORIENTATION_0);
7838 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7839 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007840 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007841
7842 // These calculations are based on the input device calibration documentation.
7843 int32_t rawX = 100;
7844 int32_t rawY = 200;
7845 int32_t rawTouchMajor = 140;
7846 int32_t rawTouchMinor = 120;
7847 int32_t rawToolMajor = 180;
7848 int32_t rawToolMinor = 160;
7849
7850 float x = toDisplayX(rawX);
7851 float y = toDisplayY(rawY);
7852 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7853 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7854 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7855 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7856 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7857
7858 processPosition(mapper, rawX, rawY);
7859 processTouchMajor(mapper, rawTouchMajor);
7860 processTouchMinor(mapper, rawTouchMinor);
7861 processToolMajor(mapper, rawToolMajor);
7862 processToolMinor(mapper, rawToolMinor);
7863 processMTSync(mapper);
7864 processSync(mapper);
7865
7866 NotifyMotionArgs args;
7867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7868 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7869 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7870}
7871
7872TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007873 addConfigurationProperty("touch.deviceType", "touchScreen");
7874 prepareDisplay(DISPLAY_ORIENTATION_0);
7875 prepareAxes(POSITION | TOUCH | TOOL);
7876 addConfigurationProperty("touch.size.calibration", "diameter");
7877 addConfigurationProperty("touch.size.scale", "10");
7878 addConfigurationProperty("touch.size.bias", "160");
7879 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007880 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007881
7882 // These calculations are based on the input device calibration documentation.
7883 // Note: We only provide a single common touch/tool value because the device is assumed
7884 // not to emit separate values for each pointer (isSummed = 1).
7885 int32_t rawX = 100;
7886 int32_t rawY = 200;
7887 int32_t rawX2 = 150;
7888 int32_t rawY2 = 250;
7889 int32_t rawTouchMajor = 5;
7890 int32_t rawToolMajor = 8;
7891
7892 float x = toDisplayX(rawX);
7893 float y = toDisplayY(rawY);
7894 float x2 = toDisplayX(rawX2);
7895 float y2 = toDisplayY(rawY2);
7896 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7897 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7898 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7899
7900 processPosition(mapper, rawX, rawY);
7901 processTouchMajor(mapper, rawTouchMajor);
7902 processToolMajor(mapper, rawToolMajor);
7903 processMTSync(mapper);
7904 processPosition(mapper, rawX2, rawY2);
7905 processTouchMajor(mapper, rawTouchMajor);
7906 processToolMajor(mapper, rawToolMajor);
7907 processMTSync(mapper);
7908 processSync(mapper);
7909
7910 NotifyMotionArgs args;
7911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7912 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7913
7914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007915 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007916 ASSERT_EQ(size_t(2), args.pointerCount);
7917 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7918 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7920 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7921}
7922
7923TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007924 addConfigurationProperty("touch.deviceType", "touchScreen");
7925 prepareDisplay(DISPLAY_ORIENTATION_0);
7926 prepareAxes(POSITION | TOUCH | TOOL);
7927 addConfigurationProperty("touch.size.calibration", "area");
7928 addConfigurationProperty("touch.size.scale", "43");
7929 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007930 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007931
7932 // These calculations are based on the input device calibration documentation.
7933 int32_t rawX = 100;
7934 int32_t rawY = 200;
7935 int32_t rawTouchMajor = 5;
7936 int32_t rawToolMajor = 8;
7937
7938 float x = toDisplayX(rawX);
7939 float y = toDisplayY(rawY);
7940 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7941 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7942 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7943
7944 processPosition(mapper, rawX, rawY);
7945 processTouchMajor(mapper, rawTouchMajor);
7946 processToolMajor(mapper, rawToolMajor);
7947 processMTSync(mapper);
7948 processSync(mapper);
7949
7950 NotifyMotionArgs args;
7951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7952 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7953 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7954}
7955
7956TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007957 addConfigurationProperty("touch.deviceType", "touchScreen");
7958 prepareDisplay(DISPLAY_ORIENTATION_0);
7959 prepareAxes(POSITION | PRESSURE);
7960 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7961 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007962 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007963
Michael Wrightaa449c92017-12-13 21:21:43 +00007964 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007965 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007966 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7967 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7968 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7969
Michael Wrightd02c5b62014-02-10 15:10:22 -08007970 // These calculations are based on the input device calibration documentation.
7971 int32_t rawX = 100;
7972 int32_t rawY = 200;
7973 int32_t rawPressure = 60;
7974
7975 float x = toDisplayX(rawX);
7976 float y = toDisplayY(rawY);
7977 float pressure = float(rawPressure) * 0.01f;
7978
7979 processPosition(mapper, rawX, rawY);
7980 processPressure(mapper, rawPressure);
7981 processMTSync(mapper);
7982 processSync(mapper);
7983
7984 NotifyMotionArgs args;
7985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7986 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7987 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7988}
7989
7990TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007991 addConfigurationProperty("touch.deviceType", "touchScreen");
7992 prepareDisplay(DISPLAY_ORIENTATION_0);
7993 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007994 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007995
7996 NotifyMotionArgs motionArgs;
7997 NotifyKeyArgs keyArgs;
7998
7999 processId(mapper, 1);
8000 processPosition(mapper, 100, 200);
8001 processSync(mapper);
8002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8003 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8004 ASSERT_EQ(0, motionArgs.buttonState);
8005
8006 // press BTN_LEFT, release BTN_LEFT
8007 processKey(mapper, BTN_LEFT, 1);
8008 processSync(mapper);
8009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8010 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8011 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8012
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8014 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8015 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8016
Michael Wrightd02c5b62014-02-10 15:10:22 -08008017 processKey(mapper, BTN_LEFT, 0);
8018 processSync(mapper);
8019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008020 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008021 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008022
8023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008024 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008025 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008026
8027 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8028 processKey(mapper, BTN_RIGHT, 1);
8029 processKey(mapper, BTN_MIDDLE, 1);
8030 processSync(mapper);
8031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8032 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8033 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8034 motionArgs.buttonState);
8035
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8037 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8038 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, 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_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8043 motionArgs.buttonState);
8044
Michael Wrightd02c5b62014-02-10 15:10:22 -08008045 processKey(mapper, BTN_RIGHT, 0);
8046 processSync(mapper);
8047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008048 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008049 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008050
8051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008052 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008053 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008054
8055 processKey(mapper, BTN_MIDDLE, 0);
8056 processSync(mapper);
8057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008058 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008059 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008060
8061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008062 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008063 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008064
8065 // press BTN_BACK, release BTN_BACK
8066 processKey(mapper, BTN_BACK, 1);
8067 processSync(mapper);
8068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8069 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8070 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008071
Michael Wrightd02c5b62014-02-10 15:10:22 -08008072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008073 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008074 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8075
8076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8077 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8078 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008079
8080 processKey(mapper, BTN_BACK, 0);
8081 processSync(mapper);
8082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008083 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008084 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008085
8086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008087 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008088 ASSERT_EQ(0, motionArgs.buttonState);
8089
Michael Wrightd02c5b62014-02-10 15:10:22 -08008090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8091 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8092 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8093
8094 // press BTN_SIDE, release BTN_SIDE
8095 processKey(mapper, BTN_SIDE, 1);
8096 processSync(mapper);
8097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8098 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8099 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008100
Michael Wrightd02c5b62014-02-10 15:10:22 -08008101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008102 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008103 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8104
8105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8106 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8107 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008108
8109 processKey(mapper, BTN_SIDE, 0);
8110 processSync(mapper);
8111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008112 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008113 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008114
8115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008116 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008117 ASSERT_EQ(0, motionArgs.buttonState);
8118
Michael Wrightd02c5b62014-02-10 15:10:22 -08008119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8120 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8121 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8122
8123 // press BTN_FORWARD, release BTN_FORWARD
8124 processKey(mapper, BTN_FORWARD, 1);
8125 processSync(mapper);
8126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8127 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8128 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008129
Michael Wrightd02c5b62014-02-10 15:10:22 -08008130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008131 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008132 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8133
8134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8135 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8136 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008137
8138 processKey(mapper, BTN_FORWARD, 0);
8139 processSync(mapper);
8140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008141 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008142 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008143
8144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008145 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008146 ASSERT_EQ(0, motionArgs.buttonState);
8147
Michael Wrightd02c5b62014-02-10 15:10:22 -08008148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8149 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8150 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8151
8152 // press BTN_EXTRA, release BTN_EXTRA
8153 processKey(mapper, BTN_EXTRA, 1);
8154 processSync(mapper);
8155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8156 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8157 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008158
Michael Wrightd02c5b62014-02-10 15:10:22 -08008159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008160 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008161 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8162
8163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8164 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8165 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008166
8167 processKey(mapper, BTN_EXTRA, 0);
8168 processSync(mapper);
8169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008170 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008171 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008172
8173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008174 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008175 ASSERT_EQ(0, motionArgs.buttonState);
8176
Michael Wrightd02c5b62014-02-10 15:10:22 -08008177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8178 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8179 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8180
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8182
Michael Wrightd02c5b62014-02-10 15:10:22 -08008183 // press BTN_STYLUS, release BTN_STYLUS
8184 processKey(mapper, BTN_STYLUS, 1);
8185 processSync(mapper);
8186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8187 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008188 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8189
8190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8191 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8192 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008193
8194 processKey(mapper, BTN_STYLUS, 0);
8195 processSync(mapper);
8196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008197 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008198 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008199
8200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008201 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008202 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008203
8204 // press BTN_STYLUS2, release BTN_STYLUS2
8205 processKey(mapper, BTN_STYLUS2, 1);
8206 processSync(mapper);
8207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8208 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008209 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8210
8211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8212 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8213 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008214
8215 processKey(mapper, BTN_STYLUS2, 0);
8216 processSync(mapper);
8217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008218 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008219 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008220
8221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008222 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008223 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008224
8225 // release touch
8226 processId(mapper, -1);
8227 processSync(mapper);
8228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8229 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8230 ASSERT_EQ(0, motionArgs.buttonState);
8231}
8232
8233TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008234 addConfigurationProperty("touch.deviceType", "touchScreen");
8235 prepareDisplay(DISPLAY_ORIENTATION_0);
8236 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008237 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008238
8239 NotifyMotionArgs motionArgs;
8240
8241 // default tool type is finger
8242 processId(mapper, 1);
8243 processPosition(mapper, 100, 200);
8244 processSync(mapper);
8245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8246 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8247 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8248
8249 // eraser
8250 processKey(mapper, BTN_TOOL_RUBBER, 1);
8251 processSync(mapper);
8252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8253 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8254 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8255
8256 // stylus
8257 processKey(mapper, BTN_TOOL_RUBBER, 0);
8258 processKey(mapper, BTN_TOOL_PEN, 1);
8259 processSync(mapper);
8260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8262 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8263
8264 // brush
8265 processKey(mapper, BTN_TOOL_PEN, 0);
8266 processKey(mapper, BTN_TOOL_BRUSH, 1);
8267 processSync(mapper);
8268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8269 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8270 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8271
8272 // pencil
8273 processKey(mapper, BTN_TOOL_BRUSH, 0);
8274 processKey(mapper, BTN_TOOL_PENCIL, 1);
8275 processSync(mapper);
8276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8277 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8278 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8279
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008280 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008281 processKey(mapper, BTN_TOOL_PENCIL, 0);
8282 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8283 processSync(mapper);
8284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8285 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8286 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8287
8288 // mouse
8289 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8290 processKey(mapper, BTN_TOOL_MOUSE, 1);
8291 processSync(mapper);
8292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8293 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8294 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8295
8296 // lens
8297 processKey(mapper, BTN_TOOL_MOUSE, 0);
8298 processKey(mapper, BTN_TOOL_LENS, 1);
8299 processSync(mapper);
8300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8301 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8302 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8303
8304 // double-tap
8305 processKey(mapper, BTN_TOOL_LENS, 0);
8306 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8307 processSync(mapper);
8308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8309 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8310 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8311
8312 // triple-tap
8313 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8314 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8315 processSync(mapper);
8316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8317 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8318 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8319
8320 // quad-tap
8321 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8322 processKey(mapper, BTN_TOOL_QUADTAP, 1);
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 // finger
8329 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8330 processKey(mapper, BTN_TOOL_FINGER, 1);
8331 processSync(mapper);
8332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8334 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8335
8336 // stylus trumps finger
8337 processKey(mapper, BTN_TOOL_PEN, 1);
8338 processSync(mapper);
8339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8340 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8341 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8342
8343 // eraser trumps stylus
8344 processKey(mapper, BTN_TOOL_RUBBER, 1);
8345 processSync(mapper);
8346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8347 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8348 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8349
8350 // mouse trumps eraser
8351 processKey(mapper, BTN_TOOL_MOUSE, 1);
8352 processSync(mapper);
8353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8355 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8356
8357 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8358 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8359 processSync(mapper);
8360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8361 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8362 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8363
8364 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8365 processToolType(mapper, MT_TOOL_PEN);
8366 processSync(mapper);
8367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8368 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8369 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8370
8371 // back to default tool type
8372 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8373 processKey(mapper, BTN_TOOL_MOUSE, 0);
8374 processKey(mapper, BTN_TOOL_RUBBER, 0);
8375 processKey(mapper, BTN_TOOL_PEN, 0);
8376 processKey(mapper, BTN_TOOL_FINGER, 0);
8377 processSync(mapper);
8378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8379 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8380 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8381}
8382
8383TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008384 addConfigurationProperty("touch.deviceType", "touchScreen");
8385 prepareDisplay(DISPLAY_ORIENTATION_0);
8386 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008387 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008388 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008389
8390 NotifyMotionArgs motionArgs;
8391
8392 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8393 processId(mapper, 1);
8394 processPosition(mapper, 100, 200);
8395 processSync(mapper);
8396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8397 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8398 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8399 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8400
8401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8402 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8403 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8404 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8405
8406 // move a little
8407 processPosition(mapper, 150, 250);
8408 processSync(mapper);
8409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8410 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8412 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8413
8414 // down when BTN_TOUCH is pressed, pressure defaults to 1
8415 processKey(mapper, BTN_TOUCH, 1);
8416 processSync(mapper);
8417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8418 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8419 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8420 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8421
8422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8423 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8425 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8426
8427 // up when BTN_TOUCH is released, hover restored
8428 processKey(mapper, BTN_TOUCH, 0);
8429 processSync(mapper);
8430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8431 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8433 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8434
8435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8436 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8437 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8438 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8439
8440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8441 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8443 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8444
8445 // exit hover when pointer goes away
8446 processId(mapper, -1);
8447 processSync(mapper);
8448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8449 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8450 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8451 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8452}
8453
8454TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008455 addConfigurationProperty("touch.deviceType", "touchScreen");
8456 prepareDisplay(DISPLAY_ORIENTATION_0);
8457 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008458 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008459
8460 NotifyMotionArgs motionArgs;
8461
8462 // initially hovering because pressure is 0
8463 processId(mapper, 1);
8464 processPosition(mapper, 100, 200);
8465 processPressure(mapper, 0);
8466 processSync(mapper);
8467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8468 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8469 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8470 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8471
8472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8473 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8475 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8476
8477 // move a little
8478 processPosition(mapper, 150, 250);
8479 processSync(mapper);
8480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8481 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8482 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8483 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8484
8485 // down when pressure becomes non-zero
8486 processPressure(mapper, RAW_PRESSURE_MAX);
8487 processSync(mapper);
8488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8489 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8490 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8491 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8492
8493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8494 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8496 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8497
8498 // up when pressure becomes 0, hover restored
8499 processPressure(mapper, 0);
8500 processSync(mapper);
8501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8502 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8503 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8504 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8505
8506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8507 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8509 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8510
8511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8512 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8513 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8514 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8515
8516 // exit hover when pointer goes away
8517 processId(mapper, -1);
8518 processSync(mapper);
8519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8520 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8521 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8522 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8523}
8524
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008525/**
8526 * Set the input device port <--> display port associations, and check that the
8527 * events are routed to the display that matches the display port.
8528 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8529 */
8530TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008531 const std::string usb2 = "USB2";
8532 const uint8_t hdmi1 = 0;
8533 const uint8_t hdmi2 = 1;
8534 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008535 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008536
8537 addConfigurationProperty("touch.deviceType", "touchScreen");
8538 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008539 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008540
8541 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8542 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8543
8544 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8545 // for this input device is specified, and the matching viewport is not present,
8546 // the input device should be disabled (at the mapper level).
8547
8548 // Add viewport for display 2 on hdmi2
8549 prepareSecondaryDisplay(type, hdmi2);
8550 // Send a touch event
8551 processPosition(mapper, 100, 100);
8552 processSync(mapper);
8553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8554
8555 // Add viewport for display 1 on hdmi1
8556 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8557 // Send a touch event again
8558 processPosition(mapper, 100, 100);
8559 processSync(mapper);
8560
8561 NotifyMotionArgs args;
8562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8563 ASSERT_EQ(DISPLAY_ID, args.displayId);
8564}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008565
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008566TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8567 addConfigurationProperty("touch.deviceType", "touchScreen");
8568 prepareAxes(POSITION);
8569 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8570
8571 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8572
8573 prepareDisplay(DISPLAY_ORIENTATION_0);
8574 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8575
8576 // Send a touch event
8577 processPosition(mapper, 100, 100);
8578 processSync(mapper);
8579
8580 NotifyMotionArgs args;
8581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8582 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8583}
8584
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008585TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008586 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008587 std::shared_ptr<FakePointerController> fakePointerController =
8588 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008589 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008590 fakePointerController->setPosition(100, 200);
8591 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008592 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008593
Garfield Tan888a6a42020-01-09 11:39:16 -08008594 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008595 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008596
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008597 prepareDisplay(DISPLAY_ORIENTATION_0);
8598 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008599 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008600
8601 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008602 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008603
8604 NotifyMotionArgs motionArgs;
8605 processPosition(mapper, 100, 100);
8606 processSync(mapper);
8607
8608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8609 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8610 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8611}
8612
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008613/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008614 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8615 */
8616TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8617 addConfigurationProperty("touch.deviceType", "touchScreen");
8618 prepareAxes(POSITION);
8619 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8620
8621 prepareDisplay(DISPLAY_ORIENTATION_0);
8622 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8623 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8624 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8625 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8626
8627 NotifyMotionArgs args;
8628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8629 ASSERT_EQ(26, args.readTime);
8630
8631 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8632 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8633 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8634
8635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8636 ASSERT_EQ(33, args.readTime);
8637}
8638
8639/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008640 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8641 * events should not be delivered to the listener.
8642 */
8643TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8644 addConfigurationProperty("touch.deviceType", "touchScreen");
8645 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8646 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8647 ViewportType::INTERNAL);
8648 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8649 prepareAxes(POSITION);
8650 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8651
8652 NotifyMotionArgs motionArgs;
8653 processPosition(mapper, 100, 100);
8654 processSync(mapper);
8655
8656 mFakeListener->assertNotifyMotionWasNotCalled();
8657}
8658
Garfield Tanc734e4f2021-01-15 20:01:39 -08008659TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8660 addConfigurationProperty("touch.deviceType", "touchScreen");
8661 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8662 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8663 ViewportType::INTERNAL);
8664 std::optional<DisplayViewport> optionalDisplayViewport =
8665 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8666 ASSERT_TRUE(optionalDisplayViewport.has_value());
8667 DisplayViewport displayViewport = *optionalDisplayViewport;
8668
8669 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8670 prepareAxes(POSITION);
8671 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8672
8673 // Finger down
8674 int32_t x = 100, y = 100;
8675 processPosition(mapper, x, y);
8676 processSync(mapper);
8677
8678 NotifyMotionArgs motionArgs;
8679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8680 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8681
8682 // Deactivate display viewport
8683 displayViewport.isActive = false;
8684 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8685 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8686
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008687 // The ongoing touch should be canceled immediately
8688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8689 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8690
8691 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008692 x += 10, y += 10;
8693 processPosition(mapper, x, y);
8694 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008696
8697 // Reactivate display viewport
8698 displayViewport.isActive = true;
8699 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8700 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8701
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008702 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008703 x += 10, y += 10;
8704 processPosition(mapper, x, y);
8705 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8707 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008708}
8709
Arthur Hung7c645402019-01-25 17:45:42 +08008710TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8711 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008712 prepareAxes(POSITION | ID | SLOT);
8713 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008714 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008715
8716 // Create the second touch screen device, and enable multi fingers.
8717 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008718 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008719 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008720 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008721 std::shared_ptr<InputDevice> device2 =
8722 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008723 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008724
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008725 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8726 0 /*flat*/, 0 /*fuzz*/);
8727 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8728 0 /*flat*/, 0 /*fuzz*/);
8729 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8730 0 /*flat*/, 0 /*fuzz*/);
8731 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8732 0 /*flat*/, 0 /*fuzz*/);
8733 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8734 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8735 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008736
8737 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008738 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008739 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8740 device2->reset(ARBITRARY_TIME);
8741
8742 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008743 std::shared_ptr<FakePointerController> fakePointerController =
8744 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008745 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008746
8747 // Setup policy for associated displays and show touches.
8748 const uint8_t hdmi1 = 0;
8749 const uint8_t hdmi2 = 1;
8750 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8751 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8752 mFakePolicy->setShowTouches(true);
8753
8754 // Create displays.
8755 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008756 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008757
8758 // Default device will reconfigure above, need additional reconfiguration for another device.
8759 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan197e0862022-07-01 14:28:00 +00008760 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8761 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008762
8763 // Two fingers down at default display.
8764 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8765 processPosition(mapper, x1, y1);
8766 processId(mapper, 1);
8767 processSlot(mapper, 1);
8768 processPosition(mapper, x2, y2);
8769 processId(mapper, 2);
8770 processSync(mapper);
8771
8772 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8773 fakePointerController->getSpots().find(DISPLAY_ID);
8774 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8775 ASSERT_EQ(size_t(2), iter->second.size());
8776
8777 // Two fingers down at second display.
8778 processPosition(mapper2, x1, y1);
8779 processId(mapper2, 1);
8780 processSlot(mapper2, 1);
8781 processPosition(mapper2, x2, y2);
8782 processId(mapper2, 2);
8783 processSync(mapper2);
8784
8785 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8786 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8787 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008788
8789 // Disable the show touches configuration and ensure the spots are cleared.
8790 mFakePolicy->setShowTouches(false);
8791 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8792 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
8793
8794 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008795}
8796
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008797TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008798 prepareAxes(POSITION);
8799 addConfigurationProperty("touch.deviceType", "touchScreen");
8800 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008801 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008802
8803 NotifyMotionArgs motionArgs;
8804 // Unrotated video frame
8805 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8806 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008807 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008808 processPosition(mapper, 100, 200);
8809 processSync(mapper);
8810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8811 ASSERT_EQ(frames, motionArgs.videoFrames);
8812
8813 // Subsequent touch events should not have any videoframes
8814 // This is implemented separately in FakeEventHub,
8815 // but that should match the behaviour of TouchVideoDevice.
8816 processPosition(mapper, 200, 200);
8817 processSync(mapper);
8818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8819 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8820}
8821
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008822TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008823 prepareAxes(POSITION);
8824 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008825 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008826 // Unrotated video frame
8827 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8828 NotifyMotionArgs motionArgs;
8829
8830 // Test all 4 orientations
8831 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008832 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8833 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8834 clearViewports();
8835 prepareDisplay(orientation);
8836 std::vector<TouchVideoFrame> frames{frame};
8837 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8838 processPosition(mapper, 100, 200);
8839 processSync(mapper);
8840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8841 ASSERT_EQ(frames, motionArgs.videoFrames);
8842 }
8843}
8844
8845TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8846 prepareAxes(POSITION);
8847 addConfigurationProperty("touch.deviceType", "touchScreen");
8848 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8849 // orientation-aware are affected by display rotation.
8850 addConfigurationProperty("touch.orientationAware", "0");
8851 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8852 // Unrotated video frame
8853 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8854 NotifyMotionArgs motionArgs;
8855
8856 // Test all 4 orientations
8857 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008858 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8859 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8860 clearViewports();
8861 prepareDisplay(orientation);
8862 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008863 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008864 processPosition(mapper, 100, 200);
8865 processSync(mapper);
8866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008867 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8868 // compared to the display. This is so that when the window transform (which contains the
8869 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8870 // window's coordinate space.
8871 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008872 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08008873
8874 // Release finger.
8875 processSync(mapper);
8876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008877 }
8878}
8879
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008880TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008881 prepareAxes(POSITION);
8882 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008883 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008884 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8885 // so mix these.
8886 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8887 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8888 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8889 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8890 NotifyMotionArgs motionArgs;
8891
8892 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008893 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008894 processPosition(mapper, 100, 200);
8895 processSync(mapper);
8896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008897 ASSERT_EQ(frames, motionArgs.videoFrames);
8898}
8899
8900TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8901 prepareAxes(POSITION);
8902 addConfigurationProperty("touch.deviceType", "touchScreen");
8903 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8904 // orientation-aware are affected by display rotation.
8905 addConfigurationProperty("touch.orientationAware", "0");
8906 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8907 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8908 // so mix these.
8909 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8910 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8911 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8912 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8913 NotifyMotionArgs motionArgs;
8914
8915 prepareDisplay(DISPLAY_ORIENTATION_90);
8916 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8917 processPosition(mapper, 100, 200);
8918 processSync(mapper);
8919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8920 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8921 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8922 // compared to the display. This is so that when the window transform (which contains the
8923 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8924 // window's coordinate space.
8925 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8926 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008927 ASSERT_EQ(frames, motionArgs.videoFrames);
8928}
8929
Arthur Hung9da14732019-09-02 16:16:58 +08008930/**
8931 * If we had defined port associations, but the viewport is not ready, the touch device would be
8932 * expected to be disabled, and it should be enabled after the viewport has found.
8933 */
8934TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008935 constexpr uint8_t hdmi2 = 1;
8936 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008937 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008938
8939 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8940
8941 addConfigurationProperty("touch.deviceType", "touchScreen");
8942 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008943 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008944
8945 ASSERT_EQ(mDevice->isEnabled(), false);
8946
8947 // Add display on hdmi2, the device should be enabled and can receive touch event.
8948 prepareSecondaryDisplay(type, hdmi2);
8949 ASSERT_EQ(mDevice->isEnabled(), true);
8950
8951 // Send a touch event.
8952 processPosition(mapper, 100, 100);
8953 processSync(mapper);
8954
8955 NotifyMotionArgs args;
8956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8957 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8958}
8959
Arthur Hung421eb1c2020-01-16 00:09:42 +08008960TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008961 addConfigurationProperty("touch.deviceType", "touchScreen");
8962 prepareDisplay(DISPLAY_ORIENTATION_0);
8963 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008964 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008965
8966 NotifyMotionArgs motionArgs;
8967
8968 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8969 // finger down
8970 processId(mapper, 1);
8971 processPosition(mapper, x1, y1);
8972 processSync(mapper);
8973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8974 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8975 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8976
8977 // finger move
8978 processId(mapper, 1);
8979 processPosition(mapper, x2, y2);
8980 processSync(mapper);
8981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8982 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8983 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8984
8985 // finger up.
8986 processId(mapper, -1);
8987 processSync(mapper);
8988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8989 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8990 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8991
8992 // new finger down
8993 processId(mapper, 1);
8994 processPosition(mapper, x3, y3);
8995 processSync(mapper);
8996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8997 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8998 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8999}
9000
9001/**
arthurhungcc7f9802020-04-30 17:55:40 +08009002 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9003 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009004 */
arthurhungcc7f9802020-04-30 17:55:40 +08009005TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009006 addConfigurationProperty("touch.deviceType", "touchScreen");
9007 prepareDisplay(DISPLAY_ORIENTATION_0);
9008 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009009 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009010
9011 NotifyMotionArgs motionArgs;
9012
9013 // default tool type is finger
9014 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009015 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009016 processPosition(mapper, x1, y1);
9017 processSync(mapper);
9018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9019 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9020 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9021
9022 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9023 processToolType(mapper, MT_TOOL_PALM);
9024 processSync(mapper);
9025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9026 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9027
9028 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009029 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009030 processPosition(mapper, x2, y2);
9031 processSync(mapper);
9032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9033
9034 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009035 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009036 processSync(mapper);
9037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9038
9039 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009040 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009041 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009042 processPosition(mapper, x3, y3);
9043 processSync(mapper);
9044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9045 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9046 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9047}
9048
arthurhungbf89a482020-04-17 17:37:55 +08009049/**
arthurhungcc7f9802020-04-30 17:55:40 +08009050 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9051 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009052 */
arthurhungcc7f9802020-04-30 17:55:40 +08009053TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009054 addConfigurationProperty("touch.deviceType", "touchScreen");
9055 prepareDisplay(DISPLAY_ORIENTATION_0);
9056 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9057 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9058
9059 NotifyMotionArgs motionArgs;
9060
9061 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009062 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9063 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009064 processPosition(mapper, x1, y1);
9065 processSync(mapper);
9066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9067 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9068 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9069
9070 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009071 processSlot(mapper, SECOND_SLOT);
9072 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009073 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009074 processSync(mapper);
9075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009076 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009077 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9078
9079 // If the tool type of the first finger changes to MT_TOOL_PALM,
9080 // we expect to receive ACTION_POINTER_UP with cancel flag.
9081 processSlot(mapper, FIRST_SLOT);
9082 processId(mapper, FIRST_TRACKING_ID);
9083 processToolType(mapper, MT_TOOL_PALM);
9084 processSync(mapper);
9085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009086 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009087 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9088
9089 // The following MOVE events of second finger should be processed.
9090 processSlot(mapper, SECOND_SLOT);
9091 processId(mapper, SECOND_TRACKING_ID);
9092 processPosition(mapper, x2 + 1, y2 + 1);
9093 processSync(mapper);
9094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9095 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9096 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9097
9098 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9099 // it. Second finger receive move.
9100 processSlot(mapper, FIRST_SLOT);
9101 processId(mapper, INVALID_TRACKING_ID);
9102 processSync(mapper);
9103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9104 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9105 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9106
9107 // Second finger keeps moving.
9108 processSlot(mapper, SECOND_SLOT);
9109 processId(mapper, SECOND_TRACKING_ID);
9110 processPosition(mapper, x2 + 2, y2 + 2);
9111 processSync(mapper);
9112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9113 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9114 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9115
9116 // Second finger up.
9117 processId(mapper, INVALID_TRACKING_ID);
9118 processSync(mapper);
9119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9120 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9121 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9122}
9123
9124/**
9125 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9126 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9127 */
9128TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9129 addConfigurationProperty("touch.deviceType", "touchScreen");
9130 prepareDisplay(DISPLAY_ORIENTATION_0);
9131 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9132 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9133
9134 NotifyMotionArgs motionArgs;
9135
9136 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9137 // First finger down.
9138 processId(mapper, FIRST_TRACKING_ID);
9139 processPosition(mapper, x1, y1);
9140 processSync(mapper);
9141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9142 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9143 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9144
9145 // Second finger down.
9146 processSlot(mapper, SECOND_SLOT);
9147 processId(mapper, SECOND_TRACKING_ID);
9148 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009149 processSync(mapper);
9150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009151 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009152 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9153
arthurhungcc7f9802020-04-30 17:55:40 +08009154 // If the tool type of the first finger changes to MT_TOOL_PALM,
9155 // we expect to receive ACTION_POINTER_UP with cancel flag.
9156 processSlot(mapper, FIRST_SLOT);
9157 processId(mapper, FIRST_TRACKING_ID);
9158 processToolType(mapper, MT_TOOL_PALM);
9159 processSync(mapper);
9160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009161 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009162 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9163
9164 // Second finger keeps moving.
9165 processSlot(mapper, SECOND_SLOT);
9166 processId(mapper, SECOND_TRACKING_ID);
9167 processPosition(mapper, x2 + 1, y2 + 1);
9168 processSync(mapper);
9169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9170 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9171
9172 // second finger becomes palm, receive cancel due to only 1 finger is active.
9173 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009174 processToolType(mapper, MT_TOOL_PALM);
9175 processSync(mapper);
9176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9177 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9178
arthurhungcc7f9802020-04-30 17:55:40 +08009179 // third finger down.
9180 processSlot(mapper, THIRD_SLOT);
9181 processId(mapper, THIRD_TRACKING_ID);
9182 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009183 processPosition(mapper, x3, y3);
9184 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9186 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9187 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009188 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9189
9190 // third finger move
9191 processId(mapper, THIRD_TRACKING_ID);
9192 processPosition(mapper, x3 + 1, y3 + 1);
9193 processSync(mapper);
9194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9195 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9196
9197 // first finger up, third finger receive move.
9198 processSlot(mapper, FIRST_SLOT);
9199 processId(mapper, INVALID_TRACKING_ID);
9200 processSync(mapper);
9201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9202 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9203 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9204
9205 // second finger up, third finger receive move.
9206 processSlot(mapper, SECOND_SLOT);
9207 processId(mapper, INVALID_TRACKING_ID);
9208 processSync(mapper);
9209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9210 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9211 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9212
9213 // third finger up.
9214 processSlot(mapper, THIRD_SLOT);
9215 processId(mapper, INVALID_TRACKING_ID);
9216 processSync(mapper);
9217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9218 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9219 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9220}
9221
9222/**
9223 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9224 * and the active finger could still be allowed to receive the events
9225 */
9226TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9227 addConfigurationProperty("touch.deviceType", "touchScreen");
9228 prepareDisplay(DISPLAY_ORIENTATION_0);
9229 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9230 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9231
9232 NotifyMotionArgs motionArgs;
9233
9234 // default tool type is finger
9235 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9236 processId(mapper, FIRST_TRACKING_ID);
9237 processPosition(mapper, x1, y1);
9238 processSync(mapper);
9239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9240 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9241 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9242
9243 // Second finger down.
9244 processSlot(mapper, SECOND_SLOT);
9245 processId(mapper, SECOND_TRACKING_ID);
9246 processPosition(mapper, x2, y2);
9247 processSync(mapper);
9248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009249 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009250 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9251
9252 // If the tool type of the second finger changes to MT_TOOL_PALM,
9253 // we expect to receive ACTION_POINTER_UP with cancel flag.
9254 processId(mapper, SECOND_TRACKING_ID);
9255 processToolType(mapper, MT_TOOL_PALM);
9256 processSync(mapper);
9257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009258 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009259 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9260
9261 // The following MOVE event should be processed.
9262 processSlot(mapper, FIRST_SLOT);
9263 processId(mapper, FIRST_TRACKING_ID);
9264 processPosition(mapper, x1 + 1, y1 + 1);
9265 processSync(mapper);
9266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9267 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9268 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9269
9270 // second finger up.
9271 processSlot(mapper, SECOND_SLOT);
9272 processId(mapper, INVALID_TRACKING_ID);
9273 processSync(mapper);
9274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9275 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9276
9277 // first finger keep moving
9278 processSlot(mapper, FIRST_SLOT);
9279 processId(mapper, FIRST_TRACKING_ID);
9280 processPosition(mapper, x1 + 2, y1 + 2);
9281 processSync(mapper);
9282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9283 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9284
9285 // first finger up.
9286 processId(mapper, INVALID_TRACKING_ID);
9287 processSync(mapper);
9288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9289 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9290 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009291}
9292
Arthur Hung9ad18942021-06-19 02:04:46 +00009293/**
9294 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9295 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9296 * cause slot be valid again.
9297 */
9298TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9299 addConfigurationProperty("touch.deviceType", "touchScreen");
9300 prepareDisplay(DISPLAY_ORIENTATION_0);
9301 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9302 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9303
9304 NotifyMotionArgs motionArgs;
9305
9306 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9307 // First finger down.
9308 processId(mapper, FIRST_TRACKING_ID);
9309 processPosition(mapper, x1, y1);
9310 processPressure(mapper, RAW_PRESSURE_MAX);
9311 processSync(mapper);
9312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9313 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9314 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9315
9316 // First finger move.
9317 processId(mapper, FIRST_TRACKING_ID);
9318 processPosition(mapper, x1 + 1, y1 + 1);
9319 processPressure(mapper, RAW_PRESSURE_MAX);
9320 processSync(mapper);
9321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9322 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9323 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9324
9325 // Second finger down.
9326 processSlot(mapper, SECOND_SLOT);
9327 processId(mapper, SECOND_TRACKING_ID);
9328 processPosition(mapper, x2, y2);
9329 processPressure(mapper, RAW_PRESSURE_MAX);
9330 processSync(mapper);
9331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009332 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009333 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9334
9335 // second finger up with some unexpected data.
9336 processSlot(mapper, SECOND_SLOT);
9337 processId(mapper, INVALID_TRACKING_ID);
9338 processPosition(mapper, x2, y2);
9339 processSync(mapper);
9340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009341 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009342 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9343
9344 // first finger up with some unexpected data.
9345 processSlot(mapper, FIRST_SLOT);
9346 processId(mapper, INVALID_TRACKING_ID);
9347 processPosition(mapper, x2, y2);
9348 processPressure(mapper, RAW_PRESSURE_MAX);
9349 processSync(mapper);
9350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9351 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9352 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9353}
9354
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009355// --- MultiTouchInputMapperTest_ExternalDevice ---
9356
9357class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9358protected:
Chris Yea52ade12020-08-27 16:49:20 -07009359 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009360};
9361
9362/**
9363 * Expect fallback to internal viewport if device is external and external viewport is not present.
9364 */
9365TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9366 prepareAxes(POSITION);
9367 addConfigurationProperty("touch.deviceType", "touchScreen");
9368 prepareDisplay(DISPLAY_ORIENTATION_0);
9369 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9370
9371 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9372
9373 NotifyMotionArgs motionArgs;
9374
9375 // Expect the event to be sent to the internal viewport,
9376 // because an external viewport is not present.
9377 processPosition(mapper, 100, 100);
9378 processSync(mapper);
9379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9380 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9381
9382 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009383 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009384 processPosition(mapper, 100, 100);
9385 processSync(mapper);
9386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9387 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9388}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009389
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009390TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9391 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9392 std::shared_ptr<FakePointerController> fakePointerController =
9393 std::make_shared<FakePointerController>();
9394 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9395 fakePointerController->setPosition(0, 0);
9396 fakePointerController->setButtonState(0);
9397
9398 // prepare device and capture
9399 prepareDisplay(DISPLAY_ORIENTATION_0);
9400 prepareAxes(POSITION | ID | SLOT);
9401 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9402 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9403 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009404 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009405 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9406
9407 // captured touchpad should be a touchpad source
9408 NotifyDeviceResetArgs resetArgs;
9409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9410 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9411
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009412 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009413
9414 const InputDeviceInfo::MotionRange* relRangeX =
9415 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9416 ASSERT_NE(relRangeX, nullptr);
9417 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9418 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9419 const InputDeviceInfo::MotionRange* relRangeY =
9420 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9421 ASSERT_NE(relRangeY, nullptr);
9422 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9423 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9424
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009425 // run captured pointer tests - note that this is unscaled, so input listener events should be
9426 // identical to what the hardware sends (accounting for any
9427 // calibration).
9428 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009429 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009430 processId(mapper, 1);
9431 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9432 processKey(mapper, BTN_TOUCH, 1);
9433 processSync(mapper);
9434
9435 // expect coord[0] to contain initial location of touch 0
9436 NotifyMotionArgs args;
9437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9438 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9439 ASSERT_EQ(1U, args.pointerCount);
9440 ASSERT_EQ(0, args.pointerProperties[0].id);
9441 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9442 ASSERT_NO_FATAL_FAILURE(
9443 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9444
9445 // FINGER 1 DOWN
9446 processSlot(mapper, 1);
9447 processId(mapper, 2);
9448 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9449 processSync(mapper);
9450
9451 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009453 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009454 ASSERT_EQ(2U, args.pointerCount);
9455 ASSERT_EQ(0, args.pointerProperties[0].id);
9456 ASSERT_EQ(1, args.pointerProperties[1].id);
9457 ASSERT_NO_FATAL_FAILURE(
9458 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9459 ASSERT_NO_FATAL_FAILURE(
9460 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9461
9462 // FINGER 1 MOVE
9463 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9464 processSync(mapper);
9465
9466 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9467 // from move
9468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9469 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9470 ASSERT_NO_FATAL_FAILURE(
9471 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9472 ASSERT_NO_FATAL_FAILURE(
9473 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9474
9475 // FINGER 0 MOVE
9476 processSlot(mapper, 0);
9477 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9478 processSync(mapper);
9479
9480 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9482 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9483 ASSERT_NO_FATAL_FAILURE(
9484 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9485 ASSERT_NO_FATAL_FAILURE(
9486 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9487
9488 // BUTTON DOWN
9489 processKey(mapper, BTN_LEFT, 1);
9490 processSync(mapper);
9491
9492 // touchinputmapper design sends a move before button press
9493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9494 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9496 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9497
9498 // BUTTON UP
9499 processKey(mapper, BTN_LEFT, 0);
9500 processSync(mapper);
9501
9502 // touchinputmapper design sends a move after button release
9503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9504 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9506 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9507
9508 // FINGER 0 UP
9509 processId(mapper, -1);
9510 processSync(mapper);
9511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9512 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9513
9514 // FINGER 1 MOVE
9515 processSlot(mapper, 1);
9516 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9517 processSync(mapper);
9518
9519 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9521 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9522 ASSERT_EQ(1U, args.pointerCount);
9523 ASSERT_EQ(1, args.pointerProperties[0].id);
9524 ASSERT_NO_FATAL_FAILURE(
9525 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9526
9527 // FINGER 1 UP
9528 processId(mapper, -1);
9529 processKey(mapper, BTN_TOUCH, 0);
9530 processSync(mapper);
9531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9532 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9533
9534 // non captured touchpad should be a mouse source
9535 mFakePolicy->setPointerCapture(false);
9536 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9538 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9539}
9540
9541TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9542 std::shared_ptr<FakePointerController> fakePointerController =
9543 std::make_shared<FakePointerController>();
9544 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9545 fakePointerController->setPosition(0, 0);
9546 fakePointerController->setButtonState(0);
9547
9548 // prepare device and capture
9549 prepareDisplay(DISPLAY_ORIENTATION_0);
9550 prepareAxes(POSITION | ID | SLOT);
9551 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9552 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009553 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009554 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9555 // run uncaptured pointer tests - pushes out generic events
9556 // FINGER 0 DOWN
9557 processId(mapper, 3);
9558 processPosition(mapper, 100, 100);
9559 processKey(mapper, BTN_TOUCH, 1);
9560 processSync(mapper);
9561
9562 // start at (100,100), cursor should be at (0,0) * scale
9563 NotifyMotionArgs args;
9564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9565 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9566 ASSERT_NO_FATAL_FAILURE(
9567 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9568
9569 // FINGER 0 MOVE
9570 processPosition(mapper, 200, 200);
9571 processSync(mapper);
9572
9573 // compute scaling to help with touch position checking
9574 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9575 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9576 float scale =
9577 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9578
9579 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9581 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9583 0, 0, 0, 0, 0, 0, 0));
9584}
9585
9586TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9587 std::shared_ptr<FakePointerController> fakePointerController =
9588 std::make_shared<FakePointerController>();
9589
9590 prepareDisplay(DISPLAY_ORIENTATION_0);
9591 prepareAxes(POSITION | ID | SLOT);
9592 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009593 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009594 mFakePolicy->setPointerCapture(false);
9595 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9596
9597 // uncaptured touchpad should be a pointer device
9598 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9599
9600 // captured touchpad should be a touchpad device
9601 mFakePolicy->setPointerCapture(true);
9602 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9603 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9604}
9605
HQ Liue6983c72022-04-19 22:14:56 +00009606class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9607protected:
9608 float mPointerMovementScale;
9609 float mPointerXZoomScale;
9610 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9611 addConfigurationProperty("touch.deviceType", "pointer");
9612 std::shared_ptr<FakePointerController> fakePointerController =
9613 std::make_shared<FakePointerController>();
9614 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9615 fakePointerController->setPosition(0, 0);
9616 fakePointerController->setButtonState(0);
9617 prepareDisplay(DISPLAY_ORIENTATION_0);
9618
9619 prepareAxes(POSITION);
9620 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9621 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9622 // needs to be disabled, and the pointer gesture needs to be enabled.
9623 mFakePolicy->setPointerCapture(false);
9624 mFakePolicy->setPointerGestureEnabled(true);
9625 mFakePolicy->setPointerController(fakePointerController);
9626
9627 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9628 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9629 mPointerMovementScale =
9630 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9631 mPointerXZoomScale =
9632 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9633 }
9634
9635 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9636 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9637 /*flat*/ 0,
9638 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9639 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9640 /*flat*/ 0,
9641 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9642 }
9643};
9644
9645/**
9646 * Two fingers down on a pointer mode touch pad. The width
9647 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9648 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9649 * be greater than the both value to be freeform gesture, so that after two
9650 * fingers start to move downwards, the gesture should be swipe.
9651 */
9652TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9653 // The min freeform gesture width is 25units/mm x 30mm = 750
9654 // which is greater than fraction of the diagnal length of the touchpad (349).
9655 // Thus, MaxSwipWidth is 750.
9656 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9657 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9658 NotifyMotionArgs motionArgs;
9659
9660 // Two fingers down at once.
9661 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9662 // Pointer's initial position is used the [0,0] coordinate.
9663 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9664
9665 processId(mapper, FIRST_TRACKING_ID);
9666 processPosition(mapper, x1, y1);
9667 processMTSync(mapper);
9668 processId(mapper, SECOND_TRACKING_ID);
9669 processPosition(mapper, x2, y2);
9670 processMTSync(mapper);
9671 processSync(mapper);
9672
9673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9674 ASSERT_EQ(1U, motionArgs.pointerCount);
9675 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9676 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9677 ASSERT_NO_FATAL_FAILURE(
9678 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9679
9680 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9681 // that there should be 1 pointer.
9682 int32_t movingDistance = 200;
9683 y1 += movingDistance;
9684 y2 += movingDistance;
9685
9686 processId(mapper, FIRST_TRACKING_ID);
9687 processPosition(mapper, x1, y1);
9688 processMTSync(mapper);
9689 processId(mapper, SECOND_TRACKING_ID);
9690 processPosition(mapper, x2, y2);
9691 processMTSync(mapper);
9692 processSync(mapper);
9693
9694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9695 ASSERT_EQ(1U, motionArgs.pointerCount);
9696 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9697 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9699 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9700 0, 0, 0, 0));
9701}
9702
9703/**
9704 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9705 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9706 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9707 * value to be freeform gesture, so that after two fingers start to move downwards,
9708 * the gesture should be swipe.
9709 */
9710TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9711 // The min freeform gesture width is 5units/mm x 30mm = 150
9712 // which is greater than fraction of the diagnal length of the touchpad (349).
9713 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
9714 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
9715 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9716 NotifyMotionArgs motionArgs;
9717
9718 // Two fingers down at once.
9719 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9720 // Pointer's initial position is used the [0,0] coordinate.
9721 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9722
9723 processId(mapper, FIRST_TRACKING_ID);
9724 processPosition(mapper, x1, y1);
9725 processMTSync(mapper);
9726 processId(mapper, SECOND_TRACKING_ID);
9727 processPosition(mapper, x2, y2);
9728 processMTSync(mapper);
9729 processSync(mapper);
9730
9731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9732 ASSERT_EQ(1U, motionArgs.pointerCount);
9733 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9734 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9735 ASSERT_NO_FATAL_FAILURE(
9736 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9737
9738 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9739 // and there should be 1 pointer.
9740 int32_t movingDistance = 200;
9741 y1 += movingDistance;
9742 y2 += movingDistance;
9743
9744 processId(mapper, FIRST_TRACKING_ID);
9745 processPosition(mapper, x1, y1);
9746 processMTSync(mapper);
9747 processId(mapper, SECOND_TRACKING_ID);
9748 processPosition(mapper, x2, y2);
9749 processMTSync(mapper);
9750 processSync(mapper);
9751
9752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9753 ASSERT_EQ(1U, motionArgs.pointerCount);
9754 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9755 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9756 // New coordinate is the scaled relative coordinate from the initial coordinate.
9757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9758 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9759 0, 0, 0, 0));
9760}
9761
9762/**
9763 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
9764 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
9765 * freeform gestures after two fingers start to move downwards.
9766 */
9767TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
9768 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9769 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9770
9771 NotifyMotionArgs motionArgs;
9772
9773 // Two fingers down at once. Wider than the max swipe width.
9774 // The gesture is expected to be PRESS, then transformed to FREEFORM
9775 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
9776
9777 processId(mapper, FIRST_TRACKING_ID);
9778 processPosition(mapper, x1, y1);
9779 processMTSync(mapper);
9780 processId(mapper, SECOND_TRACKING_ID);
9781 processPosition(mapper, x2, y2);
9782 processMTSync(mapper);
9783 processSync(mapper);
9784
9785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9786 ASSERT_EQ(1U, motionArgs.pointerCount);
9787 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9788 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9789 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
9790 ASSERT_NO_FATAL_FAILURE(
9791 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9792
9793 int32_t movingDistance = 200;
9794
9795 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
9796 // then two down events for two pointers.
9797 y1 += movingDistance;
9798 y2 += movingDistance;
9799
9800 processId(mapper, FIRST_TRACKING_ID);
9801 processPosition(mapper, x1, y1);
9802 processMTSync(mapper);
9803 processId(mapper, SECOND_TRACKING_ID);
9804 processPosition(mapper, x2, y2);
9805 processMTSync(mapper);
9806 processSync(mapper);
9807
9808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9809 // The previous PRESS gesture is cancelled, because it is transformed to freeform
9810 ASSERT_EQ(1U, motionArgs.pointerCount);
9811 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9813 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9814 ASSERT_EQ(1U, motionArgs.pointerCount);
9815 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9817 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9818 ASSERT_EQ(2U, motionArgs.pointerCount);
9819 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
9820 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9821 // Two pointers' scaled relative coordinates from their initial centroid.
9822 // Initial y coordinates are 0 as y1 and y2 have the same value.
9823 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
9824 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
9825 // When pointers move, the new coordinates equal to the initial coordinates plus
9826 // scaled moving distance.
9827 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9828 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9829 0, 0, 0, 0));
9830 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9831 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9832 0, 0, 0, 0));
9833
9834 // Move two fingers down again, expect one MOVE motion event.
9835 y1 += movingDistance;
9836 y2 += movingDistance;
9837
9838 processId(mapper, FIRST_TRACKING_ID);
9839 processPosition(mapper, x1, y1);
9840 processMTSync(mapper);
9841 processId(mapper, SECOND_TRACKING_ID);
9842 processPosition(mapper, x2, y2);
9843 processMTSync(mapper);
9844 processSync(mapper);
9845
9846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9847 ASSERT_EQ(2U, motionArgs.pointerCount);
9848 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9849 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9851 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9852 0, 0, 0, 0, 0));
9853 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9854 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9855 0, 0, 0, 0, 0));
9856}
9857
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009858// --- JoystickInputMapperTest ---
9859
9860class JoystickInputMapperTest : public InputMapperTest {
9861protected:
9862 static const int32_t RAW_X_MIN;
9863 static const int32_t RAW_X_MAX;
9864 static const int32_t RAW_Y_MIN;
9865 static const int32_t RAW_Y_MAX;
9866
9867 void SetUp() override {
9868 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9869 }
9870 void prepareAxes() {
9871 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9872 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9873 }
9874
9875 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9876 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9877 }
9878
9879 void processSync(JoystickInputMapper& mapper) {
9880 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9881 }
9882
9883 void prepareVirtualDisplay(int32_t orientation) {
9884 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9885 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9886 NO_PORT, ViewportType::VIRTUAL);
9887 }
9888};
9889
9890const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9891const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9892const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9893const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9894
9895TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9896 prepareAxes();
9897 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9898
9899 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9900
9901 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9902
9903 // Send an axis event
9904 processAxis(mapper, ABS_X, 100);
9905 processSync(mapper);
9906
9907 NotifyMotionArgs args;
9908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9909 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9910
9911 // Send another axis event
9912 processAxis(mapper, ABS_Y, 100);
9913 processSync(mapper);
9914
9915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9916 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9917}
9918
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009919// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009920
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009921class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009922protected:
9923 static const char* DEVICE_NAME;
9924 static const char* DEVICE_LOCATION;
9925 static const int32_t DEVICE_ID;
9926 static const int32_t DEVICE_GENERATION;
9927 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009928 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009929 static const int32_t EVENTHUB_ID;
9930
9931 std::shared_ptr<FakeEventHub> mFakeEventHub;
9932 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009933 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009934 std::unique_ptr<InstrumentedInputReader> mReader;
9935 std::shared_ptr<InputDevice> mDevice;
9936
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009937 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009938 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009939 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009940 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009941 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009942 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009943 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9944 }
9945
9946 void SetUp() override { SetUp(DEVICE_CLASSES); }
9947
9948 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009949 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009950 mFakePolicy.clear();
9951 }
9952
9953 void configureDevice(uint32_t changes) {
9954 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9955 mReader->requestRefreshConfiguration(changes);
9956 mReader->loopOnce();
9957 }
9958 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9959 }
9960
9961 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9962 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009963 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009964 InputDeviceIdentifier identifier;
9965 identifier.name = name;
9966 identifier.location = location;
9967 std::shared_ptr<InputDevice> device =
9968 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9969 identifier);
9970 mReader->pushNextDevice(device);
9971 mFakeEventHub->addDevice(eventHubId, name, classes);
9972 mReader->loopOnce();
9973 return device;
9974 }
9975
9976 template <class T, typename... Args>
9977 T& addControllerAndConfigure(Args... args) {
9978 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9979
9980 return controller;
9981 }
9982};
9983
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009984const char* PeripheralControllerTest::DEVICE_NAME = "device";
9985const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9986const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9987const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9988const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009989const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
9990 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009991const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009992
9993// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009994class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009995protected:
9996 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009997 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009998 }
9999};
10000
10001TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010002 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010003
10004 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
10005 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
10006}
10007
10008TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010009 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010010
10011 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
10012 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
10013}
10014
10015// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010016class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010017protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010018 void SetUp() override {
10019 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10020 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010021};
10022
Chris Ye85758332021-05-16 23:05:17 -070010023TEST_F(LightControllerTest, MonoLight) {
10024 RawLightInfo infoMono = {.id = 1,
10025 .name = "Mono",
10026 .maxBrightness = 255,
10027 .flags = InputLightClass::BRIGHTNESS,
10028 .path = ""};
10029 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010030
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010031 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010032 InputDeviceInfo info;
10033 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010034 std::vector<InputDeviceLightInfo> lights = info.getLights();
10035 ASSERT_EQ(1U, lights.size());
10036 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010037
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010038 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10039 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010040}
10041
10042TEST_F(LightControllerTest, RGBLight) {
10043 RawLightInfo infoRed = {.id = 1,
10044 .name = "red",
10045 .maxBrightness = 255,
10046 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10047 .path = ""};
10048 RawLightInfo infoGreen = {.id = 2,
10049 .name = "green",
10050 .maxBrightness = 255,
10051 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10052 .path = ""};
10053 RawLightInfo infoBlue = {.id = 3,
10054 .name = "blue",
10055 .maxBrightness = 255,
10056 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10057 .path = ""};
10058 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10059 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10060 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10061
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010062 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010063 InputDeviceInfo info;
10064 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010065 std::vector<InputDeviceLightInfo> lights = info.getLights();
10066 ASSERT_EQ(1U, lights.size());
10067 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010068
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010069 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10070 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010071}
10072
10073TEST_F(LightControllerTest, MultiColorRGBLight) {
10074 RawLightInfo infoColor = {.id = 1,
10075 .name = "red",
10076 .maxBrightness = 255,
10077 .flags = InputLightClass::BRIGHTNESS |
10078 InputLightClass::MULTI_INTENSITY |
10079 InputLightClass::MULTI_INDEX,
10080 .path = ""};
10081
10082 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10083
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010084 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010085 InputDeviceInfo info;
10086 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010087 std::vector<InputDeviceLightInfo> lights = info.getLights();
10088 ASSERT_EQ(1U, lights.size());
10089 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010090
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010091 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10092 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010093}
10094
10095TEST_F(LightControllerTest, PlayerIdLight) {
10096 RawLightInfo info1 = {.id = 1,
10097 .name = "player1",
10098 .maxBrightness = 255,
10099 .flags = InputLightClass::BRIGHTNESS,
10100 .path = ""};
10101 RawLightInfo info2 = {.id = 2,
10102 .name = "player2",
10103 .maxBrightness = 255,
10104 .flags = InputLightClass::BRIGHTNESS,
10105 .path = ""};
10106 RawLightInfo info3 = {.id = 3,
10107 .name = "player3",
10108 .maxBrightness = 255,
10109 .flags = InputLightClass::BRIGHTNESS,
10110 .path = ""};
10111 RawLightInfo info4 = {.id = 4,
10112 .name = "player4",
10113 .maxBrightness = 255,
10114 .flags = InputLightClass::BRIGHTNESS,
10115 .path = ""};
10116 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10117 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10118 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10119 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10120
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010121 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010122 InputDeviceInfo info;
10123 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010124 std::vector<InputDeviceLightInfo> lights = info.getLights();
10125 ASSERT_EQ(1U, lights.size());
10126 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010127
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010128 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10129 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10130 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010131}
10132
Michael Wrightd02c5b62014-02-10 15:10:22 -080010133} // namespace android