blob: a0e61927d340ced243a2d25601d0c7daca56b1c0 [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
Michael Wrightdde67b82020-10-27 16:09:22 +000041#include "input/DisplayViewport.h"
42#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010043
Michael Wrightd02c5b62014-02-10 15:10:22 -080044namespace android {
45
Dominik Laskowski2f01d772022-03-23 16:01:29 -070046using namespace ftl::flag_operators;
47
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070048using std::chrono_literals::operator""ms;
49
50// Timeout for waiting for an expected event
51static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
52
Michael Wrightd02c5b62014-02-10 15:10:22 -080053// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000054static constexpr nsecs_t ARBITRARY_TIME = 1234;
55static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
57// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080058static constexpr int32_t DISPLAY_ID = 0;
59static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
60static constexpr int32_t DISPLAY_WIDTH = 480;
61static constexpr int32_t DISPLAY_HEIGHT = 800;
62static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
63static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
64static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070065static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070066static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080067
arthurhungcc7f9802020-04-30 17:55:40 +080068static constexpr int32_t FIRST_SLOT = 0;
69static constexpr int32_t SECOND_SLOT = 1;
70static constexpr int32_t THIRD_SLOT = 2;
71static constexpr int32_t INVALID_TRACKING_ID = -1;
72static constexpr int32_t FIRST_TRACKING_ID = 0;
73static constexpr int32_t SECOND_TRACKING_ID = 1;
74static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080075static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080076static constexpr int32_t BATTERY_STATUS = 4;
77static constexpr int32_t BATTERY_CAPACITY = 66;
Chris Ye3fdbfef2021-01-06 18:45:18 -080078static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
79static constexpr int32_t LIGHT_COLOR = 0x7F448866;
80static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080081
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080082static constexpr int32_t ACTION_POINTER_0_DOWN =
83 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
84static constexpr int32_t ACTION_POINTER_0_UP =
85 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
86static constexpr int32_t ACTION_POINTER_1_DOWN =
87 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
88static constexpr int32_t ACTION_POINTER_1_UP =
89 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
90
Michael Wrightd02c5b62014-02-10 15:10:22 -080091// Error tolerance for floating point assertions.
92static const float EPSILON = 0.001f;
93
94template<typename T>
95static inline T min(T a, T b) {
96 return a < b ? a : b;
97}
98
99static inline float avg(float x, float y) {
100 return (x + y) / 2;
101}
102
Chris Ye3fdbfef2021-01-06 18:45:18 -0800103// Mapping for light color name and the light color
104const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
105 {"green", LightColor::GREEN},
106 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800107
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700108static int32_t getInverseRotation(int32_t orientation) {
109 switch (orientation) {
110 case DISPLAY_ORIENTATION_90:
111 return DISPLAY_ORIENTATION_270;
112 case DISPLAY_ORIENTATION_270:
113 return DISPLAY_ORIENTATION_90;
114 default:
115 return orientation;
116 }
117}
118
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800119static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
120 InputDeviceInfo info;
121 mapper.populateDeviceInfo(&info);
122
123 const InputDeviceInfo::MotionRange* motionRange =
124 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
125 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
126}
127
128static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
129 InputDeviceInfo info;
130 mapper.populateDeviceInfo(&info);
131
132 const InputDeviceInfo::MotionRange* motionRange =
133 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
134 ASSERT_EQ(nullptr, motionRange);
135}
136
Michael Wrightd02c5b62014-02-10 15:10:22 -0800137// --- FakePointerController ---
138
139class FakePointerController : public PointerControllerInterface {
140 bool mHaveBounds;
141 float mMinX, mMinY, mMaxX, mMaxY;
142 float mX, mY;
143 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800144 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145
Michael Wrightd02c5b62014-02-10 15:10:22 -0800146public:
147 FakePointerController() :
148 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800149 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800150 }
151
Michael Wright17db18e2020-06-26 20:51:44 +0100152 virtual ~FakePointerController() {}
153
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154 void setBounds(float minX, float minY, float maxX, float maxY) {
155 mHaveBounds = true;
156 mMinX = minX;
157 mMinY = minY;
158 mMaxX = maxX;
159 mMaxY = maxY;
160 }
161
Chris Yea52ade12020-08-27 16:49:20 -0700162 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800163 mX = x;
164 mY = y;
165 }
166
Chris Yea52ade12020-08-27 16:49:20 -0700167 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168
Chris Yea52ade12020-08-27 16:49:20 -0700169 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170
Chris Yea52ade12020-08-27 16:49:20 -0700171 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172 *outX = mX;
173 *outY = mY;
174 }
175
Chris Yea52ade12020-08-27 16:49:20 -0700176 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800177
Chris Yea52ade12020-08-27 16:49:20 -0700178 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800179 mDisplayId = viewport.displayId;
180 }
181
Arthur Hung7c645402019-01-25 17:45:42 +0800182 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
183 return mSpotsByDisplay;
184 }
185
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186private:
Chris Yea52ade12020-08-27 16:49:20 -0700187 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188 *outMinX = mMinX;
189 *outMinY = mMinY;
190 *outMaxX = mMaxX;
191 *outMaxY = mMaxY;
192 return mHaveBounds;
193 }
194
Chris Yea52ade12020-08-27 16:49:20 -0700195 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800196 mX += deltaX;
197 if (mX < mMinX) mX = mMinX;
198 if (mX > mMaxX) mX = mMaxX;
199 mY += deltaY;
200 if (mY < mMinY) mY = mMinY;
201 if (mY > mMaxY) mY = mMaxY;
202 }
203
Chris Yea52ade12020-08-27 16:49:20 -0700204 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800205
Chris Yea52ade12020-08-27 16:49:20 -0700206 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800207
Chris Yea52ade12020-08-27 16:49:20 -0700208 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800209
Chris Yea52ade12020-08-27 16:49:20 -0700210 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
211 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800212 std::vector<int32_t> newSpots;
213 // Add spots for fingers that are down.
214 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
215 uint32_t id = idBits.clearFirstMarkedBit();
216 newSpots.push_back(id);
217 }
218
219 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800220 }
221
Prabir Pradhan197e0862022-07-01 14:28:00 +0000222 void clearSpots() override { mSpotsByDisplay.clear(); }
Arthur Hung7c645402019-01-25 17:45:42 +0800223
224 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800225};
226
227
228// --- FakeInputReaderPolicy ---
229
230class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700231 std::mutex mLock;
232 std::condition_variable mDevicesChangedCondition;
233
Michael Wrightd02c5b62014-02-10 15:10:22 -0800234 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000235 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700236 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
237 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100238 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700239 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800240
241protected:
Chris Yea52ade12020-08-27 16:49:20 -0700242 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243
244public:
245 FakeInputReaderPolicy() {
246 }
247
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700248 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800249 waitForInputDevices([](bool devicesChanged) {
250 if (!devicesChanged) {
251 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
252 }
253 });
254 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700255
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800256 void assertInputDevicesNotChanged() {
257 waitForInputDevices([](bool devicesChanged) {
258 if (devicesChanged) {
259 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
260 }
261 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700262 }
263
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700264 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100265 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100266 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700267 }
268
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700269 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
270 return mConfig.getDisplayViewportByUniqueId(uniqueId);
271 }
272 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
273 return mConfig.getDisplayViewportByType(type);
274 }
275
276 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
277 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700278 }
279
Prabir Pradhan5632d622021-09-06 07:57:20 -0700280 void addDisplayViewport(DisplayViewport viewport) {
281 mViewports.push_back(std::move(viewport));
282 mConfig.setDisplayViewports(mViewports);
283 }
284
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700285 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000286 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700287 std::optional<uint8_t> physicalPort, ViewportType type) {
288 const bool isRotated =
289 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
290 DisplayViewport v;
291 v.displayId = displayId;
292 v.orientation = orientation;
293 v.logicalLeft = 0;
294 v.logicalTop = 0;
295 v.logicalRight = isRotated ? height : width;
296 v.logicalBottom = isRotated ? width : height;
297 v.physicalLeft = 0;
298 v.physicalTop = 0;
299 v.physicalRight = isRotated ? height : width;
300 v.physicalBottom = isRotated ? width : height;
301 v.deviceWidth = isRotated ? height : width;
302 v.deviceHeight = isRotated ? width : height;
303 v.isActive = isActive;
304 v.uniqueId = uniqueId;
305 v.physicalPort = physicalPort;
306 v.type = type;
307
308 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800309 }
310
Arthur Hung6cd19a42019-08-30 19:04:12 +0800311 bool updateViewport(const DisplayViewport& viewport) {
312 size_t count = mViewports.size();
313 for (size_t i = 0; i < count; i++) {
314 const DisplayViewport& currentViewport = mViewports[i];
315 if (currentViewport.displayId == viewport.displayId) {
316 mViewports[i] = viewport;
317 mConfig.setDisplayViewports(mViewports);
318 return true;
319 }
320 }
321 // no viewport found.
322 return false;
323 }
324
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100325 void addExcludedDeviceName(const std::string& deviceName) {
326 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327 }
328
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700329 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
330 mConfig.portAssociations.insert({inputPort, displayPort});
331 }
332
Christine Franks1ba71cc2021-04-07 14:37:42 -0700333 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
334 const std::string& displayUniqueId) {
335 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
336 }
337
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000338 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700339
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000340 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700341
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000342 void setPointerController(std::shared_ptr<FakePointerController> controller) {
343 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344 }
345
346 const InputReaderConfiguration* getReaderConfiguration() const {
347 return &mConfig;
348 }
349
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800350 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351 return mInputDevices;
352 }
353
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100354 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700355 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700356 return transform;
357 }
358
359 void setTouchAffineTransformation(const TouchAffineTransformation t) {
360 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800361 }
362
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000363 PointerCaptureRequest setPointerCapture(bool enabled) {
364 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
365 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800366 }
367
Arthur Hung7c645402019-01-25 17:45:42 +0800368 void setShowTouches(bool enabled) {
369 mConfig.showTouches = enabled;
370 }
371
Garfield Tan888a6a42020-01-09 11:39:16 -0800372 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
373 mConfig.defaultPointerDisplayId = pointerDisplayId;
374 }
375
HQ Liue6983c72022-04-19 22:14:56 +0000376 void setPointerGestureEnabled(bool enabled) { mConfig.pointerGesturesEnabled = enabled; }
377
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800378 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
379
HQ Liue6983c72022-04-19 22:14:56 +0000380 float getPointerGestureZoomSpeedRatio() { return mConfig.pointerGestureZoomSpeedRatio; }
381
Prabir Pradhanf99d6e72022-04-21 15:28:35 +0000382 void setVelocityControlParams(const VelocityControlParameters& params) {
383 mConfig.pointerVelocityControlParameters = params;
384 mConfig.wheelVelocityControlParameters = params;
385 }
386
Michael Wrightd02c5b62014-02-10 15:10:22 -0800387private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000388 uint32_t mNextPointerCaptureSequenceNumber = 0;
389
Chris Yea52ade12020-08-27 16:49:20 -0700390 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800391 *outConfig = mConfig;
392 }
393
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000394 std::shared_ptr<PointerControllerInterface> obtainPointerController(
395 int32_t /*deviceId*/) override {
396 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800397 }
398
Chris Yea52ade12020-08-27 16:49:20 -0700399 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700400 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800401 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700402 mInputDevicesChanged = true;
403 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800404 }
405
Chris Yea52ade12020-08-27 16:49:20 -0700406 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
407 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700408 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800409 }
410
Chris Yea52ade12020-08-27 16:49:20 -0700411 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800412
413 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
414 std::unique_lock<std::mutex> lock(mLock);
415 base::ScopedLockAssertion assumeLocked(mLock);
416
417 const bool devicesChanged =
418 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
419 return mInputDevicesChanged;
420 });
421 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
422 mInputDevicesChanged = false;
423 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800424};
425
Michael Wrightd02c5b62014-02-10 15:10:22 -0800426// --- FakeEventHub ---
427
428class FakeEventHub : public EventHubInterface {
429 struct KeyInfo {
430 int32_t keyCode;
431 uint32_t flags;
432 };
433
Chris Yef59a2f42020-10-16 12:55:26 -0700434 struct SensorInfo {
435 InputDeviceSensorType sensorType;
436 int32_t sensorDataIndex;
437 };
438
Michael Wrightd02c5b62014-02-10 15:10:22 -0800439 struct Device {
440 InputDeviceIdentifier identifier;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700441 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800442 PropertyMap configuration;
443 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
444 KeyedVector<int, bool> relativeAxes;
445 KeyedVector<int32_t, int32_t> keyCodeStates;
446 KeyedVector<int32_t, int32_t> scanCodeStates;
447 KeyedVector<int32_t, int32_t> switchStates;
448 KeyedVector<int32_t, int32_t> absoluteAxisValue;
449 KeyedVector<int32_t, KeyInfo> keysByScanCode;
450 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
451 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100452 // fake mapping which would normally come from keyCharacterMap
453 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700454 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
455 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800456 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700457 bool enabled;
458
459 status_t enable() {
460 enabled = true;
461 return OK;
462 }
463
464 status_t disable() {
465 enabled = false;
466 return OK;
467 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800468
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700469 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800470 };
471
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700472 std::mutex mLock;
473 std::condition_variable mEventsCondition;
474
Michael Wrightd02c5b62014-02-10 15:10:22 -0800475 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100476 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000477 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600478 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000479 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800480 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
481 // Simulates a device light brightness, from light id to light brightness.
482 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
483 // Simulates a device light intensities, from light id to light intensities map.
484 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
485 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800486
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700487public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800488 virtual ~FakeEventHub() {
489 for (size_t i = 0; i < mDevices.size(); i++) {
490 delete mDevices.valueAt(i);
491 }
492 }
493
Michael Wrightd02c5b62014-02-10 15:10:22 -0800494 FakeEventHub() { }
495
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700496 void addDevice(int32_t deviceId, const std::string& name,
497 ftl::Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800498 Device* device = new Device(classes);
499 device->identifier.name = name;
500 mDevices.add(deviceId, device);
501
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000502 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800503 }
504
505 void removeDevice(int32_t deviceId) {
506 delete mDevices.valueFor(deviceId);
507 mDevices.removeItem(deviceId);
508
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000509 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800510 }
511
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700512 bool isDeviceEnabled(int32_t deviceId) {
513 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700514 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700515 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
516 return false;
517 }
518 return device->enabled;
519 }
520
521 status_t enableDevice(int32_t deviceId) {
522 status_t result;
523 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700524 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700525 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
526 return BAD_VALUE;
527 }
528 if (device->enabled) {
529 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
530 return OK;
531 }
532 result = device->enable();
533 return result;
534 }
535
536 status_t disableDevice(int32_t deviceId) {
537 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700538 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700539 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
540 return BAD_VALUE;
541 }
542 if (!device->enabled) {
543 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
544 return OK;
545 }
546 return device->disable();
547 }
548
Michael Wrightd02c5b62014-02-10 15:10:22 -0800549 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000550 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800551 }
552
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700553 void addConfigurationProperty(int32_t deviceId, const char* key, const char* value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800554 Device* device = getDevice(deviceId);
555 device->configuration.addProperty(key, value);
556 }
557
558 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
559 Device* device = getDevice(deviceId);
560 device->configuration.addAll(configuration);
561 }
562
563 void addAbsoluteAxis(int32_t deviceId, int axis,
564 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
565 Device* device = getDevice(deviceId);
566
567 RawAbsoluteAxisInfo info;
568 info.valid = true;
569 info.minValue = minValue;
570 info.maxValue = maxValue;
571 info.flat = flat;
572 info.fuzz = fuzz;
573 info.resolution = resolution;
574 device->absoluteAxes.add(axis, info);
575 }
576
577 void addRelativeAxis(int32_t deviceId, int32_t axis) {
578 Device* device = getDevice(deviceId);
579 device->relativeAxes.add(axis, true);
580 }
581
582 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
583 Device* device = getDevice(deviceId);
584 device->keyCodeStates.replaceValueFor(keyCode, state);
585 }
586
587 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
588 Device* device = getDevice(deviceId);
589 device->scanCodeStates.replaceValueFor(scanCode, state);
590 }
591
592 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
593 Device* device = getDevice(deviceId);
594 device->switchStates.replaceValueFor(switchCode, state);
595 }
596
597 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
598 Device* device = getDevice(deviceId);
599 device->absoluteAxisValue.replaceValueFor(axis, value);
600 }
601
602 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
603 int32_t keyCode, uint32_t flags) {
604 Device* device = getDevice(deviceId);
605 KeyInfo info;
606 info.keyCode = keyCode;
607 info.flags = flags;
608 if (scanCode) {
609 device->keysByScanCode.add(scanCode, info);
610 }
611 if (usageCode) {
612 device->keysByUsageCode.add(usageCode, info);
613 }
614 }
615
Philip Junker4af3b3d2021-12-14 10:36:55 +0100616 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
617 Device* device = getDevice(deviceId);
618 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
619 }
620
Michael Wrightd02c5b62014-02-10 15:10:22 -0800621 void addLed(int32_t deviceId, int32_t led, bool initialState) {
622 Device* device = getDevice(deviceId);
623 device->leds.add(led, initialState);
624 }
625
Chris Yef59a2f42020-10-16 12:55:26 -0700626 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
627 int32_t sensorDataIndex) {
628 Device* device = getDevice(deviceId);
629 SensorInfo info;
630 info.sensorType = sensorType;
631 info.sensorDataIndex = sensorDataIndex;
632 device->sensorsByAbsCode.emplace(absCode, info);
633 }
634
635 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
636 Device* device = getDevice(deviceId);
637 typename BitArray<MSC_MAX>::Buffer buffer;
638 buffer[mscEvent / 32] = 1 << mscEvent % 32;
639 device->mscBitmask.loadFromBuffer(buffer);
640 }
641
Chris Ye3fdbfef2021-01-06 18:45:18 -0800642 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
643 mRawLightInfos.emplace(rawId, std::move(info));
644 }
645
646 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
647 mLightBrightness.emplace(rawId, brightness);
648 }
649
650 void fakeLightIntensities(int32_t rawId,
651 const std::unordered_map<LightColor, int32_t> intensities) {
652 mLightIntensities.emplace(rawId, std::move(intensities));
653 }
654
Michael Wrightd02c5b62014-02-10 15:10:22 -0800655 bool getLedState(int32_t deviceId, int32_t led) {
656 Device* device = getDevice(deviceId);
657 return device->leds.valueFor(led);
658 }
659
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100660 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800661 return mExcludedDevices;
662 }
663
664 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
665 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800666 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800667 }
668
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000669 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
670 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700671 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800672 RawEvent event;
673 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000674 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800675 event.deviceId = deviceId;
676 event.type = type;
677 event.code = code;
678 event.value = value;
679 mEvents.push_back(event);
680
681 if (type == EV_ABS) {
682 setAbsoluteAxisValue(deviceId, code, value);
683 }
684 }
685
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600686 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
687 std::vector<TouchVideoFrame>> videoFrames) {
688 mVideoFrames = std::move(videoFrames);
689 }
690
Michael Wrightd02c5b62014-02-10 15:10:22 -0800691 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700692 std::unique_lock<std::mutex> lock(mLock);
693 base::ScopedLockAssertion assumeLocked(mLock);
694 const bool queueIsEmpty =
695 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
696 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
697 if (!queueIsEmpty) {
698 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
699 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800700 }
701
702private:
703 Device* getDevice(int32_t deviceId) const {
704 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100705 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800706 }
707
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700708 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800709 Device* device = getDevice(deviceId);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700710 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800711 }
712
Chris Yea52ade12020-08-27 16:49:20 -0700713 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800714 Device* device = getDevice(deviceId);
715 return device ? device->identifier : InputDeviceIdentifier();
716 }
717
Chris Yea52ade12020-08-27 16:49:20 -0700718 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800719
Chris Yea52ade12020-08-27 16:49:20 -0700720 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800721 Device* device = getDevice(deviceId);
722 if (device) {
723 *outConfiguration = device->configuration;
724 }
725 }
726
Chris Yea52ade12020-08-27 16:49:20 -0700727 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
728 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800729 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800730 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800731 ssize_t index = device->absoluteAxes.indexOfKey(axis);
732 if (index >= 0) {
733 *outAxisInfo = device->absoluteAxes.valueAt(index);
734 return OK;
735 }
736 }
737 outAxisInfo->clear();
738 return -1;
739 }
740
Chris Yea52ade12020-08-27 16:49:20 -0700741 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800742 Device* device = getDevice(deviceId);
743 if (device) {
744 return device->relativeAxes.indexOfKey(axis) >= 0;
745 }
746 return false;
747 }
748
Chris Yea52ade12020-08-27 16:49:20 -0700749 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800750
Chris Yef59a2f42020-10-16 12:55:26 -0700751 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
752 Device* device = getDevice(deviceId);
753 if (device) {
754 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
755 }
756 return false;
757 }
758
Chris Yea52ade12020-08-27 16:49:20 -0700759 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
760 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800761 Device* device = getDevice(deviceId);
762 if (device) {
763 const KeyInfo* key = getKey(device, scanCode, usageCode);
764 if (key) {
765 if (outKeycode) {
766 *outKeycode = key->keyCode;
767 }
768 if (outFlags) {
769 *outFlags = key->flags;
770 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700771 if (outMetaState) {
772 *outMetaState = metaState;
773 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800774 return OK;
775 }
776 }
777 return NAME_NOT_FOUND;
778 }
779
780 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
781 if (usageCode) {
782 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
783 if (index >= 0) {
784 return &device->keysByUsageCode.valueAt(index);
785 }
786 }
787 if (scanCode) {
788 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
789 if (index >= 0) {
790 return &device->keysByScanCode.valueAt(index);
791 }
792 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700793 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800794 }
795
Chris Yea52ade12020-08-27 16:49:20 -0700796 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800797
Chris Yef59a2f42020-10-16 12:55:26 -0700798 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
799 int32_t absCode) {
800 Device* device = getDevice(deviceId);
801 if (!device) {
802 return Errorf("Sensor device not found.");
803 }
804 auto it = device->sensorsByAbsCode.find(absCode);
805 if (it == device->sensorsByAbsCode.end()) {
806 return Errorf("Sensor map not found.");
807 }
808 const SensorInfo& info = it->second;
809 return std::make_pair(info.sensorType, info.sensorDataIndex);
810 }
811
Chris Yea52ade12020-08-27 16:49:20 -0700812 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800813 mExcludedDevices = devices;
814 }
815
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000816 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
817 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800818
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000819 const size_t filledSize = std::min(mEvents.size(), bufferSize);
820 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
821
822 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700823 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000824 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800825 }
826
Chris Yea52ade12020-08-27 16:49:20 -0700827 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600828 auto it = mVideoFrames.find(deviceId);
829 if (it != mVideoFrames.end()) {
830 std::vector<TouchVideoFrame> frames = std::move(it->second);
831 mVideoFrames.erase(deviceId);
832 return frames;
833 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800834 return {};
835 }
836
Chris Yea52ade12020-08-27 16:49:20 -0700837 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800838 Device* device = getDevice(deviceId);
839 if (device) {
840 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
841 if (index >= 0) {
842 return device->scanCodeStates.valueAt(index);
843 }
844 }
845 return AKEY_STATE_UNKNOWN;
846 }
847
Chris Yea52ade12020-08-27 16:49:20 -0700848 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800849 Device* device = getDevice(deviceId);
850 if (device) {
851 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
852 if (index >= 0) {
853 return device->keyCodeStates.valueAt(index);
854 }
855 }
856 return AKEY_STATE_UNKNOWN;
857 }
858
Chris Yea52ade12020-08-27 16:49:20 -0700859 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800860 Device* device = getDevice(deviceId);
861 if (device) {
862 ssize_t index = device->switchStates.indexOfKey(sw);
863 if (index >= 0) {
864 return device->switchStates.valueAt(index);
865 }
866 }
867 return AKEY_STATE_UNKNOWN;
868 }
869
Chris Yea52ade12020-08-27 16:49:20 -0700870 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
871 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800872 Device* device = getDevice(deviceId);
873 if (device) {
874 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
875 if (index >= 0) {
876 *outValue = device->absoluteAxisValue.valueAt(index);
877 return OK;
878 }
879 }
880 *outValue = 0;
881 return -1;
882 }
883
Philip Junker4af3b3d2021-12-14 10:36:55 +0100884 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
885 Device* device = getDevice(deviceId);
886 if (!device) {
887 return AKEYCODE_UNKNOWN;
888 }
889 auto it = device->keyCodeMapping.find(locationKeyCode);
890 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
891 }
892
Chris Yea52ade12020-08-27 16:49:20 -0700893 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700894 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700895 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800896 bool result = false;
897 Device* device = getDevice(deviceId);
898 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700899 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700900 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800901 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
902 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
903 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800904 }
905 }
906 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
907 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
908 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909 }
910 }
911 }
912 }
913 return result;
914 }
915
Chris Yea52ade12020-08-27 16:49:20 -0700916 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800917 Device* device = getDevice(deviceId);
918 if (device) {
919 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
920 return index >= 0;
921 }
922 return false;
923 }
924
Arthur Hungcb40a002021-08-03 14:31:01 +0000925 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
926 Device* device = getDevice(deviceId);
927 if (!device) {
928 return false;
929 }
930 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
931 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
932 return true;
933 }
934 }
935 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
936 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
937 return true;
938 }
939 }
940 return false;
941 }
942
Chris Yea52ade12020-08-27 16:49:20 -0700943 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800944 Device* device = getDevice(deviceId);
945 return device && device->leds.indexOfKey(led) >= 0;
946 }
947
Chris Yea52ade12020-08-27 16:49:20 -0700948 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800949 Device* device = getDevice(deviceId);
950 if (device) {
951 ssize_t index = device->leds.indexOfKey(led);
952 if (index >= 0) {
953 device->leds.replaceValueAt(led, on);
954 } else {
955 ADD_FAILURE()
956 << "Attempted to set the state of an LED that the EventHub declared "
957 "was not present. led=" << led;
958 }
959 }
960 }
961
Chris Yea52ade12020-08-27 16:49:20 -0700962 void getVirtualKeyDefinitions(
963 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800964 outVirtualKeys.clear();
965
966 Device* device = getDevice(deviceId);
967 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800968 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800969 }
970 }
971
Chris Yea52ade12020-08-27 16:49:20 -0700972 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700973 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800974 }
975
Chris Yea52ade12020-08-27 16:49:20 -0700976 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800977 return false;
978 }
979
Chris Yea52ade12020-08-27 16:49:20 -0700980 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800981
Chris Yea52ade12020-08-27 16:49:20 -0700982 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800983
Chris Ye87143712020-11-10 05:05:58 +0000984 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
985
Chris Yee2b1e5c2021-03-10 22:45:12 -0800986 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
987 return BATTERY_CAPACITY;
988 }
Kim Low03ea0352020-11-06 12:45:07 -0800989
Chris Yee2b1e5c2021-03-10 22:45:12 -0800990 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
991 return BATTERY_STATUS;
992 }
993
994 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
995
996 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
997 return std::nullopt;
998 }
Kim Low03ea0352020-11-06 12:45:07 -0800999
Chris Ye3fdbfef2021-01-06 18:45:18 -08001000 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
1001 std::vector<int32_t> ids;
1002 for (const auto& [rawId, info] : mRawLightInfos) {
1003 ids.push_back(rawId);
1004 }
1005 return ids;
1006 }
1007
1008 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
1009 auto it = mRawLightInfos.find(lightId);
1010 if (it == mRawLightInfos.end()) {
1011 return std::nullopt;
1012 }
1013 return it->second;
1014 }
1015
1016 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1017 mLightBrightness.emplace(lightId, brightness);
1018 }
1019
1020 void setLightIntensities(int32_t deviceId, int32_t lightId,
1021 std::unordered_map<LightColor, int32_t> intensities) override {
1022 mLightIntensities.emplace(lightId, intensities);
1023 };
1024
1025 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
1026 auto lightIt = mLightBrightness.find(lightId);
1027 if (lightIt == mLightBrightness.end()) {
1028 return std::nullopt;
1029 }
1030 return lightIt->second;
1031 }
1032
1033 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
1034 int32_t deviceId, int32_t lightId) override {
1035 auto lightIt = mLightIntensities.find(lightId);
1036 if (lightIt == mLightIntensities.end()) {
1037 return std::nullopt;
1038 }
1039 return lightIt->second;
1040 };
1041
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001042 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001043 return false;
1044 }
1045
Chris Yea52ade12020-08-27 16:49:20 -07001046 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001047
Chris Yea52ade12020-08-27 16:49:20 -07001048 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001049
Chris Yea52ade12020-08-27 16:49:20 -07001050 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001051
Chris Yea52ade12020-08-27 16:49:20 -07001052 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001053};
1054
Michael Wrightd02c5b62014-02-10 15:10:22 -08001055// --- FakeInputMapper ---
1056
1057class FakeInputMapper : public InputMapper {
1058 uint32_t mSources;
1059 int32_t mKeyboardType;
1060 int32_t mMetaState;
1061 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1062 KeyedVector<int32_t, int32_t> mScanCodeStates;
1063 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001064 // fake mapping which would normally come from keyCharacterMap
1065 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001066 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001067
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001068 std::mutex mLock;
1069 std::condition_variable mStateChangedCondition;
1070 bool mConfigureWasCalled GUARDED_BY(mLock);
1071 bool mResetWasCalled GUARDED_BY(mLock);
1072 bool mProcessWasCalled GUARDED_BY(mLock);
1073 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001074
Arthur Hungc23540e2018-11-29 20:42:11 +08001075 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001076public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001077 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1078 : InputMapper(deviceContext),
1079 mSources(sources),
1080 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001081 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001082 mConfigureWasCalled(false),
1083 mResetWasCalled(false),
1084 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001085
Chris Yea52ade12020-08-27 16:49:20 -07001086 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001087
1088 void setKeyboardType(int32_t keyboardType) {
1089 mKeyboardType = keyboardType;
1090 }
1091
1092 void setMetaState(int32_t metaState) {
1093 mMetaState = metaState;
1094 }
1095
1096 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001097 std::unique_lock<std::mutex> lock(mLock);
1098 base::ScopedLockAssertion assumeLocked(mLock);
1099 const bool configureCalled =
1100 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1101 return mConfigureWasCalled;
1102 });
1103 if (!configureCalled) {
1104 FAIL() << "Expected configure() to have been called.";
1105 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001106 mConfigureWasCalled = false;
1107 }
1108
1109 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001110 std::unique_lock<std::mutex> lock(mLock);
1111 base::ScopedLockAssertion assumeLocked(mLock);
1112 const bool resetCalled =
1113 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1114 return mResetWasCalled;
1115 });
1116 if (!resetCalled) {
1117 FAIL() << "Expected reset() to have been called.";
1118 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001119 mResetWasCalled = false;
1120 }
1121
Yi Kong9b14ac62018-07-17 13:48:38 -07001122 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001123 std::unique_lock<std::mutex> lock(mLock);
1124 base::ScopedLockAssertion assumeLocked(mLock);
1125 const bool processCalled =
1126 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1127 return mProcessWasCalled;
1128 });
1129 if (!processCalled) {
1130 FAIL() << "Expected process() to have been called.";
1131 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001132 if (outLastEvent) {
1133 *outLastEvent = mLastEvent;
1134 }
1135 mProcessWasCalled = false;
1136 }
1137
1138 void setKeyCodeState(int32_t keyCode, int32_t state) {
1139 mKeyCodeStates.replaceValueFor(keyCode, state);
1140 }
1141
1142 void setScanCodeState(int32_t scanCode, int32_t state) {
1143 mScanCodeStates.replaceValueFor(scanCode, state);
1144 }
1145
1146 void setSwitchState(int32_t switchCode, int32_t state) {
1147 mSwitchStates.replaceValueFor(switchCode, state);
1148 }
1149
1150 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001151 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001152 }
1153
Philip Junker4af3b3d2021-12-14 10:36:55 +01001154 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1155 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1156 }
1157
Michael Wrightd02c5b62014-02-10 15:10:22 -08001158private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001159 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001160
Chris Yea52ade12020-08-27 16:49:20 -07001161 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001162 InputMapper::populateDeviceInfo(deviceInfo);
1163
1164 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1165 deviceInfo->setKeyboardType(mKeyboardType);
1166 }
1167 }
1168
Chris Yea52ade12020-08-27 16:49:20 -07001169 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001170 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001171 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001172
1173 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001174 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001175 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1176 mViewport = config->getDisplayViewportByPort(*displayPort);
1177 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001178
1179 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001180 }
1181
Chris Yea52ade12020-08-27 16:49:20 -07001182 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001183 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001184 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001185 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001186 }
1187
Chris Yea52ade12020-08-27 16:49:20 -07001188 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001189 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001190 mLastEvent = *rawEvent;
1191 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001192 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001193 }
1194
Chris Yea52ade12020-08-27 16:49:20 -07001195 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001196 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1197 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1198 }
1199
Philip Junker4af3b3d2021-12-14 10:36:55 +01001200 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1201 auto it = mKeyCodeMapping.find(locationKeyCode);
1202 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1203 }
1204
Chris Yea52ade12020-08-27 16:49:20 -07001205 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001206 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1207 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1208 }
1209
Chris Yea52ade12020-08-27 16:49:20 -07001210 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001211 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1212 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1213 }
1214
Chris Yea52ade12020-08-27 16:49:20 -07001215 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001216 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -07001217 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001218 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001219 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1220 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1221 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001222 }
1223 }
1224 }
Chris Yea52ade12020-08-27 16:49:20 -07001225 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001226 return result;
1227 }
1228
1229 virtual int32_t getMetaState() {
1230 return mMetaState;
1231 }
1232
1233 virtual void fadePointer() {
1234 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001235
1236 virtual std::optional<int32_t> getAssociatedDisplay() {
1237 if (mViewport) {
1238 return std::make_optional(mViewport->displayId);
1239 }
1240 return std::nullopt;
1241 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001242};
1243
1244
1245// --- InstrumentedInputReader ---
1246
1247class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001248 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001249
1250public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001251 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1252 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001253 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001254 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001255
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001256 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001257
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001258 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001259
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001260 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001261 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001262 InputDeviceIdentifier identifier;
1263 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001264 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001265 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001266 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001267 }
1268
Prabir Pradhan28efc192019-11-05 01:10:04 +00001269 // Make the protected loopOnce method accessible to tests.
1270 using InputReader::loopOnce;
1271
Michael Wrightd02c5b62014-02-10 15:10:22 -08001272protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001273 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1274 const InputDeviceIdentifier& identifier)
1275 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001276 if (!mNextDevices.empty()) {
1277 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1278 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001279 return device;
1280 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001281 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001282 }
1283
arthurhungdcef2dc2020-08-11 14:47:50 +08001284 // --- FakeInputReaderContext ---
1285 class FakeInputReaderContext : public ContextImpl {
1286 int32_t mGlobalMetaState;
1287 bool mUpdateGlobalMetaStateWasCalled;
1288 int32_t mGeneration;
1289
1290 public:
1291 FakeInputReaderContext(InputReader* reader)
1292 : ContextImpl(reader),
1293 mGlobalMetaState(0),
1294 mUpdateGlobalMetaStateWasCalled(false),
1295 mGeneration(1) {}
1296
1297 virtual ~FakeInputReaderContext() {}
1298
1299 void assertUpdateGlobalMetaStateWasCalled() {
1300 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1301 << "Expected updateGlobalMetaState() to have been called.";
1302 mUpdateGlobalMetaStateWasCalled = false;
1303 }
1304
1305 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1306
1307 uint32_t getGeneration() { return mGeneration; }
1308
1309 void updateGlobalMetaState() override {
1310 mUpdateGlobalMetaStateWasCalled = true;
1311 ContextImpl::updateGlobalMetaState();
1312 }
1313
1314 int32_t getGlobalMetaState() override {
1315 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1316 }
1317
1318 int32_t bumpGeneration() override {
1319 mGeneration = ContextImpl::bumpGeneration();
1320 return mGeneration;
1321 }
1322 } mFakeContext;
1323
Michael Wrightd02c5b62014-02-10 15:10:22 -08001324 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001325
1326public:
1327 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001328};
1329
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001330// --- InputReaderPolicyTest ---
1331class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001332protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001333 sp<FakeInputReaderPolicy> mFakePolicy;
1334
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001335 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -07001336 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001337};
1338
1339/**
1340 * Check that empty set of viewports is an acceptable configuration.
1341 * Also try to get internal viewport two different ways - by type and by uniqueId.
1342 *
1343 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1344 * Such configuration is not currently allowed.
1345 */
1346TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001347 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001348
1349 // We didn't add any viewports yet, so there shouldn't be any.
1350 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001351 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001352 ASSERT_FALSE(internalViewport);
1353
1354 // Add an internal viewport, then clear it
1355 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001356 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001357 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001358
1359 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001360 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001361 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001362 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001363
1364 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001365 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001366 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001367 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001368
1369 mFakePolicy->clearViewports();
1370 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001371 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001372 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001373 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001374 ASSERT_FALSE(internalViewport);
1375}
1376
1377TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1378 const std::string internalUniqueId = "local:0";
1379 const std::string externalUniqueId = "local:1";
1380 const std::string virtualUniqueId1 = "virtual:2";
1381 const std::string virtualUniqueId2 = "virtual:3";
1382 constexpr int32_t virtualDisplayId1 = 2;
1383 constexpr int32_t virtualDisplayId2 = 3;
1384
1385 // Add an internal viewport
1386 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001387 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1388 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001389 // Add an external viewport
1390 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001391 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1392 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001393 // Add an virtual viewport
1394 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001395 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1396 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001397 // Add another virtual viewport
1398 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001399 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1400 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001401
1402 // Check matching by type for internal
1403 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001404 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001405 ASSERT_TRUE(internalViewport);
1406 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1407
1408 // Check matching by type for external
1409 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001410 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001411 ASSERT_TRUE(externalViewport);
1412 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1413
1414 // Check matching by uniqueId for virtual viewport #1
1415 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001416 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001417 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001418 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001419 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1420 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1421
1422 // Check matching by uniqueId for virtual viewport #2
1423 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001424 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001425 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001426 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001427 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1428 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1429}
1430
1431
1432/**
1433 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1434 * that lookup works by checking display id.
1435 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1436 */
1437TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1438 const std::string uniqueId1 = "uniqueId1";
1439 const std::string uniqueId2 = "uniqueId2";
1440 constexpr int32_t displayId1 = 2;
1441 constexpr int32_t displayId2 = 3;
1442
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001443 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1444 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001445 for (const ViewportType& type : types) {
1446 mFakePolicy->clearViewports();
1447 // Add a viewport
1448 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001449 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1450 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001451 // Add another viewport
1452 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001453 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1454 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001455
1456 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001457 std::optional<DisplayViewport> viewport1 =
1458 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001459 ASSERT_TRUE(viewport1);
1460 ASSERT_EQ(displayId1, viewport1->displayId);
1461 ASSERT_EQ(type, viewport1->type);
1462
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001463 std::optional<DisplayViewport> viewport2 =
1464 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001465 ASSERT_TRUE(viewport2);
1466 ASSERT_EQ(displayId2, viewport2->displayId);
1467 ASSERT_EQ(type, viewport2->type);
1468
1469 // When there are multiple viewports of the same kind, and uniqueId is not specified
1470 // in the call to getDisplayViewport, then that situation is not supported.
1471 // The viewports can be stored in any order, so we cannot rely on the order, since that
1472 // is just implementation detail.
1473 // However, we can check that it still returns *a* viewport, we just cannot assert
1474 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001475 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001476 ASSERT_TRUE(someViewport);
1477 }
1478}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001479
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001480/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001481 * When we have multiple internal displays make sure we always return the default display when
1482 * querying by type.
1483 */
1484TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1485 const std::string uniqueId1 = "uniqueId1";
1486 const std::string uniqueId2 = "uniqueId2";
1487 constexpr int32_t nonDefaultDisplayId = 2;
1488 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1489 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1490
1491 // Add the default display first and ensure it gets returned.
1492 mFakePolicy->clearViewports();
1493 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001494 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001495 ViewportType::INTERNAL);
1496 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001497 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001498 ViewportType::INTERNAL);
1499
1500 std::optional<DisplayViewport> viewport =
1501 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1502 ASSERT_TRUE(viewport);
1503 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1504 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1505
1506 // Add the default display second to make sure order doesn't matter.
1507 mFakePolicy->clearViewports();
1508 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001509 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001510 ViewportType::INTERNAL);
1511 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001512 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001513 ViewportType::INTERNAL);
1514
1515 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1516 ASSERT_TRUE(viewport);
1517 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1518 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1519}
1520
1521/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001522 * Check getDisplayViewportByPort
1523 */
1524TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001525 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001526 const std::string uniqueId1 = "uniqueId1";
1527 const std::string uniqueId2 = "uniqueId2";
1528 constexpr int32_t displayId1 = 1;
1529 constexpr int32_t displayId2 = 2;
1530 const uint8_t hdmi1 = 0;
1531 const uint8_t hdmi2 = 1;
1532 const uint8_t hdmi3 = 2;
1533
1534 mFakePolicy->clearViewports();
1535 // Add a viewport that's associated with some display port that's not of interest.
1536 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001537 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1538 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001539 // Add another viewport, connected to HDMI1 port
1540 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001541 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1542 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001543
1544 // Check that correct display viewport was returned by comparing the display ports.
1545 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1546 ASSERT_TRUE(hdmi1Viewport);
1547 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1548 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1549
1550 // Check that we can still get the same viewport using the uniqueId
1551 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1552 ASSERT_TRUE(hdmi1Viewport);
1553 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1554 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1555 ASSERT_EQ(type, hdmi1Viewport->type);
1556
1557 // Check that we cannot find a port with "HDMI2", because we never added one
1558 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1559 ASSERT_FALSE(hdmi2Viewport);
1560}
1561
Michael Wrightd02c5b62014-02-10 15:10:22 -08001562// --- InputReaderTest ---
1563
1564class InputReaderTest : public testing::Test {
1565protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001566 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001567 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001568 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001569 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001570
Chris Yea52ade12020-08-27 16:49:20 -07001571 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001572 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001573 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001574 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001575
Prabir Pradhan28efc192019-11-05 01:10:04 +00001576 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001577 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001578 }
1579
Chris Yea52ade12020-08-27 16:49:20 -07001580 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001581 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001582 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001583 }
1584
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001585 void addDevice(int32_t eventHubId, const std::string& name,
1586 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001587 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001588
1589 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001590 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001591 }
1592 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001593 mReader->loopOnce();
1594 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001595 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1596 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001597 }
1598
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001599 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001600 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001601 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001602 }
1603
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001604 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001605 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001606 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001607 }
1608
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001609 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001610 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001611 ftl::Flags<InputDeviceClass> classes,
1612 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001613 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001614 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1615 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001616 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001617 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001618 return mapper;
1619 }
1620};
1621
Chris Ye98d3f532020-10-01 21:48:59 -07001622TEST_F(InputReaderTest, PolicyGetInputDevices) {
1623 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001624 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001625 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001626
1627 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001628 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001629 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001630 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001631 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001632 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1633 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001634 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001635}
1636
Chris Yee7310032020-09-22 15:36:28 -07001637TEST_F(InputReaderTest, GetMergedInputDevices) {
1638 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1639 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1640 // Add two subdevices to device
1641 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1642 // Must add at least one mapper or the device will be ignored!
1643 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1644 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1645
1646 // Push same device instance for next device to be added, so they'll have same identifier.
1647 mReader->pushNextDevice(device);
1648 mReader->pushNextDevice(device);
1649 ASSERT_NO_FATAL_FAILURE(
1650 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1651 ASSERT_NO_FATAL_FAILURE(
1652 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1653
1654 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001655 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001656}
1657
Chris Yee14523a2020-12-19 13:46:00 -08001658TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1659 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1660 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1661 // Add two subdevices to device
1662 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1663 // Must add at least one mapper or the device will be ignored!
1664 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1665 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1666
1667 // Push same device instance for next device to be added, so they'll have same identifier.
1668 mReader->pushNextDevice(device);
1669 mReader->pushNextDevice(device);
1670 // Sensor device is initially disabled
1671 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1672 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1673 nullptr));
1674 // Device is disabled because the only sub device is a sensor device and disabled initially.
1675 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1676 ASSERT_FALSE(device->isEnabled());
1677 ASSERT_NO_FATAL_FAILURE(
1678 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1679 // The merged device is enabled if any sub device is enabled
1680 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1681 ASSERT_TRUE(device->isEnabled());
1682}
1683
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001684TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001685 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001686 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001687 constexpr int32_t eventHubId = 1;
1688 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001689 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001690 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001691 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001692 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001693
Yi Kong9b14ac62018-07-17 13:48:38 -07001694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001695
1696 NotifyDeviceResetArgs resetArgs;
1697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001698 ASSERT_EQ(deviceId, resetArgs.deviceId);
1699
1700 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001701 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001702 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001703
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001705 ASSERT_EQ(deviceId, resetArgs.deviceId);
1706 ASSERT_EQ(device->isEnabled(), false);
1707
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001708 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001709 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001712 ASSERT_EQ(device->isEnabled(), false);
1713
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001714 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001715 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001717 ASSERT_EQ(deviceId, resetArgs.deviceId);
1718 ASSERT_EQ(device->isEnabled(), true);
1719}
1720
Michael Wrightd02c5b62014-02-10 15:10:22 -08001721TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001722 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001723 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001724 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001725 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001726 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001727 AINPUT_SOURCE_KEYBOARD, nullptr);
1728 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001729
1730 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1731 AINPUT_SOURCE_ANY, AKEYCODE_A))
1732 << "Should return unknown when the device id is >= 0 but unknown.";
1733
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001734 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1735 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1736 << "Should return unknown when the device id is valid but the sources are not "
1737 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001738
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001739 ASSERT_EQ(AKEY_STATE_DOWN,
1740 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1741 AKEYCODE_A))
1742 << "Should return value provided by mapper when device id is valid and the device "
1743 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001744
1745 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1746 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1747 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1748
1749 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1750 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1751 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1752}
1753
Philip Junker4af3b3d2021-12-14 10:36:55 +01001754TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1755 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1756 constexpr int32_t eventHubId = 1;
1757 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1758 InputDeviceClass::KEYBOARD,
1759 AINPUT_SOURCE_KEYBOARD, nullptr);
1760 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1761
1762 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1763 << "Should return unknown when the device with the specified id is not found.";
1764
1765 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1766 << "Should return correct mapping when device id is valid and mapping exists.";
1767
1768 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1769 << "Should return the location key code when device id is valid and there's no "
1770 "mapping.";
1771}
1772
1773TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1774 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1775 constexpr int32_t eventHubId = 1;
1776 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1777 InputDeviceClass::JOYSTICK,
1778 AINPUT_SOURCE_GAMEPAD, nullptr);
1779 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1780
1781 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1782 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1783}
1784
Michael Wrightd02c5b62014-02-10 15:10:22 -08001785TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001786 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001787 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001788 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001789 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001790 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001791 AINPUT_SOURCE_KEYBOARD, nullptr);
1792 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001793
1794 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1795 AINPUT_SOURCE_ANY, KEY_A))
1796 << "Should return unknown when the device id is >= 0 but unknown.";
1797
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001798 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1799 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1800 << "Should return unknown when the device id is valid but the sources are not "
1801 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001802
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001803 ASSERT_EQ(AKEY_STATE_DOWN,
1804 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1805 KEY_A))
1806 << "Should return value provided by mapper when device id is valid and the device "
1807 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001808
1809 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1810 AINPUT_SOURCE_TRACKBALL, KEY_A))
1811 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1812
1813 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1814 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1815 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1816}
1817
1818TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001819 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001820 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001821 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001822 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001823 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001824 AINPUT_SOURCE_KEYBOARD, nullptr);
1825 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001826
1827 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1828 AINPUT_SOURCE_ANY, SW_LID))
1829 << "Should return unknown when the device id is >= 0 but unknown.";
1830
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001831 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1832 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1833 << "Should return unknown when the device id is valid but the sources are not "
1834 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001835
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001836 ASSERT_EQ(AKEY_STATE_DOWN,
1837 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1838 SW_LID))
1839 << "Should return value provided by mapper when device id is valid and the device "
1840 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001841
1842 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1843 AINPUT_SOURCE_TRACKBALL, SW_LID))
1844 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1845
1846 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1847 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1848 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1849}
1850
1851TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001852 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001853 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001854 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001855 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001856 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001857 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001858
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001859 mapper.addSupportedKeyCode(AKEYCODE_A);
1860 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001861
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001862 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001863 uint8_t flags[4] = { 0, 0, 0, 1 };
1864
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001865 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08001866 << "Should return false when device id is >= 0 but unknown.";
1867 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1868
1869 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001870 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001871 << "Should return false when device id is valid but the sources are not supported by "
1872 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001873 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1874
1875 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001876 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001877 keyCodes, flags))
1878 << "Should return value provided by mapper when device id is valid and the device "
1879 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001880 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1881
1882 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001883 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1884 << "Should return false when the device id is < 0 but the sources are not supported by "
1885 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001886 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1887
1888 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001889 ASSERT_TRUE(
1890 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1891 << "Should return value provided by mapper when device id is < 0 and one of the "
1892 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001893 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1894}
1895
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001896TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001897 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001898 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001899
1900 NotifyConfigurationChangedArgs args;
1901
1902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1903 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1904}
1905
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001906TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001907 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001908 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001909 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001910 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001911 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001912 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001913 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001914 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001915
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001916 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001917 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001918 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1919
1920 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001921 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001922 ASSERT_EQ(when, event.when);
1923 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001924 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001925 ASSERT_EQ(EV_KEY, event.type);
1926 ASSERT_EQ(KEY_A, event.code);
1927 ASSERT_EQ(1, event.value);
1928}
1929
Garfield Tan1c7bc862020-01-28 13:24:04 -08001930TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001931 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001932 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001933 constexpr int32_t eventHubId = 1;
1934 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001935 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001936 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001937 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001938 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001939
1940 NotifyDeviceResetArgs resetArgs;
1941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001942 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001943
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001944 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001945 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001947 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001948 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001949
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001950 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001951 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001953 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001954 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001955
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001956 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001957 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001959 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001960 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001961}
1962
Garfield Tan1c7bc862020-01-28 13:24:04 -08001963TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1964 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001965 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001966 constexpr int32_t eventHubId = 1;
1967 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1968 // Must add at least one mapper or the device will be ignored!
1969 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001970 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001971 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1972
1973 NotifyDeviceResetArgs resetArgs;
1974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1975 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1976}
1977
Arthur Hungc23540e2018-11-29 20:42:11 +08001978TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001979 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001980 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001981 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001982 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001983 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1984 FakeInputMapper& mapper =
1985 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001986 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001987
1988 const uint8_t hdmi1 = 1;
1989
1990 // Associated touch screen with second display.
1991 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1992
1993 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001994 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001995 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001996 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001997 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001998 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001999 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002000 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002001 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002002 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002003
2004 // Add the device, and make sure all of the callbacks are triggered.
2005 // The device is added after the input port associations are processed since
2006 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002007 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002010 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002011
Arthur Hung2c9a3342019-07-23 14:18:59 +08002012 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002013 ASSERT_EQ(deviceId, device->getId());
2014 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2015 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002016
2017 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002018 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002019 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002020 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002021}
2022
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002023TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2024 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002025 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002026 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2027 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2028 // Must add at least one mapper or the device will be ignored!
2029 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2030 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2031 mReader->pushNextDevice(device);
2032 mReader->pushNextDevice(device);
2033 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2034 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2035
2036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2037
2038 NotifyDeviceResetArgs resetArgs;
2039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2040 ASSERT_EQ(deviceId, resetArgs.deviceId);
2041 ASSERT_TRUE(device->isEnabled());
2042 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2043 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2044
2045 disableDevice(deviceId);
2046 mReader->loopOnce();
2047
2048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2049 ASSERT_EQ(deviceId, resetArgs.deviceId);
2050 ASSERT_FALSE(device->isEnabled());
2051 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2052 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2053
2054 enableDevice(deviceId);
2055 mReader->loopOnce();
2056
2057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2058 ASSERT_EQ(deviceId, resetArgs.deviceId);
2059 ASSERT_TRUE(device->isEnabled());
2060 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2061 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2062}
2063
2064TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2065 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002066 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002067 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2068 // Add two subdevices to device
2069 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2070 FakeInputMapper& mapperDevice1 =
2071 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2072 FakeInputMapper& mapperDevice2 =
2073 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2074 mReader->pushNextDevice(device);
2075 mReader->pushNextDevice(device);
2076 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2077 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2078
2079 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2080 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2081
2082 ASSERT_EQ(AKEY_STATE_DOWN,
2083 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2084 ASSERT_EQ(AKEY_STATE_DOWN,
2085 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2086 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2087 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2088}
2089
Prabir Pradhan7e186182020-11-10 13:56:45 -08002090TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2091 NotifyPointerCaptureChangedArgs args;
2092
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002093 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002094 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2095 mReader->loopOnce();
2096 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002097 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2098 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002099
2100 mFakePolicy->setPointerCapture(false);
2101 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2102 mReader->loopOnce();
2103 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002104 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002105
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002106 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002107 // does not change.
2108 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2109 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002110 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002111}
2112
Chris Ye87143712020-11-10 05:05:58 +00002113class FakeVibratorInputMapper : public FakeInputMapper {
2114public:
2115 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2116 : FakeInputMapper(deviceContext, sources) {}
2117
2118 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2119};
2120
2121TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2122 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002123 ftl::Flags<InputDeviceClass> deviceClass =
2124 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002125 constexpr int32_t eventHubId = 1;
2126 const char* DEVICE_LOCATION = "BLUETOOTH";
2127 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2128 FakeVibratorInputMapper& mapper =
2129 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2130 mReader->pushNextDevice(device);
2131
2132 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2133 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2134
2135 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2136 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2137}
2138
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002139// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002140
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002141class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002142public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002143 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002144
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002145 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002146
2147 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2148
2149 void dump(std::string& dump) override {}
2150
2151 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2152 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002153 }
2154
Chris Yee2b1e5c2021-03-10 22:45:12 -08002155 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2156 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002157 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002158
2159 bool setLightColor(int32_t lightId, int32_t color) override {
2160 getDeviceContext().setLightBrightness(lightId, color >> 24);
2161 return true;
2162 }
2163
2164 std::optional<int32_t> getLightColor(int32_t lightId) override {
2165 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2166 if (!result.has_value()) {
2167 return std::nullopt;
2168 }
2169 return result.value() << 24;
2170 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002171
2172 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2173
2174 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2175
2176private:
2177 InputDeviceContext& mDeviceContext;
2178 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2179 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002180};
2181
Chris Yee2b1e5c2021-03-10 22:45:12 -08002182TEST_F(InputReaderTest, BatteryGetCapacity) {
2183 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002184 ftl::Flags<InputDeviceClass> deviceClass =
2185 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002186 constexpr int32_t eventHubId = 1;
2187 const char* DEVICE_LOCATION = "BLUETOOTH";
2188 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002189 FakePeripheralController& controller =
2190 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002191 mReader->pushNextDevice(device);
2192
2193 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2194
2195 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2196 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2197}
2198
2199TEST_F(InputReaderTest, BatteryGetStatus) {
2200 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002201 ftl::Flags<InputDeviceClass> deviceClass =
2202 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002203 constexpr int32_t eventHubId = 1;
2204 const char* DEVICE_LOCATION = "BLUETOOTH";
2205 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002206 FakePeripheralController& controller =
2207 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002208 mReader->pushNextDevice(device);
2209
2210 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2211
2212 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2213 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2214}
2215
Chris Ye3fdbfef2021-01-06 18:45:18 -08002216TEST_F(InputReaderTest, LightGetColor) {
2217 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002218 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002219 constexpr int32_t eventHubId = 1;
2220 const char* DEVICE_LOCATION = "BLUETOOTH";
2221 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002222 FakePeripheralController& controller =
2223 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002224 mReader->pushNextDevice(device);
2225 RawLightInfo info = {.id = 1,
2226 .name = "Mono",
2227 .maxBrightness = 255,
2228 .flags = InputLightClass::BRIGHTNESS,
2229 .path = ""};
2230 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2231 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2232
2233 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002234
Chris Yee2b1e5c2021-03-10 22:45:12 -08002235 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2236 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002237 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2238 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2239}
2240
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002241// --- InputReaderIntegrationTest ---
2242
2243// These tests create and interact with the InputReader only through its interface.
2244// The InputReader is started during SetUp(), which starts its processing in its own
2245// thread. The tests use linux uinput to emulate input devices.
2246// NOTE: Interacting with the physical device while these tests are running may cause
2247// the tests to fail.
2248class InputReaderIntegrationTest : public testing::Test {
2249protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002250 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002251 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002252 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002253
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002254 std::shared_ptr<FakePointerController> mFakePointerController;
2255
Chris Yea52ade12020-08-27 16:49:20 -07002256 void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002257 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002258 mFakePointerController = std::make_shared<FakePointerController>();
2259 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002260 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2261 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002262
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002263 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2264 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002265 ASSERT_EQ(mReader->start(), OK);
2266
2267 // Since this test is run on a real device, all the input devices connected
2268 // to the test device will show up in mReader. We wait for those input devices to
2269 // show up before beginning the tests.
2270 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2271 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2272 }
2273
Chris Yea52ade12020-08-27 16:49:20 -07002274 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002275 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002276 mReader.reset();
2277 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002278 mFakePolicy.clear();
2279 }
2280};
2281
2282TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2283 // An invalid input device that is only used for this test.
2284 class InvalidUinputDevice : public UinputDevice {
2285 public:
2286 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2287
2288 private:
2289 void configureDevice(int fd, uinput_user_dev* device) override {}
2290 };
2291
2292 const size_t numDevices = mFakePolicy->getInputDevices().size();
2293
2294 // UinputDevice does not set any event or key bits, so InputReader should not
2295 // consider it as a valid device.
2296 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2297 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2298 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2299 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2300
2301 invalidDevice.reset();
2302 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2303 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2304 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2305}
2306
2307TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2308 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2309
2310 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2311 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2312 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2313 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2314
2315 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002316 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002317 const auto& it =
2318 std::find_if(inputDevices.begin(), inputDevices.end(),
2319 [&keyboard](const InputDeviceInfo& info) {
2320 return info.getIdentifier().name == keyboard->getName();
2321 });
2322
2323 ASSERT_NE(it, inputDevices.end());
2324 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2325 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2326 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002327
2328 keyboard.reset();
2329 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2330 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2331 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2332}
2333
2334TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2335 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2336 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2337
2338 NotifyConfigurationChangedArgs configChangedArgs;
2339 ASSERT_NO_FATAL_FAILURE(
2340 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002341 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002342 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2343
2344 NotifyKeyArgs keyArgs;
2345 keyboard->pressAndReleaseHomeKey();
2346 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2347 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002348 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002349 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002350 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002351 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002352 prevTimestamp = keyArgs.eventTime;
2353
2354 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2355 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002356 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002357 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002358 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002359}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002360
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002361/**
2362 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2363 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2364 * are passed to the listener.
2365 */
2366static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2367TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2368 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2369 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2370 NotifyKeyArgs keyArgs;
2371
2372 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2373 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2374 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2375 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2376
2377 controller->pressAndReleaseKey(BTN_GEAR_UP);
2378 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2379 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2380 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2381}
2382
Arthur Hungaab25622020-01-16 11:22:11 +08002383// --- TouchProcessTest ---
2384class TouchIntegrationTest : public InputReaderIntegrationTest {
2385protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002386 const std::string UNIQUE_ID = "local:0";
2387
Chris Yea52ade12020-08-27 16:49:20 -07002388 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002389 InputReaderIntegrationTest::SetUp();
2390 // At least add an internal display.
2391 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2392 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002393 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002394
2395 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2396 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2397 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2398 }
2399
2400 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2401 int32_t orientation, const std::string& uniqueId,
2402 std::optional<uint8_t> physicalPort,
2403 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002404 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2405 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002406 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2407 }
2408
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002409 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2410 NotifyMotionArgs args;
2411 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2412 EXPECT_EQ(action, args.action);
2413 ASSERT_EQ(points.size(), args.pointerCount);
2414 for (size_t i = 0; i < args.pointerCount; i++) {
2415 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2416 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2417 }
2418 }
2419
Arthur Hungaab25622020-01-16 11:22:11 +08002420 std::unique_ptr<UinputTouchScreen> mDevice;
2421};
2422
2423TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2424 NotifyMotionArgs args;
2425 const Point centerPoint = mDevice->getCenterPoint();
2426
2427 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002428 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002429 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002430 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002431 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2432 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2433
2434 // ACTION_MOVE
2435 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002436 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002437 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2438 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2439
2440 // ACTION_UP
2441 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002442 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002443 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2444 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2445}
2446
2447TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2448 NotifyMotionArgs args;
2449 const Point centerPoint = mDevice->getCenterPoint();
2450
2451 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002452 mDevice->sendSlot(FIRST_SLOT);
2453 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002454 mDevice->sendDown(centerPoint);
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_DOWN, args.action);
2458
2459 // ACTION_POINTER_DOWN (Second slot)
2460 const Point secondPoint = centerPoint + Point(100, 100);
2461 mDevice->sendSlot(SECOND_SLOT);
2462 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002463 mDevice->sendDown(secondPoint);
2464 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002465 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002466 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002467
2468 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002469 mDevice->sendMove(secondPoint + Point(1, 1));
2470 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002471 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2472 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2473
2474 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002475 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002476 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002477 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002478 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002479
2480 // ACTION_UP
2481 mDevice->sendSlot(FIRST_SLOT);
2482 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002483 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002484 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2485 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2486}
2487
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002488/**
2489 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2490 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2491 * data?
2492 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2493 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2494 * for Pointer 0 only is generated after.
2495 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2496 * events, we will not miss any information.
2497 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2498 * event generated afterwards that contains the newest movement of pointer 0.
2499 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2500 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2501 * losing information about non-palm pointers.
2502 */
2503TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2504 NotifyMotionArgs args;
2505 const Point centerPoint = mDevice->getCenterPoint();
2506
2507 // ACTION_DOWN
2508 mDevice->sendSlot(FIRST_SLOT);
2509 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2510 mDevice->sendDown(centerPoint);
2511 mDevice->sendSync();
2512 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2513
2514 // ACTION_POINTER_DOWN (Second slot)
2515 const Point secondPoint = centerPoint + Point(100, 100);
2516 mDevice->sendSlot(SECOND_SLOT);
2517 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2518 mDevice->sendDown(secondPoint);
2519 mDevice->sendSync();
2520 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2521
2522 // ACTION_MOVE (First slot)
2523 mDevice->sendSlot(FIRST_SLOT);
2524 mDevice->sendMove(centerPoint + Point(5, 5));
2525 // ACTION_POINTER_UP (Second slot)
2526 mDevice->sendSlot(SECOND_SLOT);
2527 mDevice->sendPointerUp();
2528 // Send a single sync for the above 2 pointer updates
2529 mDevice->sendSync();
2530
2531 // First, we should get POINTER_UP for the second pointer
2532 assertReceivedMotion(ACTION_POINTER_1_UP,
2533 {/*first pointer */ centerPoint + Point(5, 5),
2534 /*second pointer*/ secondPoint});
2535
2536 // Next, the MOVE event for the first pointer
2537 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2538}
2539
2540/**
2541 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2542 * move, and then it will go up, all in the same frame.
2543 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2544 * gets sent to the listener.
2545 */
2546TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2547 NotifyMotionArgs args;
2548 const Point centerPoint = mDevice->getCenterPoint();
2549
2550 // ACTION_DOWN
2551 mDevice->sendSlot(FIRST_SLOT);
2552 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2553 mDevice->sendDown(centerPoint);
2554 mDevice->sendSync();
2555 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2556
2557 // ACTION_POINTER_DOWN (Second slot)
2558 const Point secondPoint = centerPoint + Point(100, 100);
2559 mDevice->sendSlot(SECOND_SLOT);
2560 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2561 mDevice->sendDown(secondPoint);
2562 mDevice->sendSync();
2563 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2564
2565 // ACTION_MOVE (First slot)
2566 mDevice->sendSlot(FIRST_SLOT);
2567 mDevice->sendMove(centerPoint + Point(5, 5));
2568 // ACTION_POINTER_UP (Second slot)
2569 mDevice->sendSlot(SECOND_SLOT);
2570 mDevice->sendMove(secondPoint + Point(6, 6));
2571 mDevice->sendPointerUp();
2572 // Send a single sync for the above 2 pointer updates
2573 mDevice->sendSync();
2574
2575 // First, we should get POINTER_UP for the second pointer
2576 // The movement of the second pointer during the liftoff frame is ignored.
2577 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2578 assertReceivedMotion(ACTION_POINTER_1_UP,
2579 {/*first pointer */ centerPoint + Point(5, 5),
2580 /*second pointer*/ secondPoint});
2581
2582 // Next, the MOVE event for the first pointer
2583 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2584}
2585
Arthur Hungaab25622020-01-16 11:22:11 +08002586TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2587 NotifyMotionArgs args;
2588 const Point centerPoint = mDevice->getCenterPoint();
2589
2590 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002591 mDevice->sendSlot(FIRST_SLOT);
2592 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002593 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002594 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002595 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2596 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2597
arthurhungcc7f9802020-04-30 17:55:40 +08002598 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002599 const Point secondPoint = centerPoint + Point(100, 100);
2600 mDevice->sendSlot(SECOND_SLOT);
2601 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2602 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002603 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002604 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002605 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002606
arthurhungcc7f9802020-04-30 17:55:40 +08002607 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002608 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002609 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002610 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2611 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2612
arthurhungcc7f9802020-04-30 17:55:40 +08002613 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2614 // a palm event.
2615 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002616 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002617 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002618 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002619 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002620 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002621
arthurhungcc7f9802020-04-30 17:55:40 +08002622 // Send up to second slot, expect first slot send moving.
2623 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002624 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002625 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2626 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002627
arthurhungcc7f9802020-04-30 17:55:40 +08002628 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002629 mDevice->sendSlot(FIRST_SLOT);
2630 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002631 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002632
arthurhungcc7f9802020-04-30 17:55:40 +08002633 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2634 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002635}
2636
Michael Wrightd02c5b62014-02-10 15:10:22 -08002637// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002638class InputDeviceTest : public testing::Test {
2639protected:
2640 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002641 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002642 static const int32_t DEVICE_ID;
2643 static const int32_t DEVICE_GENERATION;
2644 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002645 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002646 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002647
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002648 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002649 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002650 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002651 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002652 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002653
Chris Yea52ade12020-08-27 16:49:20 -07002654 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002655 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002656 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002657 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002658 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002659 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002660 InputDeviceIdentifier identifier;
2661 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002662 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002663 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002664 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002665 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002666 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002667 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002668 }
2669
Chris Yea52ade12020-08-27 16:49:20 -07002670 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002671 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002672 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002673 }
2674};
2675
2676const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002677const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002678const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002679const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2680const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002681const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002682 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002683const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002684
2685TEST_F(InputDeviceTest, ImmutableProperties) {
2686 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002687 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002688 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002689}
2690
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002691TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2692 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002693}
2694
Michael Wrightd02c5b62014-02-10 15:10:22 -08002695TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2696 // Configuration.
2697 InputReaderConfiguration config;
2698 mDevice->configure(ARBITRARY_TIME, &config, 0);
2699
2700 // Reset.
2701 mDevice->reset(ARBITRARY_TIME);
2702
2703 NotifyDeviceResetArgs resetArgs;
2704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2705 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2706 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2707
2708 // Metadata.
2709 ASSERT_TRUE(mDevice->isIgnored());
2710 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2711
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002712 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002713 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002714 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002715 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2716 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2717
2718 // State queries.
2719 ASSERT_EQ(0, mDevice->getMetaState());
2720
2721 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2722 << "Ignored device should return unknown key code state.";
2723 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2724 << "Ignored device should return unknown scan code state.";
2725 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2726 << "Ignored device should return unknown switch state.";
2727
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002728 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002729 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002730 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002731 << "Ignored device should never mark any key codes.";
2732 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2733 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2734}
2735
2736TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2737 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002738 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08002739
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002740 FakeInputMapper& mapper1 =
2741 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002742 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2743 mapper1.setMetaState(AMETA_ALT_ON);
2744 mapper1.addSupportedKeyCode(AKEYCODE_A);
2745 mapper1.addSupportedKeyCode(AKEYCODE_B);
2746 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2747 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2748 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2749 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2750 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002751
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002752 FakeInputMapper& mapper2 =
2753 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002754 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002755
2756 InputReaderConfiguration config;
2757 mDevice->configure(ARBITRARY_TIME, &config, 0);
2758
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002759 std::string propertyValue;
2760 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002761 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002762 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002763
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002764 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2765 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002766
2767 // Reset
2768 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002769 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2770 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002771
2772 NotifyDeviceResetArgs resetArgs;
2773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2774 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2775 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2776
2777 // Metadata.
2778 ASSERT_FALSE(mDevice->isIgnored());
2779 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2780
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002781 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002782 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002783 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002784 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2785 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2786
2787 // State queries.
2788 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2789 << "Should query mappers and combine meta states.";
2790
2791 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2792 << "Should return unknown key code state when source not supported.";
2793 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2794 << "Should return unknown scan code state when source not supported.";
2795 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2796 << "Should return unknown switch state when source not supported.";
2797
2798 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2799 << "Should query mapper when source is supported.";
2800 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2801 << "Should query mapper when source is supported.";
2802 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2803 << "Should query mapper when source is supported.";
2804
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002805 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002806 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002807 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002808 << "Should do nothing when source is unsupported.";
2809 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2810 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2811 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2812 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2813
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002814 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002815 << "Should query mapper when source is supported.";
2816 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2817 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2818 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2819 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2820
2821 // Event handling.
2822 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002823 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002824 mDevice->process(&event, 1);
2825
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002826 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2827 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002828}
2829
Arthur Hung2c9a3342019-07-23 14:18:59 +08002830// A single input device is associated with a specific display. Check that:
2831// 1. Device is disabled if the viewport corresponding to the associated display is not found
2832// 2. Device is disabled when setEnabled API is called
2833TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002834 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002835
2836 // First Configuration.
2837 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2838
2839 // Device should be enabled by default.
2840 ASSERT_TRUE(mDevice->isEnabled());
2841
2842 // Prepare associated info.
2843 constexpr uint8_t hdmi = 1;
2844 const std::string UNIQUE_ID = "local:1";
2845
2846 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2847 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2848 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2849 // Device should be disabled because it is associated with a specific display via
2850 // input port <-> display port association, but the corresponding display is not found
2851 ASSERT_FALSE(mDevice->isEnabled());
2852
2853 // Prepare displays.
2854 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002855 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2856 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002857 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2858 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2859 ASSERT_TRUE(mDevice->isEnabled());
2860
2861 // Device should be disabled after set disable.
2862 mFakePolicy->addDisabledDevice(mDevice->getId());
2863 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2864 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2865 ASSERT_FALSE(mDevice->isEnabled());
2866
2867 // Device should still be disabled even found the associated display.
2868 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2869 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2870 ASSERT_FALSE(mDevice->isEnabled());
2871}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002872
Christine Franks1ba71cc2021-04-07 14:37:42 -07002873TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2874 // Device should be enabled by default.
2875 mFakePolicy->clearViewports();
2876 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2877 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2878 ASSERT_TRUE(mDevice->isEnabled());
2879
2880 // Device should be disabled because it is associated with a specific display, but the
2881 // corresponding display is not found.
2882 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
Christine Franks2a2293c2022-01-18 11:51:16 -08002883 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002884 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2885 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2886 ASSERT_FALSE(mDevice->isEnabled());
2887
2888 // Device should be enabled when a display is found.
2889 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2890 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2891 NO_PORT, ViewportType::INTERNAL);
2892 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2893 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2894 ASSERT_TRUE(mDevice->isEnabled());
2895
2896 // Device should be disabled after set disable.
2897 mFakePolicy->addDisabledDevice(mDevice->getId());
2898 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2899 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2900 ASSERT_FALSE(mDevice->isEnabled());
2901
2902 // Device should still be disabled even found the associated display.
2903 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2904 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2905 ASSERT_FALSE(mDevice->isEnabled());
2906}
2907
Christine Franks2a2293c2022-01-18 11:51:16 -08002908TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2909 mFakePolicy->clearViewports();
2910 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2911 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2912
2913 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2914 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2915 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2916 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2917 NO_PORT, ViewportType::INTERNAL);
2918 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2919 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2920 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2921}
2922
Michael Wrightd02c5b62014-02-10 15:10:22 -08002923// --- InputMapperTest ---
2924
2925class InputMapperTest : public testing::Test {
2926protected:
2927 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002928 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002929 static const int32_t DEVICE_ID;
2930 static const int32_t DEVICE_GENERATION;
2931 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002932 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002933 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002934
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002935 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002936 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002937 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002938 std::unique_ptr<InstrumentedInputReader> mReader;
2939 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002940
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002941 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002942 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002943 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002944 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002945 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002946 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002947 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002948 // Consume the device reset notification generated when adding a new device.
2949 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002950 }
2951
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002952 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002953 SetUp(DEVICE_CLASSES);
2954 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002955
Chris Yea52ade12020-08-27 16:49:20 -07002956 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002957 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002958 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002959 }
2960
2961 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002962 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002963 }
2964
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002965 void configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00002966 if (!changes ||
2967 (changes &
2968 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
2969 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002970 mReader->requestRefreshConfiguration(changes);
2971 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002972 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002973 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002974 // Loop the reader to flush the input listener queue.
2975 mReader->loopOnce();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002976 }
2977
arthurhungdcef2dc2020-08-11 14:47:50 +08002978 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2979 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002980 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002981 InputDeviceIdentifier identifier;
2982 identifier.name = name;
2983 identifier.location = location;
2984 std::shared_ptr<InputDevice> device =
2985 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2986 identifier);
2987 mReader->pushNextDevice(device);
2988 mFakeEventHub->addDevice(eventHubId, name, classes);
2989 mReader->loopOnce();
2990 return device;
2991 }
2992
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002993 template <class T, typename... Args>
2994 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002995 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002996 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002997 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002998 mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002999 // Loop the reader to flush the input listener queue.
3000 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003001 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003002 }
3003
3004 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003005 int32_t orientation, const std::string& uniqueId,
3006 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003007 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3008 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003009 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3010 }
3011
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003012 void clearViewports() {
3013 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003014 }
3015
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003016 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
3017 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003018 RawEvent event;
3019 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003020 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003021 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003022 event.type = type;
3023 event.code = code;
3024 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003025 mapper.process(&event);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003026 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003027 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003028 }
3029
3030 static void assertMotionRange(const InputDeviceInfo& info,
3031 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3032 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003033 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003034 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3035 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3036 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3037 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3038 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3039 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3040 }
3041
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003042 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3043 float size, float touchMajor, float touchMinor, float toolMajor,
3044 float toolMinor, float orientation, float distance,
3045 float scaledAxisEpsilon = 1.f) {
3046 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3047 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003048 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3049 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003050 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3051 scaledAxisEpsilon);
3052 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3053 scaledAxisEpsilon);
3054 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3055 scaledAxisEpsilon);
3056 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3057 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003058 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3059 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3060 }
3061
Michael Wright17db18e2020-06-26 20:51:44 +01003062 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003063 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003064 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003065 ASSERT_NEAR(x, actualX, 1);
3066 ASSERT_NEAR(y, actualY, 1);
3067 }
3068};
3069
3070const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003071const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003072const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003073const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3074const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003075const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3076 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003077const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003078
3079// --- SwitchInputMapperTest ---
3080
3081class SwitchInputMapperTest : public InputMapperTest {
3082protected:
3083};
3084
3085TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003086 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003087
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003088 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089}
3090
3091TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003092 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003093
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003094 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003095 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003096
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003097 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003098 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003099}
3100
3101TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003102 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003103
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003104 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3105 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3106 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3107 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003108
3109 NotifySwitchArgs args;
3110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3111 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003112 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3113 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003114 args.switchMask);
3115 ASSERT_EQ(uint32_t(0), args.policyFlags);
3116}
3117
Chris Ye87143712020-11-10 05:05:58 +00003118// --- VibratorInputMapperTest ---
3119class VibratorInputMapperTest : public InputMapperTest {
3120protected:
3121 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3122};
3123
3124TEST_F(VibratorInputMapperTest, GetSources) {
3125 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3126
3127 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3128}
3129
3130TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3131 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3132
3133 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3134}
3135
3136TEST_F(VibratorInputMapperTest, Vibrate) {
3137 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003138 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003139 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3140
3141 VibrationElement pattern(2);
3142 VibrationSequence sequence(2);
3143 pattern.duration = std::chrono::milliseconds(200);
3144 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3145 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3146 sequence.addElement(pattern);
3147 pattern.duration = std::chrono::milliseconds(500);
3148 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3149 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3150 sequence.addElement(pattern);
3151
3152 std::vector<int64_t> timings = {0, 1};
3153 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3154
3155 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003156 // Start vibrating
3157 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003158 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003159 // Verify vibrator state listener was notified.
3160 mReader->loopOnce();
3161 NotifyVibratorStateArgs args;
3162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3163 ASSERT_EQ(DEVICE_ID, args.deviceId);
3164 ASSERT_TRUE(args.isOn);
3165 // Stop vibrating
3166 mapper.cancelVibrate(VIBRATION_TOKEN);
3167 ASSERT_FALSE(mapper.isVibrating());
3168 // Verify vibrator state listener was notified.
3169 mReader->loopOnce();
3170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3171 ASSERT_EQ(DEVICE_ID, args.deviceId);
3172 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003173}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003174
Chris Yef59a2f42020-10-16 12:55:26 -07003175// --- SensorInputMapperTest ---
3176
3177class SensorInputMapperTest : public InputMapperTest {
3178protected:
3179 static const int32_t ACCEL_RAW_MIN;
3180 static const int32_t ACCEL_RAW_MAX;
3181 static const int32_t ACCEL_RAW_FUZZ;
3182 static const int32_t ACCEL_RAW_FLAT;
3183 static const int32_t ACCEL_RAW_RESOLUTION;
3184
3185 static const int32_t GYRO_RAW_MIN;
3186 static const int32_t GYRO_RAW_MAX;
3187 static const int32_t GYRO_RAW_FUZZ;
3188 static const int32_t GYRO_RAW_FLAT;
3189 static const int32_t GYRO_RAW_RESOLUTION;
3190
3191 static const float GRAVITY_MS2_UNIT;
3192 static const float DEGREE_RADIAN_UNIT;
3193
3194 void prepareAccelAxes();
3195 void prepareGyroAxes();
3196 void setAccelProperties();
3197 void setGyroProperties();
3198 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3199};
3200
3201const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3202const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3203const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3204const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3205const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3206
3207const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3208const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3209const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3210const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3211const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3212
3213const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3214const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3215
3216void SensorInputMapperTest::prepareAccelAxes() {
3217 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3218 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3219 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3220 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3221 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3222 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3223}
3224
3225void SensorInputMapperTest::prepareGyroAxes() {
3226 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3227 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3228 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3229 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3230 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3231 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3232}
3233
3234void SensorInputMapperTest::setAccelProperties() {
3235 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3236 /* sensorDataIndex */ 0);
3237 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3238 /* sensorDataIndex */ 1);
3239 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3240 /* sensorDataIndex */ 2);
3241 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3242 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3243 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3244 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3245 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3246}
3247
3248void SensorInputMapperTest::setGyroProperties() {
3249 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3250 /* sensorDataIndex */ 0);
3251 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3252 /* sensorDataIndex */ 1);
3253 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3254 /* sensorDataIndex */ 2);
3255 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3256 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3257 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3258 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3259 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3260}
3261
3262TEST_F(SensorInputMapperTest, GetSources) {
3263 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3264
3265 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3266}
3267
3268TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3269 setAccelProperties();
3270 prepareAccelAxes();
3271 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3272
3273 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3274 std::chrono::microseconds(10000),
3275 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003276 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003277 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3278 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3279 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3280 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3281 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003282
3283 NotifySensorArgs args;
3284 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3285 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3286 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3287
3288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3289 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3290 ASSERT_EQ(args.deviceId, DEVICE_ID);
3291 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3292 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3293 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3294 ASSERT_EQ(args.values, values);
3295 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3296}
3297
3298TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3299 setGyroProperties();
3300 prepareGyroAxes();
3301 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3302
3303 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
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_RX, 20000);
3308 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3309 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 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 / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3315 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3316 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_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::GYROSCOPE);
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::GYROSCOPE);
3326}
3327
Michael Wrightd02c5b62014-02-10 15:10:22 -08003328// --- KeyboardInputMapperTest ---
3329
3330class KeyboardInputMapperTest : public InputMapperTest {
3331protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003332 const std::string UNIQUE_ID = "local:0";
3333
3334 void prepareDisplay(int32_t orientation);
3335
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003336 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003337 int32_t originalKeyCode, int32_t rotatedKeyCode,
3338 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003339};
3340
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003341/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3342 * orientation.
3343 */
3344void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003345 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3346 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003347}
3348
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003349void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003350 int32_t originalScanCode, int32_t originalKeyCode,
3351 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003352 NotifyKeyArgs args;
3353
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003354 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3356 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3357 ASSERT_EQ(originalScanCode, args.scanCode);
3358 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003359 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003360
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003361 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3363 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3364 ASSERT_EQ(originalScanCode, args.scanCode);
3365 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003366 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003367}
3368
Michael Wrightd02c5b62014-02-10 15:10:22 -08003369TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003370 KeyboardInputMapper& mapper =
3371 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3372 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003373
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003374 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003375}
3376
3377TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3378 const int32_t USAGE_A = 0x070004;
3379 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003380 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3381 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003382 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3383 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3384 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003385
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003386 KeyboardInputMapper& mapper =
3387 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3388 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003389 // Initial metastate is AMETA_NONE.
3390 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003391
3392 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003393 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003394 NotifyKeyArgs args;
3395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3396 ASSERT_EQ(DEVICE_ID, args.deviceId);
3397 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3398 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3399 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3400 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3401 ASSERT_EQ(KEY_HOME, args.scanCode);
3402 ASSERT_EQ(AMETA_NONE, args.metaState);
3403 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3404 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3405 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3406
3407 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003408 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3410 ASSERT_EQ(DEVICE_ID, args.deviceId);
3411 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3412 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3413 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3414 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3415 ASSERT_EQ(KEY_HOME, args.scanCode);
3416 ASSERT_EQ(AMETA_NONE, args.metaState);
3417 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3418 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3419 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3420
3421 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003422 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3423 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3425 ASSERT_EQ(DEVICE_ID, args.deviceId);
3426 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3427 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3428 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3429 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3430 ASSERT_EQ(0, args.scanCode);
3431 ASSERT_EQ(AMETA_NONE, args.metaState);
3432 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3433 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3434 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3435
3436 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003437 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3438 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 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_A, args.keyCode);
3445 ASSERT_EQ(0, 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 with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003452 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3453 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 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(0, args.keyCode);
3460 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3461 ASSERT_EQ(AMETA_NONE, args.metaState);
3462 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3463 ASSERT_EQ(0U, args.policyFlags);
3464 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3465
3466 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003467 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3468 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 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(0, args.keyCode);
3475 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3476 ASSERT_EQ(AMETA_NONE, args.metaState);
3477 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3478 ASSERT_EQ(0U, args.policyFlags);
3479 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3480}
3481
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003482/**
3483 * Ensure that the readTime is set to the time when the EV_KEY is received.
3484 */
3485TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3486 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3487
3488 KeyboardInputMapper& mapper =
3489 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3490 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3491 NotifyKeyArgs args;
3492
3493 // Key down
3494 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3496 ASSERT_EQ(12, args.readTime);
3497
3498 // Key up
3499 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3501 ASSERT_EQ(15, args.readTime);
3502}
3503
Michael Wrightd02c5b62014-02-10 15:10:22 -08003504TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003505 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3506 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003507 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3508 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3509 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003510
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003511 KeyboardInputMapper& mapper =
3512 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3513 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003514
Arthur Hung95f68612022-04-07 14:08:22 +08003515 // Initial metastate is AMETA_NONE.
3516 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003517
3518 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003520 NotifyKeyArgs args;
3521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3522 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003523 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003524 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003525
3526 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003527 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3529 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003530 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003531
3532 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003533 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3535 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003536 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003537
3538 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003539 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3541 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003542 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003543 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003544}
3545
3546TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003547 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3548 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3549 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3550 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003551
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003552 KeyboardInputMapper& mapper =
3553 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3554 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003555
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003556 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003557 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3558 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3559 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3560 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3561 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3562 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3563 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3564 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3565}
3566
3567TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003568 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3569 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3570 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3571 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003572
Michael Wrightd02c5b62014-02-10 15:10:22 -08003573 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003574 KeyboardInputMapper& mapper =
3575 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3576 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003577
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003578 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003579 ASSERT_NO_FATAL_FAILURE(
3580 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3581 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3582 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3583 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3584 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3585 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3586 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003587
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003588 clearViewports();
3589 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003590 ASSERT_NO_FATAL_FAILURE(
3591 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3592 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3593 AKEYCODE_DPAD_UP, DISPLAY_ID));
3594 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3595 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3596 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3597 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003598
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003599 clearViewports();
3600 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003601 ASSERT_NO_FATAL_FAILURE(
3602 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3603 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3604 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3605 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3606 AKEYCODE_DPAD_UP, DISPLAY_ID));
3607 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3608 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003609
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003610 clearViewports();
3611 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003612 ASSERT_NO_FATAL_FAILURE(
3613 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3614 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3615 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3616 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3617 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3618 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3619 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003620
3621 // Special case: if orientation changes while key is down, we still emit the same keycode
3622 // in the key up as we did in the key down.
3623 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003624 clearViewports();
3625 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003626 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3628 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3629 ASSERT_EQ(KEY_UP, args.scanCode);
3630 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3631
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003632 clearViewports();
3633 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003634 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3636 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3637 ASSERT_EQ(KEY_UP, args.scanCode);
3638 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3639}
3640
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003641TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3642 // If the keyboard is not orientation aware,
3643 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003644 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003645
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003646 KeyboardInputMapper& mapper =
3647 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3648 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003649 NotifyKeyArgs args;
3650
3651 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003652 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003654 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3656 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3657
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003658 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003659 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003661 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3663 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3664}
3665
3666TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3667 // If the keyboard is orientation aware,
3668 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003669 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003670
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003671 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003672 KeyboardInputMapper& mapper =
3673 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3674 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003675 NotifyKeyArgs args;
3676
3677 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3678 // ^--- already checked by the previous test
3679
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003680 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003681 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
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(DISPLAY_ID, args.displayId);
3687
3688 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003689 clearViewports();
3690 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003691 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003692 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003694 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3696 ASSERT_EQ(newDisplayId, args.displayId);
3697}
3698
Michael Wrightd02c5b62014-02-10 15:10:22 -08003699TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003700 KeyboardInputMapper& mapper =
3701 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3702 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003703
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003704 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003705 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003706
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003707 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003708 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003709}
3710
Philip Junker4af3b3d2021-12-14 10:36:55 +01003711TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3712 KeyboardInputMapper& mapper =
3713 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3714 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3715
3716 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3717 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3718 << "If a mapping is available, the result is equal to the mapping";
3719
3720 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3721 << "If no mapping is available, the result is the key location";
3722}
3723
Michael Wrightd02c5b62014-02-10 15:10:22 -08003724TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003725 KeyboardInputMapper& mapper =
3726 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3727 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003728
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003729 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003730 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003731
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003732 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003733 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003734}
3735
3736TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003737 KeyboardInputMapper& mapper =
3738 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3739 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003740
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003741 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003742
Michael Wrightd02c5b62014-02-10 15:10:22 -08003743 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003744 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003745 ASSERT_TRUE(flags[0]);
3746 ASSERT_FALSE(flags[1]);
3747}
3748
3749TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003750 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3751 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3752 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3753 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3754 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3755 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003756
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003757 KeyboardInputMapper& mapper =
3758 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3759 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003760 // Initial metastate is AMETA_NONE.
3761 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003762
3763 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003764 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3765 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3766 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003767
3768 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003769 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3770 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003771 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3772 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3773 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003774 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003775
3776 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003777 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3778 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003779 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3780 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3781 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003782 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003783
3784 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003785 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3786 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003787 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3788 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3789 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003790 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003791
3792 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003793 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003795 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3796 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3797 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003798 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003799
3800 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003803 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3804 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3805 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003806 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003807
3808 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003809 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3810 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003811 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3812 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3813 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003814 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003815}
3816
Chris Yea52ade12020-08-27 16:49:20 -07003817TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3818 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3819 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3820 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3821 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3822
3823 KeyboardInputMapper& mapper =
3824 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3825 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3826
Chris Yea52ade12020-08-27 16:49:20 -07003827 // Meta state should be AMETA_NONE after reset
3828 mapper.reset(ARBITRARY_TIME);
3829 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3830 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3831 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3832 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3833
3834 NotifyKeyArgs args;
3835 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003836 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3838 ASSERT_EQ(AMETA_NONE, args.metaState);
3839 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3840 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3841 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3842
3843 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003844 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3846 ASSERT_EQ(AMETA_NONE, args.metaState);
3847 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3848 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3849 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3850}
3851
Arthur Hung2c9a3342019-07-23 14:18:59 +08003852TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3853 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003854 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3855 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3856 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3857 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003858
3859 // keyboard 2.
3860 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003861 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003862 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003863 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003864 std::shared_ptr<InputDevice> device2 =
3865 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003866 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003867
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003868 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3869 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3870 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3871 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003872
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003873 KeyboardInputMapper& mapper =
3874 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3875 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003876
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003877 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003878 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003879 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003880 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3881 device2->reset(ARBITRARY_TIME);
3882
3883 // Prepared displays and associated info.
3884 constexpr uint8_t hdmi1 = 0;
3885 constexpr uint8_t hdmi2 = 1;
3886 const std::string SECONDARY_UNIQUE_ID = "local:1";
3887
3888 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3889 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3890
3891 // No associated display viewport found, should disable the device.
3892 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3893 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3894 ASSERT_FALSE(device2->isEnabled());
3895
3896 // Prepare second display.
3897 constexpr int32_t newDisplayId = 2;
3898 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003899 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003900 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003901 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003902 // Default device will reconfigure above, need additional reconfiguration for another device.
3903 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3904 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3905
3906 // Device should be enabled after the associated display is found.
3907 ASSERT_TRUE(mDevice->isEnabled());
3908 ASSERT_TRUE(device2->isEnabled());
3909
3910 // Test pad key events
3911 ASSERT_NO_FATAL_FAILURE(
3912 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3913 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3914 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3915 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3916 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3917 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3918 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3919
3920 ASSERT_NO_FATAL_FAILURE(
3921 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3922 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3923 AKEYCODE_DPAD_RIGHT, newDisplayId));
3924 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3925 AKEYCODE_DPAD_DOWN, newDisplayId));
3926 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3927 AKEYCODE_DPAD_LEFT, newDisplayId));
3928}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003929
arthurhungc903df12020-08-11 15:08:42 +08003930TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3931 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3932 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3933 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3934 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3935 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3936 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3937
3938 KeyboardInputMapper& mapper =
3939 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3940 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003941 // Initial metastate is AMETA_NONE.
3942 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003943
3944 // Initialization should have turned all of the lights off.
3945 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3946 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3947 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3948
3949 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3951 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003952 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3953 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3954
3955 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003956 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3957 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003958 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3959 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3960
3961 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003962 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3963 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003964 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3965 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3966
3967 mFakeEventHub->removeDevice(EVENTHUB_ID);
3968 mReader->loopOnce();
3969
3970 // keyboard 2 should default toggle keys.
3971 const std::string USB2 = "USB2";
3972 const std::string DEVICE_NAME2 = "KEYBOARD2";
3973 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3974 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3975 std::shared_ptr<InputDevice> device2 =
3976 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003977 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003978 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3979 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3980 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3981 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3982 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3983 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3984
arthurhung6fe95782020-10-05 22:41:16 +08003985 KeyboardInputMapper& mapper2 =
3986 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3987 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003988 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3989 device2->reset(ARBITRARY_TIME);
3990
3991 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3992 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3993 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003994 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3995 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003996}
3997
Arthur Hungcb40a002021-08-03 14:31:01 +00003998TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3999 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4000 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4001 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4002
4003 // Suppose we have two mappers. (DPAD + KEYBOARD)
4004 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4005 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4006 KeyboardInputMapper& mapper =
4007 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4008 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004009 // Initial metastate is AMETA_NONE.
4010 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004011
4012 mReader->toggleCapsLockState(DEVICE_ID);
4013 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4014}
4015
Arthur Hungfb3cc112022-04-13 07:39:50 +00004016TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4017 // keyboard 1.
4018 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4019 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4020 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4021 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4022 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4023 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4024
4025 KeyboardInputMapper& mapper1 =
4026 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4027 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4028
4029 // keyboard 2.
4030 const std::string USB2 = "USB2";
4031 const std::string DEVICE_NAME2 = "KEYBOARD2";
4032 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4033 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4034 std::shared_ptr<InputDevice> device2 =
4035 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4036 ftl::Flags<InputDeviceClass>(0));
4037 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4038 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4039 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4040 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4041 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4042 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4043
4044 KeyboardInputMapper& mapper2 =
4045 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4046 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4047 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4048 device2->reset(ARBITRARY_TIME);
4049
Arthur Hung95f68612022-04-07 14:08:22 +08004050 // Initial metastate is AMETA_NONE.
4051 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4052 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4053
4054 // Toggle num lock on and off.
4055 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4056 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004057 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4058 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4059 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4060
4061 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4062 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4063 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4064 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4065 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4066
4067 // Toggle caps lock on and off.
4068 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4069 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4070 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4071 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4072 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4073
4074 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4075 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4076 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4077 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4078 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4079
4080 // Toggle scroll lock on and off.
4081 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4082 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4083 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4084 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4085 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4086
4087 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4088 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4089 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4090 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4091 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4092}
4093
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004094// --- KeyboardInputMapperTest_ExternalDevice ---
4095
4096class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4097protected:
Chris Yea52ade12020-08-27 16:49:20 -07004098 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004099};
4100
4101TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004102 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4103 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004104
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004105 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4106 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4107 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4108 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004109
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004110 KeyboardInputMapper& mapper =
4111 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4112 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004113
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004114 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004115 NotifyKeyArgs args;
4116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4117 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4118
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004119 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4121 ASSERT_EQ(uint32_t(0), args.policyFlags);
4122
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4125 ASSERT_EQ(uint32_t(0), args.policyFlags);
4126
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004127 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4129 ASSERT_EQ(uint32_t(0), args.policyFlags);
4130
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004131 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4133 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4134
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004135 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4137 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4138}
4139
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004140TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004141 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004142
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004143 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4144 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4145 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004146
Powei Fengd041c5d2019-05-03 17:11:33 -07004147 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004148 KeyboardInputMapper& mapper =
4149 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4150 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004151
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004152 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004153 NotifyKeyArgs args;
4154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4155 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4156
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004157 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4159 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4160
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004161 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4163 ASSERT_EQ(uint32_t(0), args.policyFlags);
4164
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004165 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4167 ASSERT_EQ(uint32_t(0), args.policyFlags);
4168
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004169 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4171 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4172
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004173 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4175 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4176}
4177
Michael Wrightd02c5b62014-02-10 15:10:22 -08004178// --- CursorInputMapperTest ---
4179
4180class CursorInputMapperTest : public InputMapperTest {
4181protected:
4182 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4183
Michael Wright17db18e2020-06-26 20:51:44 +01004184 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004185
Chris Yea52ade12020-08-27 16:49:20 -07004186 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004187 InputMapperTest::SetUp();
4188
Michael Wright17db18e2020-06-26 20:51:44 +01004189 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004190 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004191 }
4192
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004193 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4194 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004195
4196 void prepareDisplay(int32_t orientation) {
4197 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004198 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004199 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4200 orientation, uniqueId, NO_PORT, viewportType);
4201 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004202
4203 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4204 float pressure) {
4205 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4206 0.0f, 0.0f, 0.0f, EPSILON));
4207 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004208};
4209
4210const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4211
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004212void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4213 int32_t originalY, int32_t rotatedX,
4214 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004215 NotifyMotionArgs args;
4216
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004217 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4218 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4219 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4221 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004222 ASSERT_NO_FATAL_FAILURE(
4223 assertCursorPointerCoords(args.pointerCoords[0],
4224 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4225 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004226}
4227
4228TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004229 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004230 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004231
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004232 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004233}
4234
4235TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004236 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004237 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004238
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004239 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004240}
4241
4242TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004243 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004244 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004245
4246 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004247 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004248
4249 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004250 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4251 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004252 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4253 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4254
4255 // When the bounds are set, then there should be a valid motion range.
4256 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4257
4258 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004259 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004260
4261 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4262 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4263 1, 800 - 1, 0.0f, 0.0f));
4264 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4265 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4266 2, 480 - 1, 0.0f, 0.0f));
4267 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4268 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4269 0.0f, 1.0f, 0.0f, 0.0f));
4270}
4271
4272TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004273 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004274 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004275
4276 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004277 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004278
4279 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4280 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4281 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4282 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4283 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4284 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4285 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4286 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4287 0.0f, 1.0f, 0.0f, 0.0f));
4288}
4289
4290TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004291 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004292 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004293
arthurhungdcef2dc2020-08-11 14:47:50 +08004294 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295
4296 NotifyMotionArgs args;
4297
4298 // Button press.
4299 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004300 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4301 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4303 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4304 ASSERT_EQ(DEVICE_ID, args.deviceId);
4305 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4306 ASSERT_EQ(uint32_t(0), args.policyFlags);
4307 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4308 ASSERT_EQ(0, args.flags);
4309 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4310 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4311 ASSERT_EQ(0, args.edgeFlags);
4312 ASSERT_EQ(uint32_t(1), args.pointerCount);
4313 ASSERT_EQ(0, args.pointerProperties[0].id);
4314 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004315 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004316 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4317 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4318 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4319
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4321 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4322 ASSERT_EQ(DEVICE_ID, args.deviceId);
4323 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4324 ASSERT_EQ(uint32_t(0), args.policyFlags);
4325 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4326 ASSERT_EQ(0, args.flags);
4327 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4328 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4329 ASSERT_EQ(0, args.edgeFlags);
4330 ASSERT_EQ(uint32_t(1), args.pointerCount);
4331 ASSERT_EQ(0, args.pointerProperties[0].id);
4332 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004333 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004334 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4335 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4336 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4337
Michael Wrightd02c5b62014-02-10 15:10:22 -08004338 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004339 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4340 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4342 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4343 ASSERT_EQ(DEVICE_ID, args.deviceId);
4344 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4345 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004346 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4347 ASSERT_EQ(0, args.flags);
4348 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4349 ASSERT_EQ(0, args.buttonState);
4350 ASSERT_EQ(0, args.edgeFlags);
4351 ASSERT_EQ(uint32_t(1), args.pointerCount);
4352 ASSERT_EQ(0, args.pointerProperties[0].id);
4353 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004354 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004355 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4356 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4357 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4358
4359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4360 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4361 ASSERT_EQ(DEVICE_ID, args.deviceId);
4362 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4363 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004364 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4365 ASSERT_EQ(0, args.flags);
4366 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4367 ASSERT_EQ(0, args.buttonState);
4368 ASSERT_EQ(0, args.edgeFlags);
4369 ASSERT_EQ(uint32_t(1), args.pointerCount);
4370 ASSERT_EQ(0, args.pointerProperties[0].id);
4371 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004372 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004373 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4374 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4375 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4376}
4377
4378TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004380 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004381
4382 NotifyMotionArgs args;
4383
4384 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004385 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4386 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4388 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004389 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4390 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4391 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392
4393 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004394 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4395 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004398 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4399 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004400}
4401
4402TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004404 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004405
4406 NotifyMotionArgs args;
4407
4408 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4410 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4412 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004413 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004414
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4416 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004417 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004418
Michael Wrightd02c5b62014-02-10 15:10:22 -08004419 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004420 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4421 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004423 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004424 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004425
4426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004427 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004428 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004429}
4430
4431TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004432 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004433 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434
4435 NotifyMotionArgs args;
4436
4437 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004438 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4439 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4440 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4441 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4443 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004444 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4445 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4446 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4449 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004450 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4451 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4452 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004453
Michael Wrightd02c5b62014-02-10 15:10:22 -08004454 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004455 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4456 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4459 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004460 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4461 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4462 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004463
4464 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004465 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4466 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004468 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004469 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004470
4471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004472 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004473 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004474}
4475
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004476TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004477 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004478 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4479 // need to be rotated.
4480 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004481 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004482
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004483 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004484 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4485 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4486 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4487 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4488 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4489 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4490 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4491 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4492}
4493
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004494TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004495 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004496 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4497 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004498 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004499
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004500 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004501 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4502 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4503 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4504 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4505 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4506 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4507 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4508 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4509
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004510 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004511 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4512 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4513 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4514 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4515 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4516 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4517 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4518 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004519
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004520 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004521 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4522 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4523 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4524 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4525 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4526 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4527 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4528 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4529
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004530 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004531 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4532 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4533 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4534 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4535 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4536 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4537 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4538 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004539}
4540
4541TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004542 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004543 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004544
4545 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4546 mFakePointerController->setPosition(100, 200);
4547 mFakePointerController->setButtonState(0);
4548
4549 NotifyMotionArgs motionArgs;
4550 NotifyKeyArgs keyArgs;
4551
4552 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004553 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4554 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4556 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4557 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4558 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004559 ASSERT_NO_FATAL_FAILURE(
4560 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004561
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4563 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4564 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4565 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004566 ASSERT_NO_FATAL_FAILURE(
4567 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004568
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004569 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4570 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004572 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004573 ASSERT_EQ(0, motionArgs.buttonState);
4574 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004575 ASSERT_NO_FATAL_FAILURE(
4576 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004577
4578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004579 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004580 ASSERT_EQ(0, motionArgs.buttonState);
4581 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004582 ASSERT_NO_FATAL_FAILURE(
4583 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004584
4585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004586 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004587 ASSERT_EQ(0, motionArgs.buttonState);
4588 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004589 ASSERT_NO_FATAL_FAILURE(
4590 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004591
4592 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004593 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4594 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4595 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4597 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4598 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4599 motionArgs.buttonState);
4600 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4601 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004602 ASSERT_NO_FATAL_FAILURE(
4603 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004604
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4606 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4607 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4608 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4609 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004610 ASSERT_NO_FATAL_FAILURE(
4611 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004612
4613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4614 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4615 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4616 motionArgs.buttonState);
4617 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4618 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004619 ASSERT_NO_FATAL_FAILURE(
4620 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004621
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004622 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4623 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004625 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004626 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4627 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004628 ASSERT_NO_FATAL_FAILURE(
4629 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004630
4631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004633 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4634 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004635 ASSERT_NO_FATAL_FAILURE(
4636 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004637
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004638 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4639 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004641 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4642 ASSERT_EQ(0, motionArgs.buttonState);
4643 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004644 ASSERT_NO_FATAL_FAILURE(
4645 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004646 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4647 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004648
4649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004650 ASSERT_EQ(0, motionArgs.buttonState);
4651 ASSERT_EQ(0, mFakePointerController->getButtonState());
4652 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004653 ASSERT_NO_FATAL_FAILURE(
4654 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004655
Michael Wrightd02c5b62014-02-10 15:10:22 -08004656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4657 ASSERT_EQ(0, motionArgs.buttonState);
4658 ASSERT_EQ(0, mFakePointerController->getButtonState());
4659 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004660 ASSERT_NO_FATAL_FAILURE(
4661 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004662
4663 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004664 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4665 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4667 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4668 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004669
Michael Wrightd02c5b62014-02-10 15:10:22 -08004670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004671 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004672 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4673 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004674 ASSERT_NO_FATAL_FAILURE(
4675 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004676
4677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4678 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4679 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4680 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004681 ASSERT_NO_FATAL_FAILURE(
4682 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004683
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004684 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004687 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004688 ASSERT_EQ(0, motionArgs.buttonState);
4689 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004690 ASSERT_NO_FATAL_FAILURE(
4691 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004692
4693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004694 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004695 ASSERT_EQ(0, motionArgs.buttonState);
4696 ASSERT_EQ(0, mFakePointerController->getButtonState());
4697
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004698 ASSERT_NO_FATAL_FAILURE(
4699 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4701 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4702 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4703
4704 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004705 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4706 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4708 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4709 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004710
Michael Wrightd02c5b62014-02-10 15:10:22 -08004711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004712 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004713 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4714 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004715 ASSERT_NO_FATAL_FAILURE(
4716 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004717
4718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4719 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4720 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4721 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004722 ASSERT_NO_FATAL_FAILURE(
4723 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004724
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004725 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
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->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004728 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004729 ASSERT_EQ(0, motionArgs.buttonState);
4730 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004731 ASSERT_NO_FATAL_FAILURE(
4732 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004733
4734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4735 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4736 ASSERT_EQ(0, motionArgs.buttonState);
4737 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004738 ASSERT_NO_FATAL_FAILURE(
4739 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004740
Michael Wrightd02c5b62014-02-10 15:10:22 -08004741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4742 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4743 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4744
4745 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004746 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4747 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4749 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4750 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004751
Michael Wrightd02c5b62014-02-10 15:10:22 -08004752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004753 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004754 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4755 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004756 ASSERT_NO_FATAL_FAILURE(
4757 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004758
4759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4760 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4761 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4762 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004763 ASSERT_NO_FATAL_FAILURE(
4764 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004765
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004766 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
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->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004769 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004770 ASSERT_EQ(0, motionArgs.buttonState);
4771 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004772 ASSERT_NO_FATAL_FAILURE(
4773 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004774
4775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4776 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4777 ASSERT_EQ(0, motionArgs.buttonState);
4778 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004779 ASSERT_NO_FATAL_FAILURE(
4780 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004781
Michael Wrightd02c5b62014-02-10 15:10:22 -08004782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4783 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4784 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4785
4786 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004787 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4788 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4790 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4791 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004792
Michael Wrightd02c5b62014-02-10 15:10:22 -08004793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004794 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004795 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4796 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004797 ASSERT_NO_FATAL_FAILURE(
4798 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004799
4800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4801 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4802 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4803 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004804 ASSERT_NO_FATAL_FAILURE(
4805 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004806
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
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->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004810 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004811 ASSERT_EQ(0, motionArgs.buttonState);
4812 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004813 ASSERT_NO_FATAL_FAILURE(
4814 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004815
4816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4817 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4818 ASSERT_EQ(0, motionArgs.buttonState);
4819 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004820 ASSERT_NO_FATAL_FAILURE(
4821 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004822
Michael Wrightd02c5b62014-02-10 15:10:22 -08004823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4824 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4825 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4826}
4827
4828TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004829 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004830 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831
4832 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4833 mFakePointerController->setPosition(100, 200);
4834 mFakePointerController->setButtonState(0);
4835
4836 NotifyMotionArgs args;
4837
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004838 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4839 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4840 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004842 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4843 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4844 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4845 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 +01004846 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004847}
4848
4849TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004850 addConfigurationProperty("cursor.mode", "pointer");
4851 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004852 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004853
4854 NotifyDeviceResetArgs resetArgs;
4855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4856 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4857 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4858
4859 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4860 mFakePointerController->setPosition(100, 200);
4861 mFakePointerController->setButtonState(0);
4862
4863 NotifyMotionArgs args;
4864
4865 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004866 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4867 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4868 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4870 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4871 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4872 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4873 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 +01004874 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004875
4876 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004877 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4878 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4880 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4881 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4882 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4883 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4885 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4886 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4887 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4888 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4889
4890 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004891 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4892 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4894 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4895 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4896 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4897 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4899 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4900 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4901 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4902 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4903
4904 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004905 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4906 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4907 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4909 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4910 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4911 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4912 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 +01004913 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004914
4915 // Disable pointer capture and check that the device generation got bumped
4916 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004917 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004918 mFakePolicy->setPointerCapture(false);
4919 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004920 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004921
4922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004923 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4924
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004925 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4926 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4927 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4929 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004930 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4931 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4932 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 +01004933 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004934}
4935
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004936/**
4937 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4938 * pointer acceleration or speed processing should not be applied.
4939 */
4940TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4941 addConfigurationProperty("cursor.mode", "pointer");
4942 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4943 100.f /*high threshold*/, 10.f /*acceleration*/);
4944 mFakePolicy->setVelocityControlParams(testParams);
4945 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4946
4947 NotifyDeviceResetArgs resetArgs;
4948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4949 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4950 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4951
4952 NotifyMotionArgs args;
4953
4954 // Move and verify scale is applied.
4955 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4956 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4957 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4959 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4960 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4961 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4962 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4963 ASSERT_GT(relX, 10);
4964 ASSERT_GT(relY, 20);
4965
4966 // Enable Pointer Capture
4967 mFakePolicy->setPointerCapture(true);
4968 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4969 NotifyPointerCaptureChangedArgs captureArgs;
4970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4971 ASSERT_TRUE(captureArgs.request.enable);
4972
4973 // Move and verify scale is not applied.
4974 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4975 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4976 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4978 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4979 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4980 ASSERT_EQ(10, args.pointerCoords[0].getX());
4981 ASSERT_EQ(20, args.pointerCoords[0].getY());
4982}
4983
Prabir Pradhan208360b2022-06-24 18:37:04 +00004984TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4985 addConfigurationProperty("cursor.mode", "pointer");
4986 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4987
4988 NotifyDeviceResetArgs resetArgs;
4989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4990 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4991 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4992
4993 // Ensure the display is rotated.
4994 prepareDisplay(DISPLAY_ORIENTATION_90);
4995
4996 NotifyMotionArgs args;
4997
4998 // Verify that the coordinates are rotated.
4999 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5000 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5001 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5003 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5004 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5005 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5006 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5007
5008 // Enable Pointer Capture.
5009 mFakePolicy->setPointerCapture(true);
5010 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5011 NotifyPointerCaptureChangedArgs captureArgs;
5012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5013 ASSERT_TRUE(captureArgs.request.enable);
5014
5015 // Move and verify rotation is not 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_RELATIVE, args.source);
5021 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5022 ASSERT_EQ(10, args.pointerCoords[0].getX());
5023 ASSERT_EQ(20, args.pointerCoords[0].getY());
5024}
5025
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005026TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005027 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005028
Garfield Tan888a6a42020-01-09 11:39:16 -08005029 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005030 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08005031 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
5032 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00005033 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
5034 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08005035 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
5036 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5037
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005038 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5039 mFakePointerController->setPosition(100, 200);
5040 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005041
5042 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005043 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5044 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5045 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5047 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5048 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5049 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5050 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 +01005051 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005052 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
5053}
5054
Michael Wrightd02c5b62014-02-10 15:10:22 -08005055// --- TouchInputMapperTest ---
5056
5057class TouchInputMapperTest : public InputMapperTest {
5058protected:
5059 static const int32_t RAW_X_MIN;
5060 static const int32_t RAW_X_MAX;
5061 static const int32_t RAW_Y_MIN;
5062 static const int32_t RAW_Y_MAX;
5063 static const int32_t RAW_TOUCH_MIN;
5064 static const int32_t RAW_TOUCH_MAX;
5065 static const int32_t RAW_TOOL_MIN;
5066 static const int32_t RAW_TOOL_MAX;
5067 static const int32_t RAW_PRESSURE_MIN;
5068 static const int32_t RAW_PRESSURE_MAX;
5069 static const int32_t RAW_ORIENTATION_MIN;
5070 static const int32_t RAW_ORIENTATION_MAX;
5071 static const int32_t RAW_DISTANCE_MIN;
5072 static const int32_t RAW_DISTANCE_MAX;
5073 static const int32_t RAW_TILT_MIN;
5074 static const int32_t RAW_TILT_MAX;
5075 static const int32_t RAW_ID_MIN;
5076 static const int32_t RAW_ID_MAX;
5077 static const int32_t RAW_SLOT_MIN;
5078 static const int32_t RAW_SLOT_MAX;
5079 static const float X_PRECISION;
5080 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005081 static const float X_PRECISION_VIRTUAL;
5082 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005083
5084 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005085 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005086
5087 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5088
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005089 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005090 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005091
Michael Wrightd02c5b62014-02-10 15:10:22 -08005092 enum Axes {
5093 POSITION = 1 << 0,
5094 TOUCH = 1 << 1,
5095 TOOL = 1 << 2,
5096 PRESSURE = 1 << 3,
5097 ORIENTATION = 1 << 4,
5098 MINOR = 1 << 5,
5099 ID = 1 << 6,
5100 DISTANCE = 1 << 7,
5101 TILT = 1 << 8,
5102 SLOT = 1 << 9,
5103 TOOL_TYPE = 1 << 10,
5104 };
5105
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005106 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5107 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005108 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005109 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005110 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005111 int32_t toRawX(float displayX);
5112 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005113 int32_t toRotatedRawX(float displayX);
5114 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005115 float toCookedX(float rawX, float rawY);
5116 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005117 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005118 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005119 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005120 float toDisplayY(int32_t rawY, int32_t displayHeight);
5121
Michael Wrightd02c5b62014-02-10 15:10:22 -08005122};
5123
5124const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5125const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5126const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5127const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5128const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5129const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5130const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5131const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005132const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5133const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005134const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5135const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5136const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5137const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5138const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5139const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5140const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5141const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5142const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5143const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5144const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5145const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005146const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5147 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5148const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5149 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005150const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5151 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005152
5153const float TouchInputMapperTest::GEOMETRIC_SCALE =
5154 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5155 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5156
5157const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5158 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5159 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5160};
5161
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005162void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005163 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5164 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005165}
5166
5167void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5168 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5169 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005170}
5171
Santos Cordonfa5cf462017-04-05 10:37:00 -07005172void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005173 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5174 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5175 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005176}
5177
Michael Wrightd02c5b62014-02-10 15:10:22 -08005178void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005179 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5180 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5181 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5182 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005183}
5184
Jason Gerecke489fda82012-09-07 17:19:40 -07005185void TouchInputMapperTest::prepareLocationCalibration() {
5186 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5187}
5188
Michael Wrightd02c5b62014-02-10 15:10:22 -08005189int32_t TouchInputMapperTest::toRawX(float displayX) {
5190 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5191}
5192
5193int32_t TouchInputMapperTest::toRawY(float displayY) {
5194 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5195}
5196
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005197int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5198 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5199}
5200
5201int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5202 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5203}
5204
Jason Gerecke489fda82012-09-07 17:19:40 -07005205float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5206 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5207 return rawX;
5208}
5209
5210float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5211 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5212 return rawY;
5213}
5214
Michael Wrightd02c5b62014-02-10 15:10:22 -08005215float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005216 return toDisplayX(rawX, DISPLAY_WIDTH);
5217}
5218
5219float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5220 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005221}
5222
5223float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005224 return toDisplayY(rawY, DISPLAY_HEIGHT);
5225}
5226
5227float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5228 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005229}
5230
5231
5232// --- SingleTouchInputMapperTest ---
5233
5234class SingleTouchInputMapperTest : public TouchInputMapperTest {
5235protected:
5236 void prepareButtons();
5237 void prepareAxes(int axes);
5238
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005239 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5240 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5241 void processUp(SingleTouchInputMapper& mappery);
5242 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5243 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5244 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5245 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5246 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5247 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005248};
5249
5250void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005251 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005252}
5253
5254void SingleTouchInputMapperTest::prepareAxes(int axes) {
5255 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005256 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5257 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005258 }
5259 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005260 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5261 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005262 }
5263 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005264 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5265 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005266 }
5267 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005268 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5269 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005270 }
5271 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005272 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5273 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005274 }
5275}
5276
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005277void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005278 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5279 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5280 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005281}
5282
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005283void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005284 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5285 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005286}
5287
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005288void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005289 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005290}
5291
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005292void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005293 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005294}
5295
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005296void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5297 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005298 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005299}
5300
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005301void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005303}
5304
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005305void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5306 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5308 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005309}
5310
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005311void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5312 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005313 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005314}
5315
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005316void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005317 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005318}
5319
Michael Wrightd02c5b62014-02-10 15:10:22 -08005320TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005321 prepareButtons();
5322 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005323 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005324
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005325 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005326}
5327
5328TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005329 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
5330 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005331 prepareButtons();
5332 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005333 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005334
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005335 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005336}
5337
5338TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005339 prepareButtons();
5340 prepareAxes(POSITION);
5341 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005342 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005343
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005344 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005345}
5346
5347TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005348 prepareButtons();
5349 prepareAxes(POSITION);
5350 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005351 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005352
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005353 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005354}
5355
5356TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005357 addConfigurationProperty("touch.deviceType", "touchScreen");
5358 prepareDisplay(DISPLAY_ORIENTATION_0);
5359 prepareButtons();
5360 prepareAxes(POSITION);
5361 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005362 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005363
5364 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005365 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005366
5367 // Virtual key is down.
5368 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5369 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5370 processDown(mapper, x, y);
5371 processSync(mapper);
5372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5373
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005374 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005375
5376 // Virtual key is up.
5377 processUp(mapper);
5378 processSync(mapper);
5379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5380
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005381 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005382}
5383
5384TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005385 addConfigurationProperty("touch.deviceType", "touchScreen");
5386 prepareDisplay(DISPLAY_ORIENTATION_0);
5387 prepareButtons();
5388 prepareAxes(POSITION);
5389 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005390 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005391
5392 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005393 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005394
5395 // Virtual key is down.
5396 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5397 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5398 processDown(mapper, x, y);
5399 processSync(mapper);
5400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5401
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005402 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005403
5404 // Virtual key is up.
5405 processUp(mapper);
5406 processSync(mapper);
5407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5408
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005409 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005410}
5411
5412TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005413 addConfigurationProperty("touch.deviceType", "touchScreen");
5414 prepareDisplay(DISPLAY_ORIENTATION_0);
5415 prepareButtons();
5416 prepareAxes(POSITION);
5417 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005418 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005419
Michael Wrightd02c5b62014-02-10 15:10:22 -08005420 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005421 ASSERT_TRUE(
5422 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005423 ASSERT_TRUE(flags[0]);
5424 ASSERT_FALSE(flags[1]);
5425}
5426
5427TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005428 addConfigurationProperty("touch.deviceType", "touchScreen");
5429 prepareDisplay(DISPLAY_ORIENTATION_0);
5430 prepareButtons();
5431 prepareAxes(POSITION);
5432 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005433 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005434
arthurhungdcef2dc2020-08-11 14:47:50 +08005435 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005436
5437 NotifyKeyArgs args;
5438
5439 // Press virtual key.
5440 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5441 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5442 processDown(mapper, x, y);
5443 processSync(mapper);
5444
5445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5446 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5447 ASSERT_EQ(DEVICE_ID, args.deviceId);
5448 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5449 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5450 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5451 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5452 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5453 ASSERT_EQ(KEY_HOME, args.scanCode);
5454 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5455 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5456
5457 // Release virtual key.
5458 processUp(mapper);
5459 processSync(mapper);
5460
5461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5462 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5463 ASSERT_EQ(DEVICE_ID, args.deviceId);
5464 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5465 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5466 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5467 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5468 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5469 ASSERT_EQ(KEY_HOME, args.scanCode);
5470 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5471 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5472
5473 // Should not have sent any motions.
5474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5475}
5476
5477TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005478 addConfigurationProperty("touch.deviceType", "touchScreen");
5479 prepareDisplay(DISPLAY_ORIENTATION_0);
5480 prepareButtons();
5481 prepareAxes(POSITION);
5482 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005483 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005484
arthurhungdcef2dc2020-08-11 14:47:50 +08005485 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005486
5487 NotifyKeyArgs keyArgs;
5488
5489 // Press virtual key.
5490 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5491 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5492 processDown(mapper, x, y);
5493 processSync(mapper);
5494
5495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5496 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5497 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5498 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5499 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5500 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5501 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5502 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5503 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5504 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5505 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5506
5507 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5508 // into the display area.
5509 y -= 100;
5510 processMove(mapper, x, y);
5511 processSync(mapper);
5512
5513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5514 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5515 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5516 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5517 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5518 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5519 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5520 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5521 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5522 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5523 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5524 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5525
5526 NotifyMotionArgs motionArgs;
5527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5528 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5529 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5530 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5531 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5532 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5533 ASSERT_EQ(0, motionArgs.flags);
5534 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5535 ASSERT_EQ(0, motionArgs.buttonState);
5536 ASSERT_EQ(0, motionArgs.edgeFlags);
5537 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5538 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5539 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5541 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5542 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5543 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5544 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5545
5546 // Keep moving out of bounds. Should generate a pointer move.
5547 y -= 50;
5548 processMove(mapper, x, y);
5549 processSync(mapper);
5550
5551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5552 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5553 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5554 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5555 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5556 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5557 ASSERT_EQ(0, motionArgs.flags);
5558 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5559 ASSERT_EQ(0, motionArgs.buttonState);
5560 ASSERT_EQ(0, motionArgs.edgeFlags);
5561 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5562 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5563 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5564 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5565 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5566 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5567 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5568 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5569
5570 // Release out of bounds. Should generate a pointer up.
5571 processUp(mapper);
5572 processSync(mapper);
5573
5574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5575 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5576 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5577 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5578 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5579 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5580 ASSERT_EQ(0, motionArgs.flags);
5581 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5582 ASSERT_EQ(0, motionArgs.buttonState);
5583 ASSERT_EQ(0, motionArgs.edgeFlags);
5584 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5585 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5586 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5588 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5589 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5590 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5591 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5592
5593 // Should not have sent any more keys or motions.
5594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5596}
5597
5598TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005599 addConfigurationProperty("touch.deviceType", "touchScreen");
5600 prepareDisplay(DISPLAY_ORIENTATION_0);
5601 prepareButtons();
5602 prepareAxes(POSITION);
5603 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005604 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005605
arthurhungdcef2dc2020-08-11 14:47:50 +08005606 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005607
5608 NotifyMotionArgs motionArgs;
5609
5610 // Initially go down out of bounds.
5611 int32_t x = -10;
5612 int32_t y = -10;
5613 processDown(mapper, x, y);
5614 processSync(mapper);
5615
5616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5617
5618 // Move into the display area. Should generate a pointer down.
5619 x = 50;
5620 y = 75;
5621 processMove(mapper, x, y);
5622 processSync(mapper);
5623
5624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5625 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5626 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5627 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5628 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5629 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5630 ASSERT_EQ(0, motionArgs.flags);
5631 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5632 ASSERT_EQ(0, motionArgs.buttonState);
5633 ASSERT_EQ(0, motionArgs.edgeFlags);
5634 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5635 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5636 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5638 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5639 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5640 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5641 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5642
5643 // Release. Should generate a pointer up.
5644 processUp(mapper);
5645 processSync(mapper);
5646
5647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5648 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5649 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5650 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5651 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5652 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5653 ASSERT_EQ(0, motionArgs.flags);
5654 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5655 ASSERT_EQ(0, motionArgs.buttonState);
5656 ASSERT_EQ(0, motionArgs.edgeFlags);
5657 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5658 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5659 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5661 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5662 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5663 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5664 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5665
5666 // Should not have sent any more keys or motions.
5667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5669}
5670
Santos Cordonfa5cf462017-04-05 10:37:00 -07005671TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005672 addConfigurationProperty("touch.deviceType", "touchScreen");
5673 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5674
5675 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5676 prepareButtons();
5677 prepareAxes(POSITION);
5678 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005679 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005680
arthurhungdcef2dc2020-08-11 14:47:50 +08005681 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005682
5683 NotifyMotionArgs motionArgs;
5684
5685 // Down.
5686 int32_t x = 100;
5687 int32_t y = 125;
5688 processDown(mapper, x, y);
5689 processSync(mapper);
5690
5691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5692 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5693 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5694 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5695 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5696 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5697 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5698 ASSERT_EQ(0, motionArgs.flags);
5699 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5700 ASSERT_EQ(0, motionArgs.buttonState);
5701 ASSERT_EQ(0, motionArgs.edgeFlags);
5702 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5703 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5704 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5705 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5706 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5707 1, 0, 0, 0, 0, 0, 0, 0));
5708 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5709 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5710 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5711
5712 // Move.
5713 x += 50;
5714 y += 75;
5715 processMove(mapper, x, y);
5716 processSync(mapper);
5717
5718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5719 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5720 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5721 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5722 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5723 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5724 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5725 ASSERT_EQ(0, motionArgs.flags);
5726 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5727 ASSERT_EQ(0, motionArgs.buttonState);
5728 ASSERT_EQ(0, motionArgs.edgeFlags);
5729 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5730 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5731 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5732 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5733 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5734 1, 0, 0, 0, 0, 0, 0, 0));
5735 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5736 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5737 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5738
5739 // Up.
5740 processUp(mapper);
5741 processSync(mapper);
5742
5743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5744 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5745 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5746 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5747 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5748 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5749 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5750 ASSERT_EQ(0, motionArgs.flags);
5751 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5752 ASSERT_EQ(0, motionArgs.buttonState);
5753 ASSERT_EQ(0, motionArgs.edgeFlags);
5754 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5755 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5756 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5758 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5759 1, 0, 0, 0, 0, 0, 0, 0));
5760 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5761 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5762 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5763
5764 // Should not have sent any more keys or motions.
5765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5767}
5768
Michael Wrightd02c5b62014-02-10 15:10:22 -08005769TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005770 addConfigurationProperty("touch.deviceType", "touchScreen");
5771 prepareDisplay(DISPLAY_ORIENTATION_0);
5772 prepareButtons();
5773 prepareAxes(POSITION);
5774 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005775 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005776
arthurhungdcef2dc2020-08-11 14:47:50 +08005777 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005778
5779 NotifyMotionArgs motionArgs;
5780
5781 // Down.
5782 int32_t x = 100;
5783 int32_t y = 125;
5784 processDown(mapper, x, y);
5785 processSync(mapper);
5786
5787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5788 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5789 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5790 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5791 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5792 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5793 ASSERT_EQ(0, motionArgs.flags);
5794 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5795 ASSERT_EQ(0, motionArgs.buttonState);
5796 ASSERT_EQ(0, motionArgs.edgeFlags);
5797 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5798 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5799 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5800 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5801 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5802 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5803 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5804 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5805
5806 // Move.
5807 x += 50;
5808 y += 75;
5809 processMove(mapper, x, y);
5810 processSync(mapper);
5811
5812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5813 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5814 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5815 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5816 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5817 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5818 ASSERT_EQ(0, motionArgs.flags);
5819 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5820 ASSERT_EQ(0, motionArgs.buttonState);
5821 ASSERT_EQ(0, motionArgs.edgeFlags);
5822 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5823 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5824 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5825 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5826 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5827 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5828 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5829 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5830
5831 // Up.
5832 processUp(mapper);
5833 processSync(mapper);
5834
5835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5836 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5837 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5838 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5839 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5840 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5841 ASSERT_EQ(0, motionArgs.flags);
5842 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5843 ASSERT_EQ(0, motionArgs.buttonState);
5844 ASSERT_EQ(0, motionArgs.edgeFlags);
5845 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5846 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5847 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5848 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5849 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5850 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5851 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5852 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5853
5854 // Should not have sent any more keys or motions.
5855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5857}
5858
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005859TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005860 addConfigurationProperty("touch.deviceType", "touchScreen");
5861 prepareButtons();
5862 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005863 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5864 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005865 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005866
5867 NotifyMotionArgs args;
5868
5869 // Rotation 90.
5870 prepareDisplay(DISPLAY_ORIENTATION_90);
5871 processDown(mapper, toRawX(50), toRawY(75));
5872 processSync(mapper);
5873
5874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5875 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5876 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5877
5878 processUp(mapper);
5879 processSync(mapper);
5880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5881}
5882
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005883TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005884 addConfigurationProperty("touch.deviceType", "touchScreen");
5885 prepareButtons();
5886 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005887 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5888 // orientation-aware are affected by display rotation.
5889 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005890 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005891
5892 NotifyMotionArgs args;
5893
5894 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005895 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005896 prepareDisplay(DISPLAY_ORIENTATION_0);
5897 processDown(mapper, toRawX(50), toRawY(75));
5898 processSync(mapper);
5899
5900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5901 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5902 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5903
5904 processUp(mapper);
5905 processSync(mapper);
5906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5907
5908 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005909 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005910 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005911 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005912 processSync(mapper);
5913
5914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5915 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5916 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5917
5918 processUp(mapper);
5919 processSync(mapper);
5920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5921
5922 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005923 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005924 prepareDisplay(DISPLAY_ORIENTATION_180);
5925 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5926 processSync(mapper);
5927
5928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5929 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5930 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5931
5932 processUp(mapper);
5933 processSync(mapper);
5934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5935
5936 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005937 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005938 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005939 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005940 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
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005951TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5952 addConfigurationProperty("touch.deviceType", "touchScreen");
5953 prepareButtons();
5954 prepareAxes(POSITION);
5955 addConfigurationProperty("touch.orientationAware", "1");
5956 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5957 clearViewports();
5958 prepareDisplay(DISPLAY_ORIENTATION_0);
5959 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5960 NotifyMotionArgs args;
5961
5962 // Orientation 0.
5963 processDown(mapper, toRawX(50), toRawY(75));
5964 processSync(mapper);
5965
5966 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5967 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5968 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5969
5970 processUp(mapper);
5971 processSync(mapper);
5972 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5973}
5974
5975TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5976 addConfigurationProperty("touch.deviceType", "touchScreen");
5977 prepareButtons();
5978 prepareAxes(POSITION);
5979 addConfigurationProperty("touch.orientationAware", "1");
5980 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5981 clearViewports();
5982 prepareDisplay(DISPLAY_ORIENTATION_0);
5983 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5984 NotifyMotionArgs args;
5985
5986 // Orientation 90.
5987 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5988 processSync(mapper);
5989
5990 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5991 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5992 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5993
5994 processUp(mapper);
5995 processSync(mapper);
5996 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5997}
5998
5999TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6000 addConfigurationProperty("touch.deviceType", "touchScreen");
6001 prepareButtons();
6002 prepareAxes(POSITION);
6003 addConfigurationProperty("touch.orientationAware", "1");
6004 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6005 clearViewports();
6006 prepareDisplay(DISPLAY_ORIENTATION_0);
6007 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6008 NotifyMotionArgs args;
6009
6010 // Orientation 180.
6011 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6012 processSync(mapper);
6013
6014 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6015 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6016 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6017
6018 processUp(mapper);
6019 processSync(mapper);
6020 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6021}
6022
6023TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6024 addConfigurationProperty("touch.deviceType", "touchScreen");
6025 prepareButtons();
6026 prepareAxes(POSITION);
6027 addConfigurationProperty("touch.orientationAware", "1");
6028 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6029 clearViewports();
6030 prepareDisplay(DISPLAY_ORIENTATION_0);
6031 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6032 NotifyMotionArgs args;
6033
6034 // Orientation 270.
6035 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6036 processSync(mapper);
6037
6038 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6039 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6040 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6041
6042 processUp(mapper);
6043 processSync(mapper);
6044 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6045}
6046
6047TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6048 addConfigurationProperty("touch.deviceType", "touchScreen");
6049 prepareButtons();
6050 prepareAxes(POSITION);
6051 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6052 // orientation-aware are affected by display rotation.
6053 addConfigurationProperty("touch.orientationAware", "0");
6054 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6055 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6056
6057 NotifyMotionArgs args;
6058
6059 // Orientation 90, Rotation 0.
6060 clearViewports();
6061 prepareDisplay(DISPLAY_ORIENTATION_0);
6062 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6063 processSync(mapper);
6064
6065 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6066 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6067 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6068
6069 processUp(mapper);
6070 processSync(mapper);
6071 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6072
6073 // Orientation 90, Rotation 90.
6074 clearViewports();
6075 prepareDisplay(DISPLAY_ORIENTATION_90);
6076 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6077 processSync(mapper);
6078
6079 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6080 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6081 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6082
6083 processUp(mapper);
6084 processSync(mapper);
6085 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6086
6087 // Orientation 90, Rotation 180.
6088 clearViewports();
6089 prepareDisplay(DISPLAY_ORIENTATION_180);
6090 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6091 processSync(mapper);
6092
6093 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6094 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6095 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6096
6097 processUp(mapper);
6098 processSync(mapper);
6099 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6100
6101 // Orientation 90, Rotation 270.
6102 clearViewports();
6103 prepareDisplay(DISPLAY_ORIENTATION_270);
6104 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6105 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6106 processSync(mapper);
6107
6108 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6109 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6110 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6111
6112 processUp(mapper);
6113 processSync(mapper);
6114 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6115}
6116
Michael Wrightd02c5b62014-02-10 15:10:22 -08006117TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006118 addConfigurationProperty("touch.deviceType", "touchScreen");
6119 prepareDisplay(DISPLAY_ORIENTATION_0);
6120 prepareButtons();
6121 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006122 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006123
6124 // These calculations are based on the input device calibration documentation.
6125 int32_t rawX = 100;
6126 int32_t rawY = 200;
6127 int32_t rawPressure = 10;
6128 int32_t rawToolMajor = 12;
6129 int32_t rawDistance = 2;
6130 int32_t rawTiltX = 30;
6131 int32_t rawTiltY = 110;
6132
6133 float x = toDisplayX(rawX);
6134 float y = toDisplayY(rawY);
6135 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6136 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6137 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6138 float distance = float(rawDistance);
6139
6140 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6141 float tiltScale = M_PI / 180;
6142 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6143 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6144 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6145 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6146
6147 processDown(mapper, rawX, rawY);
6148 processPressure(mapper, rawPressure);
6149 processToolMajor(mapper, rawToolMajor);
6150 processDistance(mapper, rawDistance);
6151 processTilt(mapper, rawTiltX, rawTiltY);
6152 processSync(mapper);
6153
6154 NotifyMotionArgs args;
6155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6156 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6157 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6158 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6159}
6160
Jason Gerecke489fda82012-09-07 17:19:40 -07006161TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006162 addConfigurationProperty("touch.deviceType", "touchScreen");
6163 prepareDisplay(DISPLAY_ORIENTATION_0);
6164 prepareLocationCalibration();
6165 prepareButtons();
6166 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006167 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006168
6169 int32_t rawX = 100;
6170 int32_t rawY = 200;
6171
6172 float x = toDisplayX(toCookedX(rawX, rawY));
6173 float y = toDisplayY(toCookedY(rawX, rawY));
6174
6175 processDown(mapper, rawX, rawY);
6176 processSync(mapper);
6177
6178 NotifyMotionArgs args;
6179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6180 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6181 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6182}
6183
Michael Wrightd02c5b62014-02-10 15:10:22 -08006184TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006185 addConfigurationProperty("touch.deviceType", "touchScreen");
6186 prepareDisplay(DISPLAY_ORIENTATION_0);
6187 prepareButtons();
6188 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006189 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006190
6191 NotifyMotionArgs motionArgs;
6192 NotifyKeyArgs keyArgs;
6193
6194 processDown(mapper, 100, 200);
6195 processSync(mapper);
6196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6197 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6198 ASSERT_EQ(0, motionArgs.buttonState);
6199
6200 // press BTN_LEFT, release BTN_LEFT
6201 processKey(mapper, BTN_LEFT, 1);
6202 processSync(mapper);
6203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6204 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6205 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6206
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6208 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6209 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6210
Michael Wrightd02c5b62014-02-10 15:10:22 -08006211 processKey(mapper, BTN_LEFT, 0);
6212 processSync(mapper);
6213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006214 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006215 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006216
6217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006218 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006219 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006220
6221 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6222 processKey(mapper, BTN_RIGHT, 1);
6223 processKey(mapper, BTN_MIDDLE, 1);
6224 processSync(mapper);
6225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6226 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6227 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6228 motionArgs.buttonState);
6229
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6231 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6232 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6233
6234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6235 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6236 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6237 motionArgs.buttonState);
6238
Michael Wrightd02c5b62014-02-10 15:10:22 -08006239 processKey(mapper, BTN_RIGHT, 0);
6240 processSync(mapper);
6241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006242 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006243 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006244
6245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006246 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006247 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006248
6249 processKey(mapper, BTN_MIDDLE, 0);
6250 processSync(mapper);
6251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006252 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006253 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006254
6255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006256 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006257 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006258
6259 // press BTN_BACK, release BTN_BACK
6260 processKey(mapper, BTN_BACK, 1);
6261 processSync(mapper);
6262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6263 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6264 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006265
Michael Wrightd02c5b62014-02-10 15:10:22 -08006266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006267 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006268 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6269
6270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6271 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6272 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006273
6274 processKey(mapper, BTN_BACK, 0);
6275 processSync(mapper);
6276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006277 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006278 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006279
6280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006281 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006282 ASSERT_EQ(0, motionArgs.buttonState);
6283
Michael Wrightd02c5b62014-02-10 15:10:22 -08006284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6285 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6286 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6287
6288 // press BTN_SIDE, release BTN_SIDE
6289 processKey(mapper, BTN_SIDE, 1);
6290 processSync(mapper);
6291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6292 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6293 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006294
Michael Wrightd02c5b62014-02-10 15:10:22 -08006295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006296 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006297 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6298
6299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6300 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6301 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006302
6303 processKey(mapper, BTN_SIDE, 0);
6304 processSync(mapper);
6305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006306 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006307 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006308
6309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006310 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006311 ASSERT_EQ(0, motionArgs.buttonState);
6312
Michael Wrightd02c5b62014-02-10 15:10:22 -08006313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6314 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6315 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6316
6317 // press BTN_FORWARD, release BTN_FORWARD
6318 processKey(mapper, BTN_FORWARD, 1);
6319 processSync(mapper);
6320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6321 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6322 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006323
Michael Wrightd02c5b62014-02-10 15:10:22 -08006324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006325 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006326 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6327
6328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6329 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6330 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006331
6332 processKey(mapper, BTN_FORWARD, 0);
6333 processSync(mapper);
6334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006335 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006336 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006337
6338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006339 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006340 ASSERT_EQ(0, motionArgs.buttonState);
6341
Michael Wrightd02c5b62014-02-10 15:10:22 -08006342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6343 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6344 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6345
6346 // press BTN_EXTRA, release BTN_EXTRA
6347 processKey(mapper, BTN_EXTRA, 1);
6348 processSync(mapper);
6349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6350 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6351 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006352
Michael Wrightd02c5b62014-02-10 15:10:22 -08006353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006355 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6356
6357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6358 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6359 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006360
6361 processKey(mapper, BTN_EXTRA, 0);
6362 processSync(mapper);
6363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006364 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006365 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006366
6367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006368 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006369 ASSERT_EQ(0, motionArgs.buttonState);
6370
Michael Wrightd02c5b62014-02-10 15:10:22 -08006371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6372 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6373 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6374
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6376
Michael Wrightd02c5b62014-02-10 15:10:22 -08006377 // press BTN_STYLUS, release BTN_STYLUS
6378 processKey(mapper, BTN_STYLUS, 1);
6379 processSync(mapper);
6380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006382 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6383
6384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6385 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6386 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006387
6388 processKey(mapper, BTN_STYLUS, 0);
6389 processSync(mapper);
6390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006391 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006392 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006393
6394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006395 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006396 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006397
6398 // press BTN_STYLUS2, release BTN_STYLUS2
6399 processKey(mapper, BTN_STYLUS2, 1);
6400 processSync(mapper);
6401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6402 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006403 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6404
6405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6406 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6407 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006408
6409 processKey(mapper, BTN_STYLUS2, 0);
6410 processSync(mapper);
6411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006412 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006413 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006414
6415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006416 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006417 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006418
6419 // release touch
6420 processUp(mapper);
6421 processSync(mapper);
6422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6423 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6424 ASSERT_EQ(0, motionArgs.buttonState);
6425}
6426
6427TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006428 addConfigurationProperty("touch.deviceType", "touchScreen");
6429 prepareDisplay(DISPLAY_ORIENTATION_0);
6430 prepareButtons();
6431 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006432 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006433
6434 NotifyMotionArgs motionArgs;
6435
6436 // default tool type is finger
6437 processDown(mapper, 100, 200);
6438 processSync(mapper);
6439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6440 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6441 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6442
6443 // eraser
6444 processKey(mapper, BTN_TOOL_RUBBER, 1);
6445 processSync(mapper);
6446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6447 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6448 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6449
6450 // stylus
6451 processKey(mapper, BTN_TOOL_RUBBER, 0);
6452 processKey(mapper, BTN_TOOL_PEN, 1);
6453 processSync(mapper);
6454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6455 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6456 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6457
6458 // brush
6459 processKey(mapper, BTN_TOOL_PEN, 0);
6460 processKey(mapper, BTN_TOOL_BRUSH, 1);
6461 processSync(mapper);
6462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6463 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6464 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6465
6466 // pencil
6467 processKey(mapper, BTN_TOOL_BRUSH, 0);
6468 processKey(mapper, BTN_TOOL_PENCIL, 1);
6469 processSync(mapper);
6470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6471 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6472 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6473
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006474 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006475 processKey(mapper, BTN_TOOL_PENCIL, 0);
6476 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6477 processSync(mapper);
6478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6480 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6481
6482 // mouse
6483 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6484 processKey(mapper, BTN_TOOL_MOUSE, 1);
6485 processSync(mapper);
6486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6487 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6488 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6489
6490 // lens
6491 processKey(mapper, BTN_TOOL_MOUSE, 0);
6492 processKey(mapper, BTN_TOOL_LENS, 1);
6493 processSync(mapper);
6494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6495 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6496 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6497
6498 // double-tap
6499 processKey(mapper, BTN_TOOL_LENS, 0);
6500 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6501 processSync(mapper);
6502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6503 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6504 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6505
6506 // triple-tap
6507 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6508 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6509 processSync(mapper);
6510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6511 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6512 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6513
6514 // quad-tap
6515 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6516 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6517 processSync(mapper);
6518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6519 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6520 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6521
6522 // finger
6523 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6524 processKey(mapper, BTN_TOOL_FINGER, 1);
6525 processSync(mapper);
6526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6527 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6528 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6529
6530 // stylus trumps finger
6531 processKey(mapper, BTN_TOOL_PEN, 1);
6532 processSync(mapper);
6533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6534 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6535 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6536
6537 // eraser trumps stylus
6538 processKey(mapper, BTN_TOOL_RUBBER, 1);
6539 processSync(mapper);
6540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6541 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6542 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6543
6544 // mouse trumps eraser
6545 processKey(mapper, BTN_TOOL_MOUSE, 1);
6546 processSync(mapper);
6547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6548 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6549 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6550
6551 // back to default tool type
6552 processKey(mapper, BTN_TOOL_MOUSE, 0);
6553 processKey(mapper, BTN_TOOL_RUBBER, 0);
6554 processKey(mapper, BTN_TOOL_PEN, 0);
6555 processKey(mapper, BTN_TOOL_FINGER, 0);
6556 processSync(mapper);
6557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6558 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6559 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6560}
6561
6562TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006563 addConfigurationProperty("touch.deviceType", "touchScreen");
6564 prepareDisplay(DISPLAY_ORIENTATION_0);
6565 prepareButtons();
6566 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006567 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006568 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006569
6570 NotifyMotionArgs motionArgs;
6571
6572 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6573 processKey(mapper, BTN_TOOL_FINGER, 1);
6574 processMove(mapper, 100, 200);
6575 processSync(mapper);
6576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6577 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6579 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6580
6581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6582 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6584 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6585
6586 // move a little
6587 processMove(mapper, 150, 250);
6588 processSync(mapper);
6589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6590 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6592 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6593
6594 // down when BTN_TOUCH is pressed, pressure defaults to 1
6595 processKey(mapper, BTN_TOUCH, 1);
6596 processSync(mapper);
6597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6598 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6600 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6601
6602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6603 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6604 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6605 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6606
6607 // up when BTN_TOUCH is released, hover restored
6608 processKey(mapper, BTN_TOUCH, 0);
6609 processSync(mapper);
6610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6611 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6613 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6614
6615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6616 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6617 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6618 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6619
6620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6621 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6623 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6624
6625 // exit hover when pointer goes away
6626 processKey(mapper, BTN_TOOL_FINGER, 0);
6627 processSync(mapper);
6628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6629 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6631 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6632}
6633
6634TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006635 addConfigurationProperty("touch.deviceType", "touchScreen");
6636 prepareDisplay(DISPLAY_ORIENTATION_0);
6637 prepareButtons();
6638 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006639 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006640
6641 NotifyMotionArgs motionArgs;
6642
6643 // initially hovering because pressure is 0
6644 processDown(mapper, 100, 200);
6645 processPressure(mapper, 0);
6646 processSync(mapper);
6647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6648 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6649 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6650 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6651
6652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6653 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6655 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6656
6657 // move a little
6658 processMove(mapper, 150, 250);
6659 processSync(mapper);
6660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6661 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6662 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6663 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6664
6665 // down when pressure is non-zero
6666 processPressure(mapper, RAW_PRESSURE_MAX);
6667 processSync(mapper);
6668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6669 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6671 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6672
6673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6674 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6676 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6677
6678 // up when pressure becomes 0, hover restored
6679 processPressure(mapper, 0);
6680 processSync(mapper);
6681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6682 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6683 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6684 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6685
6686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6687 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6688 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6689 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6690
6691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6692 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6693 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6694 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6695
6696 // exit hover when pointer goes away
6697 processUp(mapper);
6698 processSync(mapper);
6699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6700 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6701 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6702 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6703}
6704
lilinnan687e58f2022-07-19 16:00:50 +08006705TEST_F(SingleTouchInputMapperTest,
6706 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6707 addConfigurationProperty("touch.deviceType", "touchScreen");
6708 prepareDisplay(DISPLAY_ORIENTATION_0);
6709 prepareButtons();
6710 prepareAxes(POSITION);
6711 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6712 NotifyMotionArgs motionArgs;
6713
6714 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006715 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006716 processSync(mapper);
6717
6718 // We should receive a down event
6719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6720 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6721
6722 // Change display id
6723 clearViewports();
6724 prepareSecondaryDisplay(ViewportType::INTERNAL);
6725
6726 // We should receive a cancel event
6727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6728 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6729 // Then receive reset called
6730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6731}
6732
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006733TEST_F(SingleTouchInputMapperTest,
6734 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6735 addConfigurationProperty("touch.deviceType", "touchScreen");
6736 prepareDisplay(DISPLAY_ORIENTATION_0);
6737 prepareButtons();
6738 prepareAxes(POSITION);
6739 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6741 NotifyMotionArgs motionArgs;
6742
6743 // Start a new gesture.
6744 processDown(mapper, 100, 200);
6745 processSync(mapper);
6746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6747 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6748
6749 // Make the viewport inactive. This will put the device in disabled mode.
6750 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6751 viewport->isActive = false;
6752 mFakePolicy->updateViewport(*viewport);
6753 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6754
6755 // We should receive a cancel event for the ongoing gesture.
6756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6757 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6758 // Then we should be notified that the device was reset.
6759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6760
6761 // No events are generated while the viewport is inactive.
6762 processMove(mapper, 101, 201);
6763 processSync(mapper);
6764 processDown(mapper, 102, 202);
6765 processSync(mapper);
6766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6767
6768 // Make the viewport active again. The device should resume processing events.
6769 viewport->isActive = true;
6770 mFakePolicy->updateViewport(*viewport);
6771 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6772
6773 // The device is reset because it changes back to direct mode, without generating any events.
6774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6776
6777 // Start a new gesture.
6778 processDown(mapper, 100, 200);
6779 processSync(mapper);
6780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6781 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6782
6783 // No more events.
6784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6786}
6787
Prabir Pradhan5632d622021-09-06 07:57:20 -07006788// --- TouchDisplayProjectionTest ---
6789
6790class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6791public:
6792 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6793 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6794 // rotated equivalent of the given un-rotated physical display bounds.
6795 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6796 uint32_t inverseRotationFlags;
6797 auto width = DISPLAY_WIDTH;
6798 auto height = DISPLAY_HEIGHT;
6799 switch (orientation) {
6800 case DISPLAY_ORIENTATION_90:
6801 inverseRotationFlags = ui::Transform::ROT_270;
6802 std::swap(width, height);
6803 break;
6804 case DISPLAY_ORIENTATION_180:
6805 inverseRotationFlags = ui::Transform::ROT_180;
6806 break;
6807 case DISPLAY_ORIENTATION_270:
6808 inverseRotationFlags = ui::Transform::ROT_90;
6809 std::swap(width, height);
6810 break;
6811 case DISPLAY_ORIENTATION_0:
6812 inverseRotationFlags = ui::Transform::ROT_0;
6813 break;
6814 default:
6815 FAIL() << "Invalid orientation: " << orientation;
6816 }
6817
6818 const ui::Transform rotation(inverseRotationFlags, width, height);
6819 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6820
6821 std::optional<DisplayViewport> internalViewport =
6822 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6823 DisplayViewport& v = *internalViewport;
6824 v.displayId = DISPLAY_ID;
6825 v.orientation = orientation;
6826
6827 v.logicalLeft = 0;
6828 v.logicalTop = 0;
6829 v.logicalRight = 100;
6830 v.logicalBottom = 100;
6831
6832 v.physicalLeft = rotatedPhysicalDisplay.left;
6833 v.physicalTop = rotatedPhysicalDisplay.top;
6834 v.physicalRight = rotatedPhysicalDisplay.right;
6835 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6836
6837 v.deviceWidth = width;
6838 v.deviceHeight = height;
6839
6840 v.isActive = true;
6841 v.uniqueId = UNIQUE_ID;
6842 v.type = ViewportType::INTERNAL;
6843 mFakePolicy->updateViewport(v);
6844 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6845 }
6846
6847 void assertReceivedMove(const Point& point) {
6848 NotifyMotionArgs motionArgs;
6849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6850 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6851 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6852 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6853 1, 0, 0, 0, 0, 0, 0, 0));
6854 }
6855};
6856
6857TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6858 addConfigurationProperty("touch.deviceType", "touchScreen");
6859 prepareDisplay(DISPLAY_ORIENTATION_0);
6860
6861 prepareButtons();
6862 prepareAxes(POSITION);
6863 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6864
6865 NotifyMotionArgs motionArgs;
6866
6867 // Configure the DisplayViewport such that the logical display maps to a subsection of
6868 // the display panel called the physical display. Here, the physical display is bounded by the
6869 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6870 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6871 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6872 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6873
6874 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6875 DISPLAY_ORIENTATION_270}) {
6876 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6877
6878 // Touches outside the physical display should be ignored, and should not generate any
6879 // events. Ensure touches at the following points that lie outside of the physical display
6880 // area do not generate any events.
6881 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6882 processDown(mapper, toRawX(point.x), toRawY(point.y));
6883 processSync(mapper);
6884 processUp(mapper);
6885 processSync(mapper);
6886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6887 << "Unexpected event generated for touch outside physical display at point: "
6888 << point.x << ", " << point.y;
6889 }
6890 }
6891}
6892
6893TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6894 addConfigurationProperty("touch.deviceType", "touchScreen");
6895 prepareDisplay(DISPLAY_ORIENTATION_0);
6896
6897 prepareButtons();
6898 prepareAxes(POSITION);
6899 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6900
6901 NotifyMotionArgs motionArgs;
6902
6903 // Configure the DisplayViewport such that the logical display maps to a subsection of
6904 // the display panel called the physical display. Here, the physical display is bounded by the
6905 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6906 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6907
6908 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6909 DISPLAY_ORIENTATION_270}) {
6910 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6911
6912 // Touches that start outside the physical display should be ignored until it enters the
6913 // physical display bounds, at which point it should generate a down event. Start a touch at
6914 // the point (5, 100), which is outside the physical display bounds.
6915 static const Point kOutsidePoint{5, 100};
6916 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6917 processSync(mapper);
6918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6919
6920 // Move the touch into the physical display area. This should generate a pointer down.
6921 processMove(mapper, toRawX(11), toRawY(21));
6922 processSync(mapper);
6923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6924 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6925 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6926 ASSERT_NO_FATAL_FAILURE(
6927 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6928
6929 // Move the touch inside the physical display area. This should generate a pointer move.
6930 processMove(mapper, toRawX(69), toRawY(159));
6931 processSync(mapper);
6932 assertReceivedMove({69, 159});
6933
6934 // Move outside the physical display area. Since the pointer is already down, this should
6935 // now continue generating events.
6936 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6937 processSync(mapper);
6938 assertReceivedMove(kOutsidePoint);
6939
6940 // Release. This should generate a pointer up.
6941 processUp(mapper);
6942 processSync(mapper);
6943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6944 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6945 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6946 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6947
6948 // Ensure no more events were generated.
6949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6951 }
6952}
6953
Michael Wrightd02c5b62014-02-10 15:10:22 -08006954// --- MultiTouchInputMapperTest ---
6955
6956class MultiTouchInputMapperTest : public TouchInputMapperTest {
6957protected:
6958 void prepareAxes(int axes);
6959
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006960 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6961 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6962 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6963 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6964 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6965 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6966 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6967 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6968 void processId(MultiTouchInputMapper& mapper, int32_t id);
6969 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6970 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6971 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6972 void processMTSync(MultiTouchInputMapper& mapper);
6973 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006974};
6975
6976void MultiTouchInputMapperTest::prepareAxes(int axes) {
6977 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006978 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6979 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006980 }
6981 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006982 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6983 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006984 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006985 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6986 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006987 }
6988 }
6989 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006990 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6991 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006992 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08006993 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006994 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006995 }
6996 }
6997 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006998 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6999 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007000 }
7001 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007002 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7003 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007004 }
7005 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007006 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7007 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007008 }
7009 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007010 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7011 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007012 }
7013 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007014 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7015 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007016 }
7017 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007018 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007019 }
7020}
7021
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007022void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7023 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007024 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7025 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007026}
7027
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007028void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7029 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007030 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007031}
7032
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007033void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7034 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007035 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007036}
7037
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007038void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007039 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007040}
7041
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007042void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007043 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007044}
7045
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007046void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7047 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007048 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007049}
7050
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007051void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007052 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007053}
7054
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007055void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007056 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007057}
7058
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007059void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007060 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007061}
7062
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007063void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007064 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007065}
7066
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007067void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007068 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007069}
7070
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007071void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7072 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007073 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007074}
7075
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007076void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007077 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007078}
7079
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007080void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007081 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007082}
7083
Michael Wrightd02c5b62014-02-10 15:10:22 -08007084TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007085 addConfigurationProperty("touch.deviceType", "touchScreen");
7086 prepareDisplay(DISPLAY_ORIENTATION_0);
7087 prepareAxes(POSITION);
7088 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007089 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007090
arthurhungdcef2dc2020-08-11 14:47:50 +08007091 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007092
7093 NotifyMotionArgs motionArgs;
7094
7095 // Two fingers down at once.
7096 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7097 processPosition(mapper, x1, y1);
7098 processMTSync(mapper);
7099 processPosition(mapper, x2, y2);
7100 processMTSync(mapper);
7101 processSync(mapper);
7102
7103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7104 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7105 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7106 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7107 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7108 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7109 ASSERT_EQ(0, motionArgs.flags);
7110 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7111 ASSERT_EQ(0, motionArgs.buttonState);
7112 ASSERT_EQ(0, motionArgs.edgeFlags);
7113 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7114 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7115 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7116 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7117 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7118 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7119 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7120 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7121
7122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7123 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7124 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7125 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7126 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007127 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007128 ASSERT_EQ(0, motionArgs.flags);
7129 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7130 ASSERT_EQ(0, motionArgs.buttonState);
7131 ASSERT_EQ(0, motionArgs.edgeFlags);
7132 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7133 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7134 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7135 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7136 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7137 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7138 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7139 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7140 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7141 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7142 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7143 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7144
7145 // Move.
7146 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7147 processPosition(mapper, x1, y1);
7148 processMTSync(mapper);
7149 processPosition(mapper, x2, y2);
7150 processMTSync(mapper);
7151 processSync(mapper);
7152
7153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7154 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7155 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7156 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7157 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7158 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7159 ASSERT_EQ(0, motionArgs.flags);
7160 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7161 ASSERT_EQ(0, motionArgs.buttonState);
7162 ASSERT_EQ(0, motionArgs.edgeFlags);
7163 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7164 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7165 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7166 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7167 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7168 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7169 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7170 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7171 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7172 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7173 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7174 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7175
7176 // First finger up.
7177 x2 += 15; y2 -= 20;
7178 processPosition(mapper, x2, y2);
7179 processMTSync(mapper);
7180 processSync(mapper);
7181
7182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7183 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7184 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7185 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7186 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007187 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007188 ASSERT_EQ(0, motionArgs.flags);
7189 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7190 ASSERT_EQ(0, motionArgs.buttonState);
7191 ASSERT_EQ(0, motionArgs.edgeFlags);
7192 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7193 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7194 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7195 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7196 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7197 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7198 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7199 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7200 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7201 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7202 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7203 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7204
7205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7206 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7207 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7208 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7209 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7210 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7211 ASSERT_EQ(0, motionArgs.flags);
7212 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7213 ASSERT_EQ(0, motionArgs.buttonState);
7214 ASSERT_EQ(0, motionArgs.edgeFlags);
7215 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7216 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7217 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7218 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7219 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7220 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7221 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7222 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7223
7224 // Move.
7225 x2 += 20; y2 -= 25;
7226 processPosition(mapper, x2, y2);
7227 processMTSync(mapper);
7228 processSync(mapper);
7229
7230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7231 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7232 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7233 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7234 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7235 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7236 ASSERT_EQ(0, motionArgs.flags);
7237 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7238 ASSERT_EQ(0, motionArgs.buttonState);
7239 ASSERT_EQ(0, motionArgs.edgeFlags);
7240 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7241 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7242 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7243 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7244 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7245 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7246 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7247 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7248
7249 // New finger down.
7250 int32_t x3 = 700, y3 = 300;
7251 processPosition(mapper, x2, y2);
7252 processMTSync(mapper);
7253 processPosition(mapper, x3, y3);
7254 processMTSync(mapper);
7255 processSync(mapper);
7256
7257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7258 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7259 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7260 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7261 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007262 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007263 ASSERT_EQ(0, motionArgs.flags);
7264 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7265 ASSERT_EQ(0, motionArgs.buttonState);
7266 ASSERT_EQ(0, motionArgs.edgeFlags);
7267 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7268 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7269 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7270 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7271 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7272 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7273 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7274 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7275 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7276 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7277 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7278 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7279
7280 // Second finger up.
7281 x3 += 30; y3 -= 20;
7282 processPosition(mapper, x3, y3);
7283 processMTSync(mapper);
7284 processSync(mapper);
7285
7286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7287 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7288 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7289 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7290 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007291 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007292 ASSERT_EQ(0, motionArgs.flags);
7293 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7294 ASSERT_EQ(0, motionArgs.buttonState);
7295 ASSERT_EQ(0, motionArgs.edgeFlags);
7296 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7297 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7298 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7299 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7300 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7301 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7302 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7303 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7304 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7305 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7306 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7307 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7308
7309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7310 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7311 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7312 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7313 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7314 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7315 ASSERT_EQ(0, motionArgs.flags);
7316 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7317 ASSERT_EQ(0, motionArgs.buttonState);
7318 ASSERT_EQ(0, motionArgs.edgeFlags);
7319 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7320 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7321 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7322 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7323 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7324 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7325 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7326 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7327
7328 // Last finger up.
7329 processMTSync(mapper);
7330 processSync(mapper);
7331
7332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7333 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7334 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7335 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7336 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7337 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7338 ASSERT_EQ(0, motionArgs.flags);
7339 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7340 ASSERT_EQ(0, motionArgs.buttonState);
7341 ASSERT_EQ(0, motionArgs.edgeFlags);
7342 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7343 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7344 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7345 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7346 toDisplayX(x3), toDisplayY(y3), 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 // Should not have sent any more keys or motions.
7352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7354}
7355
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007356TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7357 addConfigurationProperty("touch.deviceType", "touchScreen");
7358 prepareDisplay(DISPLAY_ORIENTATION_0);
7359
7360 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7361 /*fuzz*/ 0, /*resolution*/ 10);
7362 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7363 /*fuzz*/ 0, /*resolution*/ 11);
7364 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7365 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7366 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7367 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7368 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7369 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7370 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7371 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7372
7373 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7374
7375 // X and Y axes
7376 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7377 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7378 // Touch major and minor
7379 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7380 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7381 // Tool major and minor
7382 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7383 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7384}
7385
7386TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7387 addConfigurationProperty("touch.deviceType", "touchScreen");
7388 prepareDisplay(DISPLAY_ORIENTATION_0);
7389
7390 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7391 /*fuzz*/ 0, /*resolution*/ 10);
7392 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7393 /*fuzz*/ 0, /*resolution*/ 11);
7394
7395 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7396
7397 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7398
7399 // Touch major and minor
7400 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7401 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7402 // Tool major and minor
7403 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7404 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7405}
7406
Michael Wrightd02c5b62014-02-10 15:10:22 -08007407TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007408 addConfigurationProperty("touch.deviceType", "touchScreen");
7409 prepareDisplay(DISPLAY_ORIENTATION_0);
7410 prepareAxes(POSITION | ID);
7411 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007412 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007413
arthurhungdcef2dc2020-08-11 14:47:50 +08007414 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007415
7416 NotifyMotionArgs motionArgs;
7417
7418 // Two fingers down at once.
7419 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7420 processPosition(mapper, x1, y1);
7421 processId(mapper, 1);
7422 processMTSync(mapper);
7423 processPosition(mapper, x2, y2);
7424 processId(mapper, 2);
7425 processMTSync(mapper);
7426 processSync(mapper);
7427
7428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7429 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7430 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7431 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7432 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7433 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7434 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7435
7436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007437 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007438 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7439 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7440 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7441 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7442 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7444 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7445 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7446 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7447
7448 // Move.
7449 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7450 processPosition(mapper, x1, y1);
7451 processId(mapper, 1);
7452 processMTSync(mapper);
7453 processPosition(mapper, x2, y2);
7454 processId(mapper, 2);
7455 processMTSync(mapper);
7456 processSync(mapper);
7457
7458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7459 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7460 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7461 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7462 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7463 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7464 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7465 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7466 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7467 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7468 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7469
7470 // First finger up.
7471 x2 += 15; y2 -= 20;
7472 processPosition(mapper, x2, y2);
7473 processId(mapper, 2);
7474 processMTSync(mapper);
7475 processSync(mapper);
7476
7477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007478 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007479 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7480 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7481 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7482 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7483 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7484 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7485 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7486 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7487 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7488
7489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7490 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7491 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7492 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7493 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7495 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7496
7497 // Move.
7498 x2 += 20; y2 -= 25;
7499 processPosition(mapper, x2, y2);
7500 processId(mapper, 2);
7501 processMTSync(mapper);
7502 processSync(mapper);
7503
7504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7505 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7506 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7507 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7508 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7510 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7511
7512 // New finger down.
7513 int32_t x3 = 700, y3 = 300;
7514 processPosition(mapper, x2, y2);
7515 processId(mapper, 2);
7516 processMTSync(mapper);
7517 processPosition(mapper, x3, y3);
7518 processId(mapper, 3);
7519 processMTSync(mapper);
7520 processSync(mapper);
7521
7522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007523 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007524 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7525 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7526 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7527 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7528 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7529 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7530 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7531 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7532 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7533
7534 // Second finger up.
7535 x3 += 30; y3 -= 20;
7536 processPosition(mapper, x3, y3);
7537 processId(mapper, 3);
7538 processMTSync(mapper);
7539 processSync(mapper);
7540
7541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007542 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007543 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7544 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7545 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7546 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7547 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7549 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7550 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7551 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7552
7553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7554 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7555 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7556 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7557 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7559 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7560
7561 // Last finger up.
7562 processMTSync(mapper);
7563 processSync(mapper);
7564
7565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7566 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7567 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7568 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7569 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7571 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7572
7573 // Should not have sent any more keys or motions.
7574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7576}
7577
7578TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007579 addConfigurationProperty("touch.deviceType", "touchScreen");
7580 prepareDisplay(DISPLAY_ORIENTATION_0);
7581 prepareAxes(POSITION | ID | SLOT);
7582 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007583 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007584
arthurhungdcef2dc2020-08-11 14:47:50 +08007585 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007586
7587 NotifyMotionArgs motionArgs;
7588
7589 // Two fingers down at once.
7590 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7591 processPosition(mapper, x1, y1);
7592 processId(mapper, 1);
7593 processSlot(mapper, 1);
7594 processPosition(mapper, x2, y2);
7595 processId(mapper, 2);
7596 processSync(mapper);
7597
7598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7599 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7600 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7601 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7602 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7604 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7605
7606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007607 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007608 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7609 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7610 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7611 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7612 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7614 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7615 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7616 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7617
7618 // Move.
7619 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7620 processSlot(mapper, 0);
7621 processPosition(mapper, x1, y1);
7622 processSlot(mapper, 1);
7623 processPosition(mapper, x2, y2);
7624 processSync(mapper);
7625
7626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7627 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7628 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7629 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7630 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7631 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7632 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7633 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7634 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7635 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7636 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7637
7638 // First finger up.
7639 x2 += 15; y2 -= 20;
7640 processSlot(mapper, 0);
7641 processId(mapper, -1);
7642 processSlot(mapper, 1);
7643 processPosition(mapper, x2, y2);
7644 processSync(mapper);
7645
7646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007647 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007648 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7649 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7650 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7651 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7652 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7654 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7655 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7656 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7657
7658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7659 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7660 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7661 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7662 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7663 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7664 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7665
7666 // Move.
7667 x2 += 20; y2 -= 25;
7668 processPosition(mapper, x2, y2);
7669 processSync(mapper);
7670
7671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7672 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7673 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7674 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7675 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7677 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7678
7679 // New finger down.
7680 int32_t x3 = 700, y3 = 300;
7681 processPosition(mapper, x2, y2);
7682 processSlot(mapper, 0);
7683 processId(mapper, 3);
7684 processPosition(mapper, x3, y3);
7685 processSync(mapper);
7686
7687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007688 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007689 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7690 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7691 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7692 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7693 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7694 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7695 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7697 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7698
7699 // Second finger up.
7700 x3 += 30; y3 -= 20;
7701 processSlot(mapper, 1);
7702 processId(mapper, -1);
7703 processSlot(mapper, 0);
7704 processPosition(mapper, x3, y3);
7705 processSync(mapper);
7706
7707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007708 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007709 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7710 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7711 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7712 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7713 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7714 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7715 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7716 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7717 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7718
7719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7720 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7721 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7722 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7723 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7724 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7725 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7726
7727 // Last finger up.
7728 processId(mapper, -1);
7729 processSync(mapper);
7730
7731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7732 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7733 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7734 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7735 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7737 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7738
7739 // Should not have sent any more keys or motions.
7740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7742}
7743
7744TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007745 addConfigurationProperty("touch.deviceType", "touchScreen");
7746 prepareDisplay(DISPLAY_ORIENTATION_0);
7747 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007748 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007749
7750 // These calculations are based on the input device calibration documentation.
7751 int32_t rawX = 100;
7752 int32_t rawY = 200;
7753 int32_t rawTouchMajor = 7;
7754 int32_t rawTouchMinor = 6;
7755 int32_t rawToolMajor = 9;
7756 int32_t rawToolMinor = 8;
7757 int32_t rawPressure = 11;
7758 int32_t rawDistance = 0;
7759 int32_t rawOrientation = 3;
7760 int32_t id = 5;
7761
7762 float x = toDisplayX(rawX);
7763 float y = toDisplayY(rawY);
7764 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7765 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7766 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7767 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7768 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7769 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7770 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7771 float distance = float(rawDistance);
7772
7773 processPosition(mapper, rawX, rawY);
7774 processTouchMajor(mapper, rawTouchMajor);
7775 processTouchMinor(mapper, rawTouchMinor);
7776 processToolMajor(mapper, rawToolMajor);
7777 processToolMinor(mapper, rawToolMinor);
7778 processPressure(mapper, rawPressure);
7779 processOrientation(mapper, rawOrientation);
7780 processDistance(mapper, rawDistance);
7781 processId(mapper, id);
7782 processMTSync(mapper);
7783 processSync(mapper);
7784
7785 NotifyMotionArgs args;
7786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7787 ASSERT_EQ(0, args.pointerProperties[0].id);
7788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7789 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7790 orientation, distance));
7791}
7792
7793TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007794 addConfigurationProperty("touch.deviceType", "touchScreen");
7795 prepareDisplay(DISPLAY_ORIENTATION_0);
7796 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7797 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007798 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007799
7800 // These calculations are based on the input device calibration documentation.
7801 int32_t rawX = 100;
7802 int32_t rawY = 200;
7803 int32_t rawTouchMajor = 140;
7804 int32_t rawTouchMinor = 120;
7805 int32_t rawToolMajor = 180;
7806 int32_t rawToolMinor = 160;
7807
7808 float x = toDisplayX(rawX);
7809 float y = toDisplayY(rawY);
7810 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7811 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7812 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7813 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7814 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7815
7816 processPosition(mapper, rawX, rawY);
7817 processTouchMajor(mapper, rawTouchMajor);
7818 processTouchMinor(mapper, rawTouchMinor);
7819 processToolMajor(mapper, rawToolMajor);
7820 processToolMinor(mapper, rawToolMinor);
7821 processMTSync(mapper);
7822 processSync(mapper);
7823
7824 NotifyMotionArgs args;
7825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7826 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7827 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7828}
7829
7830TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007831 addConfigurationProperty("touch.deviceType", "touchScreen");
7832 prepareDisplay(DISPLAY_ORIENTATION_0);
7833 prepareAxes(POSITION | TOUCH | TOOL);
7834 addConfigurationProperty("touch.size.calibration", "diameter");
7835 addConfigurationProperty("touch.size.scale", "10");
7836 addConfigurationProperty("touch.size.bias", "160");
7837 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007838 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007839
7840 // These calculations are based on the input device calibration documentation.
7841 // Note: We only provide a single common touch/tool value because the device is assumed
7842 // not to emit separate values for each pointer (isSummed = 1).
7843 int32_t rawX = 100;
7844 int32_t rawY = 200;
7845 int32_t rawX2 = 150;
7846 int32_t rawY2 = 250;
7847 int32_t rawTouchMajor = 5;
7848 int32_t rawToolMajor = 8;
7849
7850 float x = toDisplayX(rawX);
7851 float y = toDisplayY(rawY);
7852 float x2 = toDisplayX(rawX2);
7853 float y2 = toDisplayY(rawY2);
7854 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7855 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7856 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7857
7858 processPosition(mapper, rawX, rawY);
7859 processTouchMajor(mapper, rawTouchMajor);
7860 processToolMajor(mapper, rawToolMajor);
7861 processMTSync(mapper);
7862 processPosition(mapper, rawX2, rawY2);
7863 processTouchMajor(mapper, rawTouchMajor);
7864 processToolMajor(mapper, rawToolMajor);
7865 processMTSync(mapper);
7866 processSync(mapper);
7867
7868 NotifyMotionArgs args;
7869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7870 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7871
7872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007873 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007874 ASSERT_EQ(size_t(2), args.pointerCount);
7875 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7876 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7877 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7878 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7879}
7880
7881TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007882 addConfigurationProperty("touch.deviceType", "touchScreen");
7883 prepareDisplay(DISPLAY_ORIENTATION_0);
7884 prepareAxes(POSITION | TOUCH | TOOL);
7885 addConfigurationProperty("touch.size.calibration", "area");
7886 addConfigurationProperty("touch.size.scale", "43");
7887 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007888 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007889
7890 // These calculations are based on the input device calibration documentation.
7891 int32_t rawX = 100;
7892 int32_t rawY = 200;
7893 int32_t rawTouchMajor = 5;
7894 int32_t rawToolMajor = 8;
7895
7896 float x = toDisplayX(rawX);
7897 float y = toDisplayY(rawY);
7898 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7899 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7900 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7901
7902 processPosition(mapper, rawX, rawY);
7903 processTouchMajor(mapper, rawTouchMajor);
7904 processToolMajor(mapper, rawToolMajor);
7905 processMTSync(mapper);
7906 processSync(mapper);
7907
7908 NotifyMotionArgs args;
7909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7910 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7911 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7912}
7913
7914TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007915 addConfigurationProperty("touch.deviceType", "touchScreen");
7916 prepareDisplay(DISPLAY_ORIENTATION_0);
7917 prepareAxes(POSITION | PRESSURE);
7918 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7919 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007920 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007921
Michael Wrightaa449c92017-12-13 21:21:43 +00007922 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007923 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007924 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7925 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7926 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7927
Michael Wrightd02c5b62014-02-10 15:10:22 -08007928 // These calculations are based on the input device calibration documentation.
7929 int32_t rawX = 100;
7930 int32_t rawY = 200;
7931 int32_t rawPressure = 60;
7932
7933 float x = toDisplayX(rawX);
7934 float y = toDisplayY(rawY);
7935 float pressure = float(rawPressure) * 0.01f;
7936
7937 processPosition(mapper, rawX, rawY);
7938 processPressure(mapper, rawPressure);
7939 processMTSync(mapper);
7940 processSync(mapper);
7941
7942 NotifyMotionArgs args;
7943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7944 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7945 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7946}
7947
7948TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007949 addConfigurationProperty("touch.deviceType", "touchScreen");
7950 prepareDisplay(DISPLAY_ORIENTATION_0);
7951 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007952 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007953
7954 NotifyMotionArgs motionArgs;
7955 NotifyKeyArgs keyArgs;
7956
7957 processId(mapper, 1);
7958 processPosition(mapper, 100, 200);
7959 processSync(mapper);
7960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7961 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7962 ASSERT_EQ(0, motionArgs.buttonState);
7963
7964 // press BTN_LEFT, release BTN_LEFT
7965 processKey(mapper, BTN_LEFT, 1);
7966 processSync(mapper);
7967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7968 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7969 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7970
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7972 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7973 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7974
Michael Wrightd02c5b62014-02-10 15:10:22 -08007975 processKey(mapper, BTN_LEFT, 0);
7976 processSync(mapper);
7977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007978 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007979 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007980
7981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007982 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007983 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007984
7985 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7986 processKey(mapper, BTN_RIGHT, 1);
7987 processKey(mapper, BTN_MIDDLE, 1);
7988 processSync(mapper);
7989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7990 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7991 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7992 motionArgs.buttonState);
7993
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7995 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7996 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7997
7998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7999 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8000 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8001 motionArgs.buttonState);
8002
Michael Wrightd02c5b62014-02-10 15:10:22 -08008003 processKey(mapper, BTN_RIGHT, 0);
8004 processSync(mapper);
8005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008006 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008007 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008008
8009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008010 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008011 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008012
8013 processKey(mapper, BTN_MIDDLE, 0);
8014 processSync(mapper);
8015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008016 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008017 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008018
8019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008020 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008021 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008022
8023 // press BTN_BACK, release BTN_BACK
8024 processKey(mapper, BTN_BACK, 1);
8025 processSync(mapper);
8026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8027 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8028 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008029
Michael Wrightd02c5b62014-02-10 15:10:22 -08008030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008031 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008032 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8033
8034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8035 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8036 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008037
8038 processKey(mapper, BTN_BACK, 0);
8039 processSync(mapper);
8040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008041 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008042 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008043
8044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008045 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008046 ASSERT_EQ(0, motionArgs.buttonState);
8047
Michael Wrightd02c5b62014-02-10 15:10:22 -08008048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8049 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8050 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8051
8052 // press BTN_SIDE, release BTN_SIDE
8053 processKey(mapper, BTN_SIDE, 1);
8054 processSync(mapper);
8055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8056 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8057 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008058
Michael Wrightd02c5b62014-02-10 15:10:22 -08008059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008060 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008061 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8062
8063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8064 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8065 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008066
8067 processKey(mapper, BTN_SIDE, 0);
8068 processSync(mapper);
8069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008070 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008071 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008072
8073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008074 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008075 ASSERT_EQ(0, motionArgs.buttonState);
8076
Michael Wrightd02c5b62014-02-10 15:10:22 -08008077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8078 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8079 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8080
8081 // press BTN_FORWARD, release BTN_FORWARD
8082 processKey(mapper, BTN_FORWARD, 1);
8083 processSync(mapper);
8084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8085 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8086 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008087
Michael Wrightd02c5b62014-02-10 15:10:22 -08008088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008089 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008090 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8091
8092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8093 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8094 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008095
8096 processKey(mapper, BTN_FORWARD, 0);
8097 processSync(mapper);
8098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008099 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008100 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008101
8102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008103 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008104 ASSERT_EQ(0, motionArgs.buttonState);
8105
Michael Wrightd02c5b62014-02-10 15:10:22 -08008106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8107 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8108 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8109
8110 // press BTN_EXTRA, release BTN_EXTRA
8111 processKey(mapper, BTN_EXTRA, 1);
8112 processSync(mapper);
8113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8114 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8115 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008116
Michael Wrightd02c5b62014-02-10 15:10:22 -08008117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008118 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008119 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8120
8121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8122 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8123 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008124
8125 processKey(mapper, BTN_EXTRA, 0);
8126 processSync(mapper);
8127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008128 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008129 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008130
8131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008132 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008133 ASSERT_EQ(0, motionArgs.buttonState);
8134
Michael Wrightd02c5b62014-02-10 15:10:22 -08008135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8136 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8137 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8138
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8140
Michael Wrightd02c5b62014-02-10 15:10:22 -08008141 // press BTN_STYLUS, release BTN_STYLUS
8142 processKey(mapper, BTN_STYLUS, 1);
8143 processSync(mapper);
8144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8145 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008146 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8147
8148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8149 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8150 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008151
8152 processKey(mapper, BTN_STYLUS, 0);
8153 processSync(mapper);
8154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008155 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008156 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008157
8158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008159 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008160 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008161
8162 // press BTN_STYLUS2, release BTN_STYLUS2
8163 processKey(mapper, BTN_STYLUS2, 1);
8164 processSync(mapper);
8165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8166 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008167 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8168
8169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8170 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8171 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008172
8173 processKey(mapper, BTN_STYLUS2, 0);
8174 processSync(mapper);
8175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008176 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008177 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008178
8179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008180 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008181 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008182
8183 // release touch
8184 processId(mapper, -1);
8185 processSync(mapper);
8186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8187 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8188 ASSERT_EQ(0, motionArgs.buttonState);
8189}
8190
8191TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008192 addConfigurationProperty("touch.deviceType", "touchScreen");
8193 prepareDisplay(DISPLAY_ORIENTATION_0);
8194 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008195 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008196
8197 NotifyMotionArgs motionArgs;
8198
8199 // default tool type is finger
8200 processId(mapper, 1);
8201 processPosition(mapper, 100, 200);
8202 processSync(mapper);
8203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8204 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8205 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8206
8207 // eraser
8208 processKey(mapper, BTN_TOOL_RUBBER, 1);
8209 processSync(mapper);
8210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8211 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8212 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8213
8214 // stylus
8215 processKey(mapper, BTN_TOOL_RUBBER, 0);
8216 processKey(mapper, BTN_TOOL_PEN, 1);
8217 processSync(mapper);
8218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8219 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8220 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8221
8222 // brush
8223 processKey(mapper, BTN_TOOL_PEN, 0);
8224 processKey(mapper, BTN_TOOL_BRUSH, 1);
8225 processSync(mapper);
8226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8227 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8228 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8229
8230 // pencil
8231 processKey(mapper, BTN_TOOL_BRUSH, 0);
8232 processKey(mapper, BTN_TOOL_PENCIL, 1);
8233 processSync(mapper);
8234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8235 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8236 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8237
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008238 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008239 processKey(mapper, BTN_TOOL_PENCIL, 0);
8240 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8241 processSync(mapper);
8242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8243 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8244 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8245
8246 // mouse
8247 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8248 processKey(mapper, BTN_TOOL_MOUSE, 1);
8249 processSync(mapper);
8250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8251 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8252 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8253
8254 // lens
8255 processKey(mapper, BTN_TOOL_MOUSE, 0);
8256 processKey(mapper, BTN_TOOL_LENS, 1);
8257 processSync(mapper);
8258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8259 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8260 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8261
8262 // double-tap
8263 processKey(mapper, BTN_TOOL_LENS, 0);
8264 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8265 processSync(mapper);
8266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8267 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8268 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8269
8270 // triple-tap
8271 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8272 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8273 processSync(mapper);
8274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8275 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8276 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8277
8278 // quad-tap
8279 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8280 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8281 processSync(mapper);
8282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8283 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8284 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8285
8286 // finger
8287 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8288 processKey(mapper, BTN_TOOL_FINGER, 1);
8289 processSync(mapper);
8290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8291 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8292 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8293
8294 // stylus trumps finger
8295 processKey(mapper, BTN_TOOL_PEN, 1);
8296 processSync(mapper);
8297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8298 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8299 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8300
8301 // eraser trumps stylus
8302 processKey(mapper, BTN_TOOL_RUBBER, 1);
8303 processSync(mapper);
8304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8305 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8306 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8307
8308 // mouse trumps eraser
8309 processKey(mapper, BTN_TOOL_MOUSE, 1);
8310 processSync(mapper);
8311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8312 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8313 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8314
8315 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8316 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8317 processSync(mapper);
8318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8320 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8321
8322 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8323 processToolType(mapper, MT_TOOL_PEN);
8324 processSync(mapper);
8325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8326 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8327 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8328
8329 // back to default tool type
8330 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8331 processKey(mapper, BTN_TOOL_MOUSE, 0);
8332 processKey(mapper, BTN_TOOL_RUBBER, 0);
8333 processKey(mapper, BTN_TOOL_PEN, 0);
8334 processKey(mapper, BTN_TOOL_FINGER, 0);
8335 processSync(mapper);
8336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8337 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8338 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8339}
8340
8341TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008342 addConfigurationProperty("touch.deviceType", "touchScreen");
8343 prepareDisplay(DISPLAY_ORIENTATION_0);
8344 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008345 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008346 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008347
8348 NotifyMotionArgs motionArgs;
8349
8350 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8351 processId(mapper, 1);
8352 processPosition(mapper, 100, 200);
8353 processSync(mapper);
8354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8355 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8356 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8357 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8358
8359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8360 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8361 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8362 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8363
8364 // move a little
8365 processPosition(mapper, 150, 250);
8366 processSync(mapper);
8367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8368 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8369 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8370 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8371
8372 // down when BTN_TOUCH is pressed, pressure defaults to 1
8373 processKey(mapper, BTN_TOUCH, 1);
8374 processSync(mapper);
8375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8376 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8377 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8378 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8379
8380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8381 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8382 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8383 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8384
8385 // up when BTN_TOUCH is released, hover restored
8386 processKey(mapper, BTN_TOUCH, 0);
8387 processSync(mapper);
8388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8389 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8390 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8391 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8392
8393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8394 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8395 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8396 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8397
8398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8399 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8400 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8401 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8402
8403 // exit hover when pointer goes away
8404 processId(mapper, -1);
8405 processSync(mapper);
8406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8407 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8408 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8409 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8410}
8411
8412TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008413 addConfigurationProperty("touch.deviceType", "touchScreen");
8414 prepareDisplay(DISPLAY_ORIENTATION_0);
8415 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008416 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008417
8418 NotifyMotionArgs motionArgs;
8419
8420 // initially hovering because pressure is 0
8421 processId(mapper, 1);
8422 processPosition(mapper, 100, 200);
8423 processPressure(mapper, 0);
8424 processSync(mapper);
8425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8426 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8427 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8428 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8429
8430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8431 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8433 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8434
8435 // move a little
8436 processPosition(mapper, 150, 250);
8437 processSync(mapper);
8438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8439 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8440 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8441 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8442
8443 // down when pressure becomes non-zero
8444 processPressure(mapper, RAW_PRESSURE_MAX);
8445 processSync(mapper);
8446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8447 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8448 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8449 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8450
8451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8452 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8454 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8455
8456 // up when pressure becomes 0, hover restored
8457 processPressure(mapper, 0);
8458 processSync(mapper);
8459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8460 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8461 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8462 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8463
8464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8465 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8466 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8467 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8468
8469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8470 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8471 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8472 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8473
8474 // exit hover when pointer goes away
8475 processId(mapper, -1);
8476 processSync(mapper);
8477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8478 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8479 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8480 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8481}
8482
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008483/**
8484 * Set the input device port <--> display port associations, and check that the
8485 * events are routed to the display that matches the display port.
8486 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8487 */
8488TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008489 const std::string usb2 = "USB2";
8490 const uint8_t hdmi1 = 0;
8491 const uint8_t hdmi2 = 1;
8492 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008493 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008494
8495 addConfigurationProperty("touch.deviceType", "touchScreen");
8496 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008497 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008498
8499 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8500 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8501
8502 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8503 // for this input device is specified, and the matching viewport is not present,
8504 // the input device should be disabled (at the mapper level).
8505
8506 // Add viewport for display 2 on hdmi2
8507 prepareSecondaryDisplay(type, hdmi2);
8508 // Send a touch event
8509 processPosition(mapper, 100, 100);
8510 processSync(mapper);
8511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8512
8513 // Add viewport for display 1 on hdmi1
8514 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8515 // Send a touch event again
8516 processPosition(mapper, 100, 100);
8517 processSync(mapper);
8518
8519 NotifyMotionArgs args;
8520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8521 ASSERT_EQ(DISPLAY_ID, args.displayId);
8522}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008523
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008524TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8525 addConfigurationProperty("touch.deviceType", "touchScreen");
8526 prepareAxes(POSITION);
8527 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8528
8529 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8530
8531 prepareDisplay(DISPLAY_ORIENTATION_0);
8532 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8533
8534 // Send a touch event
8535 processPosition(mapper, 100, 100);
8536 processSync(mapper);
8537
8538 NotifyMotionArgs args;
8539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8540 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8541}
8542
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008543TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008544 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008545 std::shared_ptr<FakePointerController> fakePointerController =
8546 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008547 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008548 fakePointerController->setPosition(100, 200);
8549 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008550 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008551
Garfield Tan888a6a42020-01-09 11:39:16 -08008552 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008553 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008554
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008555 prepareDisplay(DISPLAY_ORIENTATION_0);
8556 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008557 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008558
8559 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008560 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008561
8562 NotifyMotionArgs motionArgs;
8563 processPosition(mapper, 100, 100);
8564 processSync(mapper);
8565
8566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8567 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8568 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8569}
8570
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008571/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008572 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8573 */
8574TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8575 addConfigurationProperty("touch.deviceType", "touchScreen");
8576 prepareAxes(POSITION);
8577 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8578
8579 prepareDisplay(DISPLAY_ORIENTATION_0);
8580 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8581 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8582 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8583 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8584
8585 NotifyMotionArgs args;
8586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8587 ASSERT_EQ(26, args.readTime);
8588
8589 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8590 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8591 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8592
8593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8594 ASSERT_EQ(33, args.readTime);
8595}
8596
8597/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008598 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8599 * events should not be delivered to the listener.
8600 */
8601TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8602 addConfigurationProperty("touch.deviceType", "touchScreen");
8603 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8604 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8605 ViewportType::INTERNAL);
8606 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8607 prepareAxes(POSITION);
8608 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8609
8610 NotifyMotionArgs motionArgs;
8611 processPosition(mapper, 100, 100);
8612 processSync(mapper);
8613
8614 mFakeListener->assertNotifyMotionWasNotCalled();
8615}
8616
Garfield Tanc734e4f2021-01-15 20:01:39 -08008617TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8618 addConfigurationProperty("touch.deviceType", "touchScreen");
8619 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8620 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8621 ViewportType::INTERNAL);
8622 std::optional<DisplayViewport> optionalDisplayViewport =
8623 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8624 ASSERT_TRUE(optionalDisplayViewport.has_value());
8625 DisplayViewport displayViewport = *optionalDisplayViewport;
8626
8627 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8628 prepareAxes(POSITION);
8629 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8630
8631 // Finger down
8632 int32_t x = 100, y = 100;
8633 processPosition(mapper, x, y);
8634 processSync(mapper);
8635
8636 NotifyMotionArgs motionArgs;
8637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8638 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8639
8640 // Deactivate display viewport
8641 displayViewport.isActive = false;
8642 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8643 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8644
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008645 // The ongoing touch should be canceled immediately
8646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8647 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8648
8649 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008650 x += 10, y += 10;
8651 processPosition(mapper, x, y);
8652 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008654
8655 // Reactivate display viewport
8656 displayViewport.isActive = true;
8657 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8658 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8659
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008660 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008661 x += 10, y += 10;
8662 processPosition(mapper, x, y);
8663 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8665 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008666}
8667
Arthur Hung7c645402019-01-25 17:45:42 +08008668TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8669 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008670 prepareAxes(POSITION | ID | SLOT);
8671 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008672 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008673
8674 // Create the second touch screen device, and enable multi fingers.
8675 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008676 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008677 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008678 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008679 std::shared_ptr<InputDevice> device2 =
8680 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008681 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008682
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008683 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8684 0 /*flat*/, 0 /*fuzz*/);
8685 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8686 0 /*flat*/, 0 /*fuzz*/);
8687 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8688 0 /*flat*/, 0 /*fuzz*/);
8689 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8690 0 /*flat*/, 0 /*fuzz*/);
8691 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8692 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8693 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008694
8695 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008696 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008697 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8698 device2->reset(ARBITRARY_TIME);
8699
8700 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008701 std::shared_ptr<FakePointerController> fakePointerController =
8702 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008703 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008704
8705 // Setup policy for associated displays and show touches.
8706 const uint8_t hdmi1 = 0;
8707 const uint8_t hdmi2 = 1;
8708 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8709 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8710 mFakePolicy->setShowTouches(true);
8711
8712 // Create displays.
8713 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008714 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008715
8716 // Default device will reconfigure above, need additional reconfiguration for another device.
8717 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan197e0862022-07-01 14:28:00 +00008718 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8719 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008720
8721 // Two fingers down at default display.
8722 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8723 processPosition(mapper, x1, y1);
8724 processId(mapper, 1);
8725 processSlot(mapper, 1);
8726 processPosition(mapper, x2, y2);
8727 processId(mapper, 2);
8728 processSync(mapper);
8729
8730 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8731 fakePointerController->getSpots().find(DISPLAY_ID);
8732 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8733 ASSERT_EQ(size_t(2), iter->second.size());
8734
8735 // Two fingers down at second display.
8736 processPosition(mapper2, x1, y1);
8737 processId(mapper2, 1);
8738 processSlot(mapper2, 1);
8739 processPosition(mapper2, x2, y2);
8740 processId(mapper2, 2);
8741 processSync(mapper2);
8742
8743 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8744 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8745 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008746
8747 // Disable the show touches configuration and ensure the spots are cleared.
8748 mFakePolicy->setShowTouches(false);
8749 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8750 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
8751
8752 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008753}
8754
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008755TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008756 prepareAxes(POSITION);
8757 addConfigurationProperty("touch.deviceType", "touchScreen");
8758 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008759 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008760
8761 NotifyMotionArgs motionArgs;
8762 // Unrotated video frame
8763 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8764 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008765 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008766 processPosition(mapper, 100, 200);
8767 processSync(mapper);
8768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8769 ASSERT_EQ(frames, motionArgs.videoFrames);
8770
8771 // Subsequent touch events should not have any videoframes
8772 // This is implemented separately in FakeEventHub,
8773 // but that should match the behaviour of TouchVideoDevice.
8774 processPosition(mapper, 200, 200);
8775 processSync(mapper);
8776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8777 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8778}
8779
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008780TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008781 prepareAxes(POSITION);
8782 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008783 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008784 // Unrotated video frame
8785 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8786 NotifyMotionArgs motionArgs;
8787
8788 // Test all 4 orientations
8789 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008790 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8791 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8792 clearViewports();
8793 prepareDisplay(orientation);
8794 std::vector<TouchVideoFrame> frames{frame};
8795 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8796 processPosition(mapper, 100, 200);
8797 processSync(mapper);
8798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8799 ASSERT_EQ(frames, motionArgs.videoFrames);
8800 }
8801}
8802
8803TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8804 prepareAxes(POSITION);
8805 addConfigurationProperty("touch.deviceType", "touchScreen");
8806 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8807 // orientation-aware are affected by display rotation.
8808 addConfigurationProperty("touch.orientationAware", "0");
8809 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8810 // Unrotated video frame
8811 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8812 NotifyMotionArgs motionArgs;
8813
8814 // Test all 4 orientations
8815 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008816 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8817 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8818 clearViewports();
8819 prepareDisplay(orientation);
8820 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008821 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008822 processPosition(mapper, 100, 200);
8823 processSync(mapper);
8824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008825 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8826 // compared to the display. This is so that when the window transform (which contains the
8827 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8828 // window's coordinate space.
8829 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008830 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08008831
8832 // Release finger.
8833 processSync(mapper);
8834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008835 }
8836}
8837
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008838TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008839 prepareAxes(POSITION);
8840 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008841 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008842 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8843 // so mix these.
8844 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8845 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8846 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8847 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8848 NotifyMotionArgs motionArgs;
8849
8850 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008851 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008852 processPosition(mapper, 100, 200);
8853 processSync(mapper);
8854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008855 ASSERT_EQ(frames, motionArgs.videoFrames);
8856}
8857
8858TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8859 prepareAxes(POSITION);
8860 addConfigurationProperty("touch.deviceType", "touchScreen");
8861 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8862 // orientation-aware are affected by display rotation.
8863 addConfigurationProperty("touch.orientationAware", "0");
8864 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8865 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8866 // so mix these.
8867 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8868 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8869 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8870 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8871 NotifyMotionArgs motionArgs;
8872
8873 prepareDisplay(DISPLAY_ORIENTATION_90);
8874 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8875 processPosition(mapper, 100, 200);
8876 processSync(mapper);
8877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8878 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8879 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8880 // compared to the display. This is so that when the window transform (which contains the
8881 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8882 // window's coordinate space.
8883 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8884 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008885 ASSERT_EQ(frames, motionArgs.videoFrames);
8886}
8887
Arthur Hung9da14732019-09-02 16:16:58 +08008888/**
8889 * If we had defined port associations, but the viewport is not ready, the touch device would be
8890 * expected to be disabled, and it should be enabled after the viewport has found.
8891 */
8892TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008893 constexpr uint8_t hdmi2 = 1;
8894 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008895 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008896
8897 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8898
8899 addConfigurationProperty("touch.deviceType", "touchScreen");
8900 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008901 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008902
8903 ASSERT_EQ(mDevice->isEnabled(), false);
8904
8905 // Add display on hdmi2, the device should be enabled and can receive touch event.
8906 prepareSecondaryDisplay(type, hdmi2);
8907 ASSERT_EQ(mDevice->isEnabled(), true);
8908
8909 // Send a touch event.
8910 processPosition(mapper, 100, 100);
8911 processSync(mapper);
8912
8913 NotifyMotionArgs args;
8914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8915 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8916}
8917
Arthur Hung421eb1c2020-01-16 00:09:42 +08008918TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008919 addConfigurationProperty("touch.deviceType", "touchScreen");
8920 prepareDisplay(DISPLAY_ORIENTATION_0);
8921 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008922 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008923
8924 NotifyMotionArgs motionArgs;
8925
8926 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8927 // finger down
8928 processId(mapper, 1);
8929 processPosition(mapper, x1, y1);
8930 processSync(mapper);
8931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8932 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8933 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8934
8935 // finger move
8936 processId(mapper, 1);
8937 processPosition(mapper, x2, y2);
8938 processSync(mapper);
8939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8940 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8941 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8942
8943 // finger up.
8944 processId(mapper, -1);
8945 processSync(mapper);
8946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8947 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8948 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8949
8950 // new finger down
8951 processId(mapper, 1);
8952 processPosition(mapper, x3, y3);
8953 processSync(mapper);
8954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8955 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8956 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8957}
8958
8959/**
arthurhungcc7f9802020-04-30 17:55:40 +08008960 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8961 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008962 */
arthurhungcc7f9802020-04-30 17:55:40 +08008963TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008964 addConfigurationProperty("touch.deviceType", "touchScreen");
8965 prepareDisplay(DISPLAY_ORIENTATION_0);
8966 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008967 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008968
8969 NotifyMotionArgs motionArgs;
8970
8971 // default tool type is finger
8972 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008973 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008974 processPosition(mapper, x1, y1);
8975 processSync(mapper);
8976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8977 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8978 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8979
8980 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8981 processToolType(mapper, MT_TOOL_PALM);
8982 processSync(mapper);
8983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8984 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8985
8986 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008987 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008988 processPosition(mapper, x2, y2);
8989 processSync(mapper);
8990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8991
8992 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008993 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008994 processSync(mapper);
8995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8996
8997 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008998 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008999 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009000 processPosition(mapper, x3, y3);
9001 processSync(mapper);
9002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9003 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9004 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9005}
9006
arthurhungbf89a482020-04-17 17:37:55 +08009007/**
arthurhungcc7f9802020-04-30 17:55:40 +08009008 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9009 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009010 */
arthurhungcc7f9802020-04-30 17:55:40 +08009011TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009012 addConfigurationProperty("touch.deviceType", "touchScreen");
9013 prepareDisplay(DISPLAY_ORIENTATION_0);
9014 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9015 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9016
9017 NotifyMotionArgs motionArgs;
9018
9019 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009020 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9021 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009022 processPosition(mapper, x1, y1);
9023 processSync(mapper);
9024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9025 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9026 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9027
9028 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009029 processSlot(mapper, SECOND_SLOT);
9030 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009031 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009032 processSync(mapper);
9033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009034 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009035 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9036
9037 // If the tool type of the first finger changes to MT_TOOL_PALM,
9038 // we expect to receive ACTION_POINTER_UP with cancel flag.
9039 processSlot(mapper, FIRST_SLOT);
9040 processId(mapper, FIRST_TRACKING_ID);
9041 processToolType(mapper, MT_TOOL_PALM);
9042 processSync(mapper);
9043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009044 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009045 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9046
9047 // The following MOVE events of second finger should be processed.
9048 processSlot(mapper, SECOND_SLOT);
9049 processId(mapper, SECOND_TRACKING_ID);
9050 processPosition(mapper, x2 + 1, y2 + 1);
9051 processSync(mapper);
9052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9053 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9054 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9055
9056 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9057 // it. Second finger receive move.
9058 processSlot(mapper, FIRST_SLOT);
9059 processId(mapper, INVALID_TRACKING_ID);
9060 processSync(mapper);
9061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9062 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9063 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9064
9065 // Second finger keeps moving.
9066 processSlot(mapper, SECOND_SLOT);
9067 processId(mapper, SECOND_TRACKING_ID);
9068 processPosition(mapper, x2 + 2, y2 + 2);
9069 processSync(mapper);
9070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9071 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9072 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9073
9074 // Second finger up.
9075 processId(mapper, INVALID_TRACKING_ID);
9076 processSync(mapper);
9077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9078 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9079 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9080}
9081
9082/**
9083 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9084 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9085 */
9086TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9087 addConfigurationProperty("touch.deviceType", "touchScreen");
9088 prepareDisplay(DISPLAY_ORIENTATION_0);
9089 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9090 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9091
9092 NotifyMotionArgs motionArgs;
9093
9094 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9095 // First finger down.
9096 processId(mapper, FIRST_TRACKING_ID);
9097 processPosition(mapper, x1, y1);
9098 processSync(mapper);
9099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9100 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9101 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9102
9103 // Second finger down.
9104 processSlot(mapper, SECOND_SLOT);
9105 processId(mapper, SECOND_TRACKING_ID);
9106 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009107 processSync(mapper);
9108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009109 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009110 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9111
arthurhungcc7f9802020-04-30 17:55:40 +08009112 // If the tool type of the first finger changes to MT_TOOL_PALM,
9113 // we expect to receive ACTION_POINTER_UP with cancel flag.
9114 processSlot(mapper, FIRST_SLOT);
9115 processId(mapper, FIRST_TRACKING_ID);
9116 processToolType(mapper, MT_TOOL_PALM);
9117 processSync(mapper);
9118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009119 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009120 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9121
9122 // Second finger keeps moving.
9123 processSlot(mapper, SECOND_SLOT);
9124 processId(mapper, SECOND_TRACKING_ID);
9125 processPosition(mapper, x2 + 1, y2 + 1);
9126 processSync(mapper);
9127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9129
9130 // second finger becomes palm, receive cancel due to only 1 finger is active.
9131 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009132 processToolType(mapper, MT_TOOL_PALM);
9133 processSync(mapper);
9134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9135 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9136
arthurhungcc7f9802020-04-30 17:55:40 +08009137 // third finger down.
9138 processSlot(mapper, THIRD_SLOT);
9139 processId(mapper, THIRD_TRACKING_ID);
9140 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009141 processPosition(mapper, x3, y3);
9142 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9144 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9145 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009146 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9147
9148 // third finger move
9149 processId(mapper, THIRD_TRACKING_ID);
9150 processPosition(mapper, x3 + 1, y3 + 1);
9151 processSync(mapper);
9152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9153 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9154
9155 // first finger up, third finger receive move.
9156 processSlot(mapper, FIRST_SLOT);
9157 processId(mapper, INVALID_TRACKING_ID);
9158 processSync(mapper);
9159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9160 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9161 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9162
9163 // second finger up, third finger receive move.
9164 processSlot(mapper, SECOND_SLOT);
9165 processId(mapper, INVALID_TRACKING_ID);
9166 processSync(mapper);
9167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9168 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9169 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9170
9171 // third finger up.
9172 processSlot(mapper, THIRD_SLOT);
9173 processId(mapper, INVALID_TRACKING_ID);
9174 processSync(mapper);
9175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9176 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9177 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9178}
9179
9180/**
9181 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9182 * and the active finger could still be allowed to receive the events
9183 */
9184TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9185 addConfigurationProperty("touch.deviceType", "touchScreen");
9186 prepareDisplay(DISPLAY_ORIENTATION_0);
9187 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9188 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9189
9190 NotifyMotionArgs motionArgs;
9191
9192 // default tool type is finger
9193 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9194 processId(mapper, FIRST_TRACKING_ID);
9195 processPosition(mapper, x1, y1);
9196 processSync(mapper);
9197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9198 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9199 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9200
9201 // Second finger down.
9202 processSlot(mapper, SECOND_SLOT);
9203 processId(mapper, SECOND_TRACKING_ID);
9204 processPosition(mapper, x2, y2);
9205 processSync(mapper);
9206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009207 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009208 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9209
9210 // If the tool type of the second finger changes to MT_TOOL_PALM,
9211 // we expect to receive ACTION_POINTER_UP with cancel flag.
9212 processId(mapper, SECOND_TRACKING_ID);
9213 processToolType(mapper, MT_TOOL_PALM);
9214 processSync(mapper);
9215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009216 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009217 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9218
9219 // The following MOVE event should be processed.
9220 processSlot(mapper, FIRST_SLOT);
9221 processId(mapper, FIRST_TRACKING_ID);
9222 processPosition(mapper, x1 + 1, y1 + 1);
9223 processSync(mapper);
9224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9225 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9226 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9227
9228 // second finger up.
9229 processSlot(mapper, SECOND_SLOT);
9230 processId(mapper, INVALID_TRACKING_ID);
9231 processSync(mapper);
9232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9233 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9234
9235 // first finger keep moving
9236 processSlot(mapper, FIRST_SLOT);
9237 processId(mapper, FIRST_TRACKING_ID);
9238 processPosition(mapper, x1 + 2, y1 + 2);
9239 processSync(mapper);
9240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9241 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9242
9243 // first finger up.
9244 processId(mapper, INVALID_TRACKING_ID);
9245 processSync(mapper);
9246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9247 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9248 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009249}
9250
Arthur Hung9ad18942021-06-19 02:04:46 +00009251/**
9252 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9253 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9254 * cause slot be valid again.
9255 */
9256TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9257 addConfigurationProperty("touch.deviceType", "touchScreen");
9258 prepareDisplay(DISPLAY_ORIENTATION_0);
9259 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9260 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9261
9262 NotifyMotionArgs motionArgs;
9263
9264 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9265 // First finger down.
9266 processId(mapper, FIRST_TRACKING_ID);
9267 processPosition(mapper, x1, y1);
9268 processPressure(mapper, RAW_PRESSURE_MAX);
9269 processSync(mapper);
9270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9271 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9272 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9273
9274 // First finger move.
9275 processId(mapper, FIRST_TRACKING_ID);
9276 processPosition(mapper, x1 + 1, y1 + 1);
9277 processPressure(mapper, RAW_PRESSURE_MAX);
9278 processSync(mapper);
9279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9280 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9281 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9282
9283 // Second finger down.
9284 processSlot(mapper, SECOND_SLOT);
9285 processId(mapper, SECOND_TRACKING_ID);
9286 processPosition(mapper, x2, y2);
9287 processPressure(mapper, RAW_PRESSURE_MAX);
9288 processSync(mapper);
9289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009290 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009291 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9292
9293 // second finger up with some unexpected data.
9294 processSlot(mapper, SECOND_SLOT);
9295 processId(mapper, INVALID_TRACKING_ID);
9296 processPosition(mapper, x2, y2);
9297 processSync(mapper);
9298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009299 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009300 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9301
9302 // first finger up with some unexpected data.
9303 processSlot(mapper, FIRST_SLOT);
9304 processId(mapper, INVALID_TRACKING_ID);
9305 processPosition(mapper, x2, y2);
9306 processPressure(mapper, RAW_PRESSURE_MAX);
9307 processSync(mapper);
9308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9309 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9310 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9311}
9312
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009313// --- MultiTouchInputMapperTest_ExternalDevice ---
9314
9315class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9316protected:
Chris Yea52ade12020-08-27 16:49:20 -07009317 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009318};
9319
9320/**
9321 * Expect fallback to internal viewport if device is external and external viewport is not present.
9322 */
9323TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9324 prepareAxes(POSITION);
9325 addConfigurationProperty("touch.deviceType", "touchScreen");
9326 prepareDisplay(DISPLAY_ORIENTATION_0);
9327 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9328
9329 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9330
9331 NotifyMotionArgs motionArgs;
9332
9333 // Expect the event to be sent to the internal viewport,
9334 // because an external viewport is not present.
9335 processPosition(mapper, 100, 100);
9336 processSync(mapper);
9337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9338 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9339
9340 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009341 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009342 processPosition(mapper, 100, 100);
9343 processSync(mapper);
9344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9345 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9346}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009347
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009348TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9349 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9350 std::shared_ptr<FakePointerController> fakePointerController =
9351 std::make_shared<FakePointerController>();
9352 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9353 fakePointerController->setPosition(0, 0);
9354 fakePointerController->setButtonState(0);
9355
9356 // prepare device and capture
9357 prepareDisplay(DISPLAY_ORIENTATION_0);
9358 prepareAxes(POSITION | ID | SLOT);
9359 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9360 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9361 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009362 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009363 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9364
9365 // captured touchpad should be a touchpad source
9366 NotifyDeviceResetArgs resetArgs;
9367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9368 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9369
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009370 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009371
9372 const InputDeviceInfo::MotionRange* relRangeX =
9373 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9374 ASSERT_NE(relRangeX, nullptr);
9375 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9376 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9377 const InputDeviceInfo::MotionRange* relRangeY =
9378 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9379 ASSERT_NE(relRangeY, nullptr);
9380 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9381 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9382
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009383 // run captured pointer tests - note that this is unscaled, so input listener events should be
9384 // identical to what the hardware sends (accounting for any
9385 // calibration).
9386 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009387 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009388 processId(mapper, 1);
9389 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9390 processKey(mapper, BTN_TOUCH, 1);
9391 processSync(mapper);
9392
9393 // expect coord[0] to contain initial location of touch 0
9394 NotifyMotionArgs args;
9395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9396 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9397 ASSERT_EQ(1U, args.pointerCount);
9398 ASSERT_EQ(0, args.pointerProperties[0].id);
9399 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9400 ASSERT_NO_FATAL_FAILURE(
9401 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9402
9403 // FINGER 1 DOWN
9404 processSlot(mapper, 1);
9405 processId(mapper, 2);
9406 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9407 processSync(mapper);
9408
9409 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009411 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009412 ASSERT_EQ(2U, args.pointerCount);
9413 ASSERT_EQ(0, args.pointerProperties[0].id);
9414 ASSERT_EQ(1, args.pointerProperties[1].id);
9415 ASSERT_NO_FATAL_FAILURE(
9416 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9417 ASSERT_NO_FATAL_FAILURE(
9418 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9419
9420 // FINGER 1 MOVE
9421 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9422 processSync(mapper);
9423
9424 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9425 // from move
9426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9427 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9428 ASSERT_NO_FATAL_FAILURE(
9429 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9430 ASSERT_NO_FATAL_FAILURE(
9431 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9432
9433 // FINGER 0 MOVE
9434 processSlot(mapper, 0);
9435 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9436 processSync(mapper);
9437
9438 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9440 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9441 ASSERT_NO_FATAL_FAILURE(
9442 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9443 ASSERT_NO_FATAL_FAILURE(
9444 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9445
9446 // BUTTON DOWN
9447 processKey(mapper, BTN_LEFT, 1);
9448 processSync(mapper);
9449
9450 // touchinputmapper design sends a move before button press
9451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9452 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9454 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9455
9456 // BUTTON UP
9457 processKey(mapper, BTN_LEFT, 0);
9458 processSync(mapper);
9459
9460 // touchinputmapper design sends a move after button release
9461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9462 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9464 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9465
9466 // FINGER 0 UP
9467 processId(mapper, -1);
9468 processSync(mapper);
9469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9470 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9471
9472 // FINGER 1 MOVE
9473 processSlot(mapper, 1);
9474 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9475 processSync(mapper);
9476
9477 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9480 ASSERT_EQ(1U, args.pointerCount);
9481 ASSERT_EQ(1, args.pointerProperties[0].id);
9482 ASSERT_NO_FATAL_FAILURE(
9483 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9484
9485 // FINGER 1 UP
9486 processId(mapper, -1);
9487 processKey(mapper, BTN_TOUCH, 0);
9488 processSync(mapper);
9489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9490 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9491
9492 // non captured touchpad should be a mouse source
9493 mFakePolicy->setPointerCapture(false);
9494 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9496 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9497}
9498
9499TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9500 std::shared_ptr<FakePointerController> fakePointerController =
9501 std::make_shared<FakePointerController>();
9502 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9503 fakePointerController->setPosition(0, 0);
9504 fakePointerController->setButtonState(0);
9505
9506 // prepare device and capture
9507 prepareDisplay(DISPLAY_ORIENTATION_0);
9508 prepareAxes(POSITION | ID | SLOT);
9509 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9510 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009511 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009512 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9513 // run uncaptured pointer tests - pushes out generic events
9514 // FINGER 0 DOWN
9515 processId(mapper, 3);
9516 processPosition(mapper, 100, 100);
9517 processKey(mapper, BTN_TOUCH, 1);
9518 processSync(mapper);
9519
9520 // start at (100,100), cursor should be at (0,0) * scale
9521 NotifyMotionArgs args;
9522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9523 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9524 ASSERT_NO_FATAL_FAILURE(
9525 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9526
9527 // FINGER 0 MOVE
9528 processPosition(mapper, 200, 200);
9529 processSync(mapper);
9530
9531 // compute scaling to help with touch position checking
9532 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9533 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9534 float scale =
9535 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9536
9537 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9539 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9541 0, 0, 0, 0, 0, 0, 0));
9542}
9543
9544TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9545 std::shared_ptr<FakePointerController> fakePointerController =
9546 std::make_shared<FakePointerController>();
9547
9548 prepareDisplay(DISPLAY_ORIENTATION_0);
9549 prepareAxes(POSITION | ID | SLOT);
9550 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009551 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009552 mFakePolicy->setPointerCapture(false);
9553 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9554
9555 // uncaptured touchpad should be a pointer device
9556 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9557
9558 // captured touchpad should be a touchpad device
9559 mFakePolicy->setPointerCapture(true);
9560 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9561 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9562}
9563
HQ Liue6983c72022-04-19 22:14:56 +00009564class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9565protected:
9566 float mPointerMovementScale;
9567 float mPointerXZoomScale;
9568 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9569 addConfigurationProperty("touch.deviceType", "pointer");
9570 std::shared_ptr<FakePointerController> fakePointerController =
9571 std::make_shared<FakePointerController>();
9572 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9573 fakePointerController->setPosition(0, 0);
9574 fakePointerController->setButtonState(0);
9575 prepareDisplay(DISPLAY_ORIENTATION_0);
9576
9577 prepareAxes(POSITION);
9578 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9579 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9580 // needs to be disabled, and the pointer gesture needs to be enabled.
9581 mFakePolicy->setPointerCapture(false);
9582 mFakePolicy->setPointerGestureEnabled(true);
9583 mFakePolicy->setPointerController(fakePointerController);
9584
9585 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9586 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9587 mPointerMovementScale =
9588 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9589 mPointerXZoomScale =
9590 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9591 }
9592
9593 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9594 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9595 /*flat*/ 0,
9596 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9597 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9598 /*flat*/ 0,
9599 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9600 }
9601};
9602
9603/**
9604 * Two fingers down on a pointer mode touch pad. The width
9605 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9606 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9607 * be greater than the both value to be freeform gesture, so that after two
9608 * fingers start to move downwards, the gesture should be swipe.
9609 */
9610TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9611 // The min freeform gesture width is 25units/mm x 30mm = 750
9612 // which is greater than fraction of the diagnal length of the touchpad (349).
9613 // Thus, MaxSwipWidth is 750.
9614 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9615 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9616 NotifyMotionArgs motionArgs;
9617
9618 // Two fingers down at once.
9619 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9620 // Pointer's initial position is used the [0,0] coordinate.
9621 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9622
9623 processId(mapper, FIRST_TRACKING_ID);
9624 processPosition(mapper, x1, y1);
9625 processMTSync(mapper);
9626 processId(mapper, SECOND_TRACKING_ID);
9627 processPosition(mapper, x2, y2);
9628 processMTSync(mapper);
9629 processSync(mapper);
9630
9631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9632 ASSERT_EQ(1U, motionArgs.pointerCount);
9633 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9634 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9635 ASSERT_NO_FATAL_FAILURE(
9636 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9637
9638 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9639 // that there should be 1 pointer.
9640 int32_t movingDistance = 200;
9641 y1 += movingDistance;
9642 y2 += movingDistance;
9643
9644 processId(mapper, FIRST_TRACKING_ID);
9645 processPosition(mapper, x1, y1);
9646 processMTSync(mapper);
9647 processId(mapper, SECOND_TRACKING_ID);
9648 processPosition(mapper, x2, y2);
9649 processMTSync(mapper);
9650 processSync(mapper);
9651
9652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9653 ASSERT_EQ(1U, motionArgs.pointerCount);
9654 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9655 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9657 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9658 0, 0, 0, 0));
9659}
9660
9661/**
9662 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9663 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9664 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9665 * value to be freeform gesture, so that after two fingers start to move downwards,
9666 * the gesture should be swipe.
9667 */
9668TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9669 // The min freeform gesture width is 5units/mm x 30mm = 150
9670 // which is greater than fraction of the diagnal length of the touchpad (349).
9671 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
9672 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
9673 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9674 NotifyMotionArgs motionArgs;
9675
9676 // Two fingers down at once.
9677 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9678 // Pointer's initial position is used the [0,0] coordinate.
9679 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9680
9681 processId(mapper, FIRST_TRACKING_ID);
9682 processPosition(mapper, x1, y1);
9683 processMTSync(mapper);
9684 processId(mapper, SECOND_TRACKING_ID);
9685 processPosition(mapper, x2, y2);
9686 processMTSync(mapper);
9687 processSync(mapper);
9688
9689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9690 ASSERT_EQ(1U, motionArgs.pointerCount);
9691 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9692 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9693 ASSERT_NO_FATAL_FAILURE(
9694 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9695
9696 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9697 // and there should be 1 pointer.
9698 int32_t movingDistance = 200;
9699 y1 += movingDistance;
9700 y2 += movingDistance;
9701
9702 processId(mapper, FIRST_TRACKING_ID);
9703 processPosition(mapper, x1, y1);
9704 processMTSync(mapper);
9705 processId(mapper, SECOND_TRACKING_ID);
9706 processPosition(mapper, x2, y2);
9707 processMTSync(mapper);
9708 processSync(mapper);
9709
9710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9711 ASSERT_EQ(1U, motionArgs.pointerCount);
9712 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9713 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9714 // New coordinate is the scaled relative coordinate from the initial coordinate.
9715 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9716 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9717 0, 0, 0, 0));
9718}
9719
9720/**
9721 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
9722 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
9723 * freeform gestures after two fingers start to move downwards.
9724 */
9725TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
9726 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9727 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9728
9729 NotifyMotionArgs motionArgs;
9730
9731 // Two fingers down at once. Wider than the max swipe width.
9732 // The gesture is expected to be PRESS, then transformed to FREEFORM
9733 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
9734
9735 processId(mapper, FIRST_TRACKING_ID);
9736 processPosition(mapper, x1, y1);
9737 processMTSync(mapper);
9738 processId(mapper, SECOND_TRACKING_ID);
9739 processPosition(mapper, x2, y2);
9740 processMTSync(mapper);
9741 processSync(mapper);
9742
9743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9744 ASSERT_EQ(1U, motionArgs.pointerCount);
9745 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9746 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9747 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
9748 ASSERT_NO_FATAL_FAILURE(
9749 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9750
9751 int32_t movingDistance = 200;
9752
9753 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
9754 // then two down events for two pointers.
9755 y1 += movingDistance;
9756 y2 += movingDistance;
9757
9758 processId(mapper, FIRST_TRACKING_ID);
9759 processPosition(mapper, x1, y1);
9760 processMTSync(mapper);
9761 processId(mapper, SECOND_TRACKING_ID);
9762 processPosition(mapper, x2, y2);
9763 processMTSync(mapper);
9764 processSync(mapper);
9765
9766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9767 // The previous PRESS gesture is cancelled, because it is transformed to freeform
9768 ASSERT_EQ(1U, motionArgs.pointerCount);
9769 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9771 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9772 ASSERT_EQ(1U, motionArgs.pointerCount);
9773 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9775 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9776 ASSERT_EQ(2U, motionArgs.pointerCount);
9777 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
9778 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9779 // Two pointers' scaled relative coordinates from their initial centroid.
9780 // Initial y coordinates are 0 as y1 and y2 have the same value.
9781 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
9782 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
9783 // When pointers move, the new coordinates equal to the initial coordinates plus
9784 // scaled moving distance.
9785 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9786 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9787 0, 0, 0, 0));
9788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9789 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9790 0, 0, 0, 0));
9791
9792 // Move two fingers down again, expect one MOVE motion event.
9793 y1 += movingDistance;
9794 y2 += movingDistance;
9795
9796 processId(mapper, FIRST_TRACKING_ID);
9797 processPosition(mapper, x1, y1);
9798 processMTSync(mapper);
9799 processId(mapper, SECOND_TRACKING_ID);
9800 processPosition(mapper, x2, y2);
9801 processMTSync(mapper);
9802 processSync(mapper);
9803
9804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9805 ASSERT_EQ(2U, motionArgs.pointerCount);
9806 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9807 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9808 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9809 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9810 0, 0, 0, 0, 0));
9811 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9812 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9813 0, 0, 0, 0, 0));
9814}
9815
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009816// --- JoystickInputMapperTest ---
9817
9818class JoystickInputMapperTest : public InputMapperTest {
9819protected:
9820 static const int32_t RAW_X_MIN;
9821 static const int32_t RAW_X_MAX;
9822 static const int32_t RAW_Y_MIN;
9823 static const int32_t RAW_Y_MAX;
9824
9825 void SetUp() override {
9826 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9827 }
9828 void prepareAxes() {
9829 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9830 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9831 }
9832
9833 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9834 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9835 }
9836
9837 void processSync(JoystickInputMapper& mapper) {
9838 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9839 }
9840
9841 void prepareVirtualDisplay(int32_t orientation) {
9842 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9843 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9844 NO_PORT, ViewportType::VIRTUAL);
9845 }
9846};
9847
9848const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9849const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9850const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9851const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9852
9853TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9854 prepareAxes();
9855 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9856
9857 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9858
9859 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9860
9861 // Send an axis event
9862 processAxis(mapper, ABS_X, 100);
9863 processSync(mapper);
9864
9865 NotifyMotionArgs args;
9866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9867 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9868
9869 // Send another axis event
9870 processAxis(mapper, ABS_Y, 100);
9871 processSync(mapper);
9872
9873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9874 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9875}
9876
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009877// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009878
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009879class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009880protected:
9881 static const char* DEVICE_NAME;
9882 static const char* DEVICE_LOCATION;
9883 static const int32_t DEVICE_ID;
9884 static const int32_t DEVICE_GENERATION;
9885 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009886 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009887 static const int32_t EVENTHUB_ID;
9888
9889 std::shared_ptr<FakeEventHub> mFakeEventHub;
9890 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009891 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009892 std::unique_ptr<InstrumentedInputReader> mReader;
9893 std::shared_ptr<InputDevice> mDevice;
9894
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009895 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009896 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009897 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009898 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009899 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009900 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009901 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9902 }
9903
9904 void SetUp() override { SetUp(DEVICE_CLASSES); }
9905
9906 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009907 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009908 mFakePolicy.clear();
9909 }
9910
9911 void configureDevice(uint32_t changes) {
9912 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9913 mReader->requestRefreshConfiguration(changes);
9914 mReader->loopOnce();
9915 }
9916 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9917 }
9918
9919 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9920 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009921 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009922 InputDeviceIdentifier identifier;
9923 identifier.name = name;
9924 identifier.location = location;
9925 std::shared_ptr<InputDevice> device =
9926 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9927 identifier);
9928 mReader->pushNextDevice(device);
9929 mFakeEventHub->addDevice(eventHubId, name, classes);
9930 mReader->loopOnce();
9931 return device;
9932 }
9933
9934 template <class T, typename... Args>
9935 T& addControllerAndConfigure(Args... args) {
9936 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9937
9938 return controller;
9939 }
9940};
9941
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009942const char* PeripheralControllerTest::DEVICE_NAME = "device";
9943const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9944const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9945const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9946const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009947const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
9948 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009949const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009950
9951// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009952class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009953protected:
9954 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009955 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009956 }
9957};
9958
9959TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009960 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009961
9962 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9963 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9964}
9965
9966TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009967 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009968
9969 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9970 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9971}
9972
9973// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009974class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009975protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009976 void SetUp() override {
9977 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9978 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009979};
9980
Chris Ye85758332021-05-16 23:05:17 -07009981TEST_F(LightControllerTest, MonoLight) {
9982 RawLightInfo infoMono = {.id = 1,
9983 .name = "Mono",
9984 .maxBrightness = 255,
9985 .flags = InputLightClass::BRIGHTNESS,
9986 .path = ""};
9987 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009988
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009989 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009990 InputDeviceInfo info;
9991 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009992 std::vector<InputDeviceLightInfo> lights = info.getLights();
9993 ASSERT_EQ(1U, lights.size());
9994 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009995
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009996 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9997 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009998}
9999
10000TEST_F(LightControllerTest, RGBLight) {
10001 RawLightInfo infoRed = {.id = 1,
10002 .name = "red",
10003 .maxBrightness = 255,
10004 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10005 .path = ""};
10006 RawLightInfo infoGreen = {.id = 2,
10007 .name = "green",
10008 .maxBrightness = 255,
10009 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10010 .path = ""};
10011 RawLightInfo infoBlue = {.id = 3,
10012 .name = "blue",
10013 .maxBrightness = 255,
10014 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10015 .path = ""};
10016 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10017 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10018 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10019
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010020 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010021 InputDeviceInfo info;
10022 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010023 std::vector<InputDeviceLightInfo> lights = info.getLights();
10024 ASSERT_EQ(1U, lights.size());
10025 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010026
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010027 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10028 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010029}
10030
10031TEST_F(LightControllerTest, MultiColorRGBLight) {
10032 RawLightInfo infoColor = {.id = 1,
10033 .name = "red",
10034 .maxBrightness = 255,
10035 .flags = InputLightClass::BRIGHTNESS |
10036 InputLightClass::MULTI_INTENSITY |
10037 InputLightClass::MULTI_INDEX,
10038 .path = ""};
10039
10040 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10041
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010042 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010043 InputDeviceInfo info;
10044 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010045 std::vector<InputDeviceLightInfo> lights = info.getLights();
10046 ASSERT_EQ(1U, lights.size());
10047 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010048
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010049 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10050 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010051}
10052
10053TEST_F(LightControllerTest, PlayerIdLight) {
10054 RawLightInfo info1 = {.id = 1,
10055 .name = "player1",
10056 .maxBrightness = 255,
10057 .flags = InputLightClass::BRIGHTNESS,
10058 .path = ""};
10059 RawLightInfo info2 = {.id = 2,
10060 .name = "player2",
10061 .maxBrightness = 255,
10062 .flags = InputLightClass::BRIGHTNESS,
10063 .path = ""};
10064 RawLightInfo info3 = {.id = 3,
10065 .name = "player3",
10066 .maxBrightness = 255,
10067 .flags = InputLightClass::BRIGHTNESS,
10068 .path = ""};
10069 RawLightInfo info4 = {.id = 4,
10070 .name = "player4",
10071 .maxBrightness = 255,
10072 .flags = InputLightClass::BRIGHTNESS,
10073 .path = ""};
10074 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10075 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10076 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10077 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10078
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010079 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010080 InputDeviceInfo info;
10081 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010082 std::vector<InputDeviceLightInfo> lights = info.getLights();
10083 ASSERT_EQ(1U, lights.size());
10084 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010085
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010086 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10087 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10088 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010089}
10090
Michael Wrightd02c5b62014-02-10 15:10:22 -080010091} // namespace android