blob: f1c1ffcf6619edbd17f86e0af3d422780c5a0bab [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
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000512 bool isDeviceEnabled(int32_t deviceId) const override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700513 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
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000521 status_t enableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700522 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
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000536 status_t disableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700537 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
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000798 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
799 int32_t deviceId, int32_t absCode) const override {
Chris Yef59a2f42020-10-16 12:55:26 -0700800 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
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000984 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override { return mVibrators; };
Chris Ye87143712020-11-10 05:05:58 +0000985
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
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000994 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override { return {}; }
Chris Yee2b1e5c2021-03-10 22:45:12 -0800995
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000996 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
997 int32_t batteryId) const override {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800998 return std::nullopt;
999 }
Kim Low03ea0352020-11-06 12:45:07 -08001000
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001001 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001002 std::vector<int32_t> ids;
1003 for (const auto& [rawId, info] : mRawLightInfos) {
1004 ids.push_back(rawId);
1005 }
1006 return ids;
1007 }
1008
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001009 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001010 auto it = mRawLightInfos.find(lightId);
1011 if (it == mRawLightInfos.end()) {
1012 return std::nullopt;
1013 }
1014 return it->second;
1015 }
1016
1017 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1018 mLightBrightness.emplace(lightId, brightness);
1019 }
1020
1021 void setLightIntensities(int32_t deviceId, int32_t lightId,
1022 std::unordered_map<LightColor, int32_t> intensities) override {
1023 mLightIntensities.emplace(lightId, intensities);
1024 };
1025
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001026 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001027 auto lightIt = mLightBrightness.find(lightId);
1028 if (lightIt == mLightBrightness.end()) {
1029 return std::nullopt;
1030 }
1031 return lightIt->second;
1032 }
1033
1034 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001035 int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001036 auto lightIt = mLightIntensities.find(lightId);
1037 if (lightIt == mLightIntensities.end()) {
1038 return std::nullopt;
1039 }
1040 return lightIt->second;
1041 };
1042
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001043 void dump(std::string&) const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001044
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001045 void monitor() const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001046
Chris Yea52ade12020-08-27 16:49:20 -07001047 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001048
Chris Yea52ade12020-08-27 16:49:20 -07001049 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001050};
1051
Michael Wrightd02c5b62014-02-10 15:10:22 -08001052// --- FakeInputMapper ---
1053
1054class FakeInputMapper : public InputMapper {
1055 uint32_t mSources;
1056 int32_t mKeyboardType;
1057 int32_t mMetaState;
1058 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1059 KeyedVector<int32_t, int32_t> mScanCodeStates;
1060 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001061 // fake mapping which would normally come from keyCharacterMap
1062 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001063 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001064
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001065 std::mutex mLock;
1066 std::condition_variable mStateChangedCondition;
1067 bool mConfigureWasCalled GUARDED_BY(mLock);
1068 bool mResetWasCalled GUARDED_BY(mLock);
1069 bool mProcessWasCalled GUARDED_BY(mLock);
1070 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001071
Arthur Hungc23540e2018-11-29 20:42:11 +08001072 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001073public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001074 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1075 : InputMapper(deviceContext),
1076 mSources(sources),
1077 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001078 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001079 mConfigureWasCalled(false),
1080 mResetWasCalled(false),
1081 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001082
Chris Yea52ade12020-08-27 16:49:20 -07001083 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001084
1085 void setKeyboardType(int32_t keyboardType) {
1086 mKeyboardType = keyboardType;
1087 }
1088
1089 void setMetaState(int32_t metaState) {
1090 mMetaState = metaState;
1091 }
1092
1093 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001094 std::unique_lock<std::mutex> lock(mLock);
1095 base::ScopedLockAssertion assumeLocked(mLock);
1096 const bool configureCalled =
1097 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1098 return mConfigureWasCalled;
1099 });
1100 if (!configureCalled) {
1101 FAIL() << "Expected configure() to have been called.";
1102 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001103 mConfigureWasCalled = false;
1104 }
1105
1106 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001107 std::unique_lock<std::mutex> lock(mLock);
1108 base::ScopedLockAssertion assumeLocked(mLock);
1109 const bool resetCalled =
1110 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1111 return mResetWasCalled;
1112 });
1113 if (!resetCalled) {
1114 FAIL() << "Expected reset() to have been called.";
1115 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001116 mResetWasCalled = false;
1117 }
1118
Yi Kong9b14ac62018-07-17 13:48:38 -07001119 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001120 std::unique_lock<std::mutex> lock(mLock);
1121 base::ScopedLockAssertion assumeLocked(mLock);
1122 const bool processCalled =
1123 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1124 return mProcessWasCalled;
1125 });
1126 if (!processCalled) {
1127 FAIL() << "Expected process() to have been called.";
1128 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001129 if (outLastEvent) {
1130 *outLastEvent = mLastEvent;
1131 }
1132 mProcessWasCalled = false;
1133 }
1134
1135 void setKeyCodeState(int32_t keyCode, int32_t state) {
1136 mKeyCodeStates.replaceValueFor(keyCode, state);
1137 }
1138
1139 void setScanCodeState(int32_t scanCode, int32_t state) {
1140 mScanCodeStates.replaceValueFor(scanCode, state);
1141 }
1142
1143 void setSwitchState(int32_t switchCode, int32_t state) {
1144 mSwitchStates.replaceValueFor(switchCode, state);
1145 }
1146
1147 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001148 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001149 }
1150
Philip Junker4af3b3d2021-12-14 10:36:55 +01001151 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1152 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1153 }
1154
Michael Wrightd02c5b62014-02-10 15:10:22 -08001155private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001156 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001157
Chris Yea52ade12020-08-27 16:49:20 -07001158 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001159 InputMapper::populateDeviceInfo(deviceInfo);
1160
1161 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1162 deviceInfo->setKeyboardType(mKeyboardType);
1163 }
1164 }
1165
Chris Yea52ade12020-08-27 16:49:20 -07001166 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001167 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001168 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001169
1170 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001171 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001172 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1173 mViewport = config->getDisplayViewportByPort(*displayPort);
1174 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001175
1176 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001177 }
1178
Chris Yea52ade12020-08-27 16:49:20 -07001179 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001180 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001181 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001182 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001183 }
1184
Chris Yea52ade12020-08-27 16:49:20 -07001185 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001186 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001187 mLastEvent = *rawEvent;
1188 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001189 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001190 }
1191
Chris Yea52ade12020-08-27 16:49:20 -07001192 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001193 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1194 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1195 }
1196
Philip Junker4af3b3d2021-12-14 10:36:55 +01001197 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1198 auto it = mKeyCodeMapping.find(locationKeyCode);
1199 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1200 }
1201
Chris Yea52ade12020-08-27 16:49:20 -07001202 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001203 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1204 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1205 }
1206
Chris Yea52ade12020-08-27 16:49:20 -07001207 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001208 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1209 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1210 }
1211
Chris Yea52ade12020-08-27 16:49:20 -07001212 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001213 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -07001214 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001215 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001216 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1217 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1218 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001219 }
1220 }
1221 }
Chris Yea52ade12020-08-27 16:49:20 -07001222 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001223 return result;
1224 }
1225
1226 virtual int32_t getMetaState() {
1227 return mMetaState;
1228 }
1229
1230 virtual void fadePointer() {
1231 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001232
1233 virtual std::optional<int32_t> getAssociatedDisplay() {
1234 if (mViewport) {
1235 return std::make_optional(mViewport->displayId);
1236 }
1237 return std::nullopt;
1238 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001239};
1240
1241
1242// --- InstrumentedInputReader ---
1243
1244class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001245 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001246
1247public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001248 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1249 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001250 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001251 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001252
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001253 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001254
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001255 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001256
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001257 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001258 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001259 InputDeviceIdentifier identifier;
1260 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001261 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001262 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001263 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001264 }
1265
Prabir Pradhan28efc192019-11-05 01:10:04 +00001266 // Make the protected loopOnce method accessible to tests.
1267 using InputReader::loopOnce;
1268
Michael Wrightd02c5b62014-02-10 15:10:22 -08001269protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001270 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1271 const InputDeviceIdentifier& identifier)
1272 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001273 if (!mNextDevices.empty()) {
1274 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1275 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001276 return device;
1277 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001278 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001279 }
1280
arthurhungdcef2dc2020-08-11 14:47:50 +08001281 // --- FakeInputReaderContext ---
1282 class FakeInputReaderContext : public ContextImpl {
1283 int32_t mGlobalMetaState;
1284 bool mUpdateGlobalMetaStateWasCalled;
1285 int32_t mGeneration;
1286
1287 public:
1288 FakeInputReaderContext(InputReader* reader)
1289 : ContextImpl(reader),
1290 mGlobalMetaState(0),
1291 mUpdateGlobalMetaStateWasCalled(false),
1292 mGeneration(1) {}
1293
1294 virtual ~FakeInputReaderContext() {}
1295
1296 void assertUpdateGlobalMetaStateWasCalled() {
1297 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1298 << "Expected updateGlobalMetaState() to have been called.";
1299 mUpdateGlobalMetaStateWasCalled = false;
1300 }
1301
1302 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1303
1304 uint32_t getGeneration() { return mGeneration; }
1305
1306 void updateGlobalMetaState() override {
1307 mUpdateGlobalMetaStateWasCalled = true;
1308 ContextImpl::updateGlobalMetaState();
1309 }
1310
1311 int32_t getGlobalMetaState() override {
1312 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1313 }
1314
1315 int32_t bumpGeneration() override {
1316 mGeneration = ContextImpl::bumpGeneration();
1317 return mGeneration;
1318 }
1319 } mFakeContext;
1320
Michael Wrightd02c5b62014-02-10 15:10:22 -08001321 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001322
1323public:
1324 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001325};
1326
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001327// --- InputReaderPolicyTest ---
1328class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001329protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001330 sp<FakeInputReaderPolicy> mFakePolicy;
1331
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001332 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -07001333 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001334};
1335
1336/**
1337 * Check that empty set of viewports is an acceptable configuration.
1338 * Also try to get internal viewport two different ways - by type and by uniqueId.
1339 *
1340 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1341 * Such configuration is not currently allowed.
1342 */
1343TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001344 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001345
1346 // We didn't add any viewports yet, so there shouldn't be any.
1347 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001348 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001349 ASSERT_FALSE(internalViewport);
1350
1351 // Add an internal viewport, then clear it
1352 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001353 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001354 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001355
1356 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001357 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001358 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001359 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001360
1361 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001362 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001363 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001364 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001365
1366 mFakePolicy->clearViewports();
1367 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001368 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001369 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001370 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001371 ASSERT_FALSE(internalViewport);
1372}
1373
1374TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1375 const std::string internalUniqueId = "local:0";
1376 const std::string externalUniqueId = "local:1";
1377 const std::string virtualUniqueId1 = "virtual:2";
1378 const std::string virtualUniqueId2 = "virtual:3";
1379 constexpr int32_t virtualDisplayId1 = 2;
1380 constexpr int32_t virtualDisplayId2 = 3;
1381
1382 // Add an internal viewport
1383 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001384 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1385 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001386 // Add an external viewport
1387 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001388 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1389 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001390 // Add an virtual viewport
1391 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001392 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1393 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001394 // Add another virtual viewport
1395 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001396 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1397 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001398
1399 // Check matching by type for internal
1400 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001401 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001402 ASSERT_TRUE(internalViewport);
1403 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1404
1405 // Check matching by type for external
1406 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001407 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001408 ASSERT_TRUE(externalViewport);
1409 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1410
1411 // Check matching by uniqueId for virtual viewport #1
1412 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001413 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001414 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001415 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001416 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1417 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1418
1419 // Check matching by uniqueId for virtual viewport #2
1420 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001421 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001422 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001423 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001424 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1425 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1426}
1427
1428
1429/**
1430 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1431 * that lookup works by checking display id.
1432 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1433 */
1434TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1435 const std::string uniqueId1 = "uniqueId1";
1436 const std::string uniqueId2 = "uniqueId2";
1437 constexpr int32_t displayId1 = 2;
1438 constexpr int32_t displayId2 = 3;
1439
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001440 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1441 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001442 for (const ViewportType& type : types) {
1443 mFakePolicy->clearViewports();
1444 // Add a viewport
1445 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001446 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1447 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001448 // Add another viewport
1449 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001450 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1451 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001452
1453 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001454 std::optional<DisplayViewport> viewport1 =
1455 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001456 ASSERT_TRUE(viewport1);
1457 ASSERT_EQ(displayId1, viewport1->displayId);
1458 ASSERT_EQ(type, viewport1->type);
1459
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001460 std::optional<DisplayViewport> viewport2 =
1461 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001462 ASSERT_TRUE(viewport2);
1463 ASSERT_EQ(displayId2, viewport2->displayId);
1464 ASSERT_EQ(type, viewport2->type);
1465
1466 // When there are multiple viewports of the same kind, and uniqueId is not specified
1467 // in the call to getDisplayViewport, then that situation is not supported.
1468 // The viewports can be stored in any order, so we cannot rely on the order, since that
1469 // is just implementation detail.
1470 // However, we can check that it still returns *a* viewport, we just cannot assert
1471 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001472 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001473 ASSERT_TRUE(someViewport);
1474 }
1475}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001476
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001477/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001478 * When we have multiple internal displays make sure we always return the default display when
1479 * querying by type.
1480 */
1481TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1482 const std::string uniqueId1 = "uniqueId1";
1483 const std::string uniqueId2 = "uniqueId2";
1484 constexpr int32_t nonDefaultDisplayId = 2;
1485 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1486 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1487
1488 // Add the default display first and ensure it gets returned.
1489 mFakePolicy->clearViewports();
1490 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001491 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001492 ViewportType::INTERNAL);
1493 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001494 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001495 ViewportType::INTERNAL);
1496
1497 std::optional<DisplayViewport> viewport =
1498 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1499 ASSERT_TRUE(viewport);
1500 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1501 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1502
1503 // Add the default display second to make sure order doesn't matter.
1504 mFakePolicy->clearViewports();
1505 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001506 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001507 ViewportType::INTERNAL);
1508 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001509 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001510 ViewportType::INTERNAL);
1511
1512 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1513 ASSERT_TRUE(viewport);
1514 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1515 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1516}
1517
1518/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001519 * Check getDisplayViewportByPort
1520 */
1521TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001522 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001523 const std::string uniqueId1 = "uniqueId1";
1524 const std::string uniqueId2 = "uniqueId2";
1525 constexpr int32_t displayId1 = 1;
1526 constexpr int32_t displayId2 = 2;
1527 const uint8_t hdmi1 = 0;
1528 const uint8_t hdmi2 = 1;
1529 const uint8_t hdmi3 = 2;
1530
1531 mFakePolicy->clearViewports();
1532 // Add a viewport that's associated with some display port that's not of interest.
1533 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001534 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1535 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001536 // Add another viewport, connected to HDMI1 port
1537 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001538 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1539 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001540
1541 // Check that correct display viewport was returned by comparing the display ports.
1542 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1543 ASSERT_TRUE(hdmi1Viewport);
1544 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1545 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1546
1547 // Check that we can still get the same viewport using the uniqueId
1548 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1549 ASSERT_TRUE(hdmi1Viewport);
1550 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1551 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1552 ASSERT_EQ(type, hdmi1Viewport->type);
1553
1554 // Check that we cannot find a port with "HDMI2", because we never added one
1555 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1556 ASSERT_FALSE(hdmi2Viewport);
1557}
1558
Michael Wrightd02c5b62014-02-10 15:10:22 -08001559// --- InputReaderTest ---
1560
1561class InputReaderTest : public testing::Test {
1562protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001563 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001564 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001565 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001566 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001567
Chris Yea52ade12020-08-27 16:49:20 -07001568 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001569 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001570 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001571 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001572
Prabir Pradhan28efc192019-11-05 01:10:04 +00001573 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001574 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001575 }
1576
Chris Yea52ade12020-08-27 16:49:20 -07001577 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001578 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001579 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001580 }
1581
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001582 void addDevice(int32_t eventHubId, const std::string& name,
1583 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001584 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001585
1586 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001587 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001588 }
1589 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001590 mReader->loopOnce();
1591 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001592 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1593 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001594 }
1595
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001596 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001597 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001598 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001599 }
1600
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001601 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001602 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001603 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001604 }
1605
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001606 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001607 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001608 ftl::Flags<InputDeviceClass> classes,
1609 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001610 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001611 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1612 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001613 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001614 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001615 return mapper;
1616 }
1617};
1618
Chris Ye98d3f532020-10-01 21:48:59 -07001619TEST_F(InputReaderTest, PolicyGetInputDevices) {
1620 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001621 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001622 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001623
1624 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001625 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001626 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001627 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001628 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001629 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1630 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001631 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001632}
1633
Chris Yee7310032020-09-22 15:36:28 -07001634TEST_F(InputReaderTest, GetMergedInputDevices) {
1635 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1636 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1637 // Add two subdevices to device
1638 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1639 // Must add at least one mapper or the device will be ignored!
1640 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1641 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1642
1643 // Push same device instance for next device to be added, so they'll have same identifier.
1644 mReader->pushNextDevice(device);
1645 mReader->pushNextDevice(device);
1646 ASSERT_NO_FATAL_FAILURE(
1647 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1648 ASSERT_NO_FATAL_FAILURE(
1649 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1650
1651 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001652 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001653}
1654
Chris Yee14523a2020-12-19 13:46:00 -08001655TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1656 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1657 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1658 // Add two subdevices to device
1659 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1660 // Must add at least one mapper or the device will be ignored!
1661 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1662 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1663
1664 // Push same device instance for next device to be added, so they'll have same identifier.
1665 mReader->pushNextDevice(device);
1666 mReader->pushNextDevice(device);
1667 // Sensor device is initially disabled
1668 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1669 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1670 nullptr));
1671 // Device is disabled because the only sub device is a sensor device and disabled initially.
1672 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1673 ASSERT_FALSE(device->isEnabled());
1674 ASSERT_NO_FATAL_FAILURE(
1675 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1676 // The merged device is enabled if any sub device is enabled
1677 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1678 ASSERT_TRUE(device->isEnabled());
1679}
1680
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001681TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001682 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001683 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001684 constexpr int32_t eventHubId = 1;
1685 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001686 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001687 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001688 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001689 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001690
Yi Kong9b14ac62018-07-17 13:48:38 -07001691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001692
1693 NotifyDeviceResetArgs resetArgs;
1694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001695 ASSERT_EQ(deviceId, resetArgs.deviceId);
1696
1697 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001698 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001699 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001700
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001702 ASSERT_EQ(deviceId, resetArgs.deviceId);
1703 ASSERT_EQ(device->isEnabled(), false);
1704
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001705 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001706 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001709 ASSERT_EQ(device->isEnabled(), false);
1710
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001711 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001712 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001714 ASSERT_EQ(deviceId, resetArgs.deviceId);
1715 ASSERT_EQ(device->isEnabled(), true);
1716}
1717
Michael Wrightd02c5b62014-02-10 15:10:22 -08001718TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001719 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001720 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001721 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001722 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001723 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001724 AINPUT_SOURCE_KEYBOARD, nullptr);
1725 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001726
1727 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1728 AINPUT_SOURCE_ANY, AKEYCODE_A))
1729 << "Should return unknown when the device id is >= 0 but unknown.";
1730
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001731 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1732 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1733 << "Should return unknown when the device id is valid but the sources are not "
1734 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001735
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001736 ASSERT_EQ(AKEY_STATE_DOWN,
1737 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1738 AKEYCODE_A))
1739 << "Should return value provided by mapper when device id is valid and the device "
1740 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001741
1742 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1743 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1744 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1745
1746 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1747 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1748 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1749}
1750
Philip Junker4af3b3d2021-12-14 10:36:55 +01001751TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1752 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1753 constexpr int32_t eventHubId = 1;
1754 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1755 InputDeviceClass::KEYBOARD,
1756 AINPUT_SOURCE_KEYBOARD, nullptr);
1757 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1758
1759 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1760 << "Should return unknown when the device with the specified id is not found.";
1761
1762 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1763 << "Should return correct mapping when device id is valid and mapping exists.";
1764
1765 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1766 << "Should return the location key code when device id is valid and there's no "
1767 "mapping.";
1768}
1769
1770TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1771 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1772 constexpr int32_t eventHubId = 1;
1773 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1774 InputDeviceClass::JOYSTICK,
1775 AINPUT_SOURCE_GAMEPAD, nullptr);
1776 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1777
1778 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1779 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1780}
1781
Michael Wrightd02c5b62014-02-10 15:10:22 -08001782TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001783 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001784 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001785 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001786 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001787 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001788 AINPUT_SOURCE_KEYBOARD, nullptr);
1789 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001790
1791 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1792 AINPUT_SOURCE_ANY, KEY_A))
1793 << "Should return unknown when the device id is >= 0 but unknown.";
1794
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001795 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1796 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1797 << "Should return unknown when the device id is valid but the sources are not "
1798 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001799
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001800 ASSERT_EQ(AKEY_STATE_DOWN,
1801 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1802 KEY_A))
1803 << "Should return value provided by mapper when device id is valid and the device "
1804 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001805
1806 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1807 AINPUT_SOURCE_TRACKBALL, KEY_A))
1808 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1809
1810 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1811 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1812 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1813}
1814
1815TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001816 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001817 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001818 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001819 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001820 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001821 AINPUT_SOURCE_KEYBOARD, nullptr);
1822 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001823
1824 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1825 AINPUT_SOURCE_ANY, SW_LID))
1826 << "Should return unknown when the device id is >= 0 but unknown.";
1827
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001828 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1829 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1830 << "Should return unknown when the device id is valid but the sources are not "
1831 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001832
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001833 ASSERT_EQ(AKEY_STATE_DOWN,
1834 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1835 SW_LID))
1836 << "Should return value provided by mapper when device id is valid and the device "
1837 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001838
1839 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1840 AINPUT_SOURCE_TRACKBALL, SW_LID))
1841 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1842
1843 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1844 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1845 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1846}
1847
1848TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001849 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001850 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001851 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001852 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001853 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001854 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001855
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001856 mapper.addSupportedKeyCode(AKEYCODE_A);
1857 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001858
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001859 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001860 uint8_t flags[4] = { 0, 0, 0, 1 };
1861
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001862 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08001863 << "Should return false when device id is >= 0 but unknown.";
1864 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1865
1866 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001867 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001868 << "Should return false when device id is valid but the sources are not supported by "
1869 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001870 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1871
1872 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001873 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001874 keyCodes, flags))
1875 << "Should return value provided by mapper when device id is valid and the device "
1876 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001877 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1878
1879 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001880 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1881 << "Should return false when the device id is < 0 but the sources are not supported by "
1882 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001883 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1884
1885 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001886 ASSERT_TRUE(
1887 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1888 << "Should return value provided by mapper when device id is < 0 and one of the "
1889 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001890 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1891}
1892
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001893TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001894 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001895 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001896
1897 NotifyConfigurationChangedArgs args;
1898
1899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1900 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1901}
1902
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001903TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001904 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001905 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001906 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001907 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001908 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001909 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001910 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001911 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001912
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001913 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001914 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001915 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1916
1917 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001918 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001919 ASSERT_EQ(when, event.when);
1920 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001921 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001922 ASSERT_EQ(EV_KEY, event.type);
1923 ASSERT_EQ(KEY_A, event.code);
1924 ASSERT_EQ(1, event.value);
1925}
1926
Garfield Tan1c7bc862020-01-28 13:24:04 -08001927TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001928 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001929 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001930 constexpr int32_t eventHubId = 1;
1931 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001932 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001933 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001934 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001935 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001936
1937 NotifyDeviceResetArgs resetArgs;
1938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001939 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001940
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001941 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001942 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001944 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001945 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001946
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001947 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001948 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001950 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001951 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001952
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001953 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001954 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001956 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001957 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001958}
1959
Garfield Tan1c7bc862020-01-28 13:24:04 -08001960TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1961 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001962 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001963 constexpr int32_t eventHubId = 1;
1964 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1965 // Must add at least one mapper or the device will be ignored!
1966 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001967 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001968 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1969
1970 NotifyDeviceResetArgs resetArgs;
1971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1972 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1973}
1974
Arthur Hungc23540e2018-11-29 20:42:11 +08001975TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001976 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001977 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001978 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001979 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001980 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1981 FakeInputMapper& mapper =
1982 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001983 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001984
1985 const uint8_t hdmi1 = 1;
1986
1987 // Associated touch screen with second display.
1988 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1989
1990 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001991 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001992 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001993 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001994 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001995 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001996 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001997 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001998 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001999 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002000
2001 // Add the device, and make sure all of the callbacks are triggered.
2002 // The device is added after the input port associations are processed since
2003 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002004 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002007 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002008
Arthur Hung2c9a3342019-07-23 14:18:59 +08002009 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002010 ASSERT_EQ(deviceId, device->getId());
2011 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2012 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002013
2014 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002015 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002016 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002017 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002018}
2019
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002020TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2021 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002022 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002023 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2024 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2025 // Must add at least one mapper or the device will be ignored!
2026 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2027 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2028 mReader->pushNextDevice(device);
2029 mReader->pushNextDevice(device);
2030 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2031 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2032
2033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2034
2035 NotifyDeviceResetArgs resetArgs;
2036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2037 ASSERT_EQ(deviceId, resetArgs.deviceId);
2038 ASSERT_TRUE(device->isEnabled());
2039 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2040 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2041
2042 disableDevice(deviceId);
2043 mReader->loopOnce();
2044
2045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2046 ASSERT_EQ(deviceId, resetArgs.deviceId);
2047 ASSERT_FALSE(device->isEnabled());
2048 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2049 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2050
2051 enableDevice(deviceId);
2052 mReader->loopOnce();
2053
2054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2055 ASSERT_EQ(deviceId, resetArgs.deviceId);
2056 ASSERT_TRUE(device->isEnabled());
2057 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2058 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2059}
2060
2061TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2062 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002063 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002064 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2065 // Add two subdevices to device
2066 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2067 FakeInputMapper& mapperDevice1 =
2068 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2069 FakeInputMapper& mapperDevice2 =
2070 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2071 mReader->pushNextDevice(device);
2072 mReader->pushNextDevice(device);
2073 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2074 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2075
2076 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2077 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2078
2079 ASSERT_EQ(AKEY_STATE_DOWN,
2080 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2081 ASSERT_EQ(AKEY_STATE_DOWN,
2082 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2083 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2084 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2085}
2086
Prabir Pradhan7e186182020-11-10 13:56:45 -08002087TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2088 NotifyPointerCaptureChangedArgs args;
2089
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002090 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002091 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2092 mReader->loopOnce();
2093 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002094 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2095 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002096
2097 mFakePolicy->setPointerCapture(false);
2098 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2099 mReader->loopOnce();
2100 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002101 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002102
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002103 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002104 // does not change.
2105 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2106 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002107 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002108}
2109
Chris Ye87143712020-11-10 05:05:58 +00002110class FakeVibratorInputMapper : public FakeInputMapper {
2111public:
2112 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2113 : FakeInputMapper(deviceContext, sources) {}
2114
2115 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2116};
2117
2118TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2119 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002120 ftl::Flags<InputDeviceClass> deviceClass =
2121 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002122 constexpr int32_t eventHubId = 1;
2123 const char* DEVICE_LOCATION = "BLUETOOTH";
2124 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2125 FakeVibratorInputMapper& mapper =
2126 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2127 mReader->pushNextDevice(device);
2128
2129 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2130 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2131
2132 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2133 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2134}
2135
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002136// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002137
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002138class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002139public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002140 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002141
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002142 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002143
2144 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2145
2146 void dump(std::string& dump) override {}
2147
2148 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2149 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002150 }
2151
Chris Yee2b1e5c2021-03-10 22:45:12 -08002152 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2153 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002154 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002155
2156 bool setLightColor(int32_t lightId, int32_t color) override {
2157 getDeviceContext().setLightBrightness(lightId, color >> 24);
2158 return true;
2159 }
2160
2161 std::optional<int32_t> getLightColor(int32_t lightId) override {
2162 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2163 if (!result.has_value()) {
2164 return std::nullopt;
2165 }
2166 return result.value() << 24;
2167 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002168
2169 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2170
2171 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2172
2173private:
2174 InputDeviceContext& mDeviceContext;
2175 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2176 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002177};
2178
Chris Yee2b1e5c2021-03-10 22:45:12 -08002179TEST_F(InputReaderTest, BatteryGetCapacity) {
2180 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002181 ftl::Flags<InputDeviceClass> deviceClass =
2182 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002183 constexpr int32_t eventHubId = 1;
2184 const char* DEVICE_LOCATION = "BLUETOOTH";
2185 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002186 FakePeripheralController& controller =
2187 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002188 mReader->pushNextDevice(device);
2189
2190 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2191
2192 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2193 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2194}
2195
2196TEST_F(InputReaderTest, BatteryGetStatus) {
2197 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002198 ftl::Flags<InputDeviceClass> deviceClass =
2199 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002200 constexpr int32_t eventHubId = 1;
2201 const char* DEVICE_LOCATION = "BLUETOOTH";
2202 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002203 FakePeripheralController& controller =
2204 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002205 mReader->pushNextDevice(device);
2206
2207 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2208
2209 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2210 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2211}
2212
Chris Ye3fdbfef2021-01-06 18:45:18 -08002213TEST_F(InputReaderTest, LightGetColor) {
2214 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002215 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002216 constexpr int32_t eventHubId = 1;
2217 const char* DEVICE_LOCATION = "BLUETOOTH";
2218 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002219 FakePeripheralController& controller =
2220 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002221 mReader->pushNextDevice(device);
2222 RawLightInfo info = {.id = 1,
2223 .name = "Mono",
2224 .maxBrightness = 255,
2225 .flags = InputLightClass::BRIGHTNESS,
2226 .path = ""};
2227 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2228 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2229
2230 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002231
Chris Yee2b1e5c2021-03-10 22:45:12 -08002232 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2233 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002234 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2235 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2236}
2237
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002238// --- InputReaderIntegrationTest ---
2239
2240// These tests create and interact with the InputReader only through its interface.
2241// The InputReader is started during SetUp(), which starts its processing in its own
2242// thread. The tests use linux uinput to emulate input devices.
2243// NOTE: Interacting with the physical device while these tests are running may cause
2244// the tests to fail.
2245class InputReaderIntegrationTest : public testing::Test {
2246protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002247 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002248 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002249 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002250
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002251 std::shared_ptr<FakePointerController> mFakePointerController;
2252
Chris Yea52ade12020-08-27 16:49:20 -07002253 void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002254 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002255 mFakePointerController = std::make_shared<FakePointerController>();
2256 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002257 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2258 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002259
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002260 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2261 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002262 ASSERT_EQ(mReader->start(), OK);
2263
2264 // Since this test is run on a real device, all the input devices connected
2265 // to the test device will show up in mReader. We wait for those input devices to
2266 // show up before beginning the tests.
2267 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2268 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2269 }
2270
Chris Yea52ade12020-08-27 16:49:20 -07002271 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002272 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002273 mReader.reset();
2274 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002275 mFakePolicy.clear();
2276 }
2277};
2278
2279TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2280 // An invalid input device that is only used for this test.
2281 class InvalidUinputDevice : public UinputDevice {
2282 public:
2283 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2284
2285 private:
2286 void configureDevice(int fd, uinput_user_dev* device) override {}
2287 };
2288
2289 const size_t numDevices = mFakePolicy->getInputDevices().size();
2290
2291 // UinputDevice does not set any event or key bits, so InputReader should not
2292 // consider it as a valid device.
2293 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2294 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2295 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2296 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2297
2298 invalidDevice.reset();
2299 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2300 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2301 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2302}
2303
2304TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2305 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2306
2307 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2308 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2309 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2310 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2311
2312 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002313 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002314 const auto& it =
2315 std::find_if(inputDevices.begin(), inputDevices.end(),
2316 [&keyboard](const InputDeviceInfo& info) {
2317 return info.getIdentifier().name == keyboard->getName();
2318 });
2319
2320 ASSERT_NE(it, inputDevices.end());
2321 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2322 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2323 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002324
2325 keyboard.reset();
2326 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2327 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2328 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2329}
2330
2331TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2332 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2333 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2334
2335 NotifyConfigurationChangedArgs configChangedArgs;
2336 ASSERT_NO_FATAL_FAILURE(
2337 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002338 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002339 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2340
2341 NotifyKeyArgs keyArgs;
2342 keyboard->pressAndReleaseHomeKey();
2343 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2344 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002345 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002346 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002347 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002348 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002349 prevTimestamp = keyArgs.eventTime;
2350
2351 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2352 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002353 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002354 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002355 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002356}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002357
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002358/**
2359 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2360 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2361 * are passed to the listener.
2362 */
2363static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2364TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2365 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2366 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2367 NotifyKeyArgs keyArgs;
2368
2369 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2370 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2371 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2372 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2373
2374 controller->pressAndReleaseKey(BTN_GEAR_UP);
2375 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2376 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2377 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2378}
2379
Arthur Hungaab25622020-01-16 11:22:11 +08002380// --- TouchProcessTest ---
2381class TouchIntegrationTest : public InputReaderIntegrationTest {
2382protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002383 const std::string UNIQUE_ID = "local:0";
2384
Chris Yea52ade12020-08-27 16:49:20 -07002385 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002386 InputReaderIntegrationTest::SetUp();
2387 // At least add an internal display.
2388 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2389 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002390 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002391
2392 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2393 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2394 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2395 }
2396
2397 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2398 int32_t orientation, const std::string& uniqueId,
2399 std::optional<uint8_t> physicalPort,
2400 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002401 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2402 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002403 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2404 }
2405
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002406 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2407 NotifyMotionArgs args;
2408 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2409 EXPECT_EQ(action, args.action);
2410 ASSERT_EQ(points.size(), args.pointerCount);
2411 for (size_t i = 0; i < args.pointerCount; i++) {
2412 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2413 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2414 }
2415 }
2416
Arthur Hungaab25622020-01-16 11:22:11 +08002417 std::unique_ptr<UinputTouchScreen> mDevice;
2418};
2419
2420TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2421 NotifyMotionArgs args;
2422 const Point centerPoint = mDevice->getCenterPoint();
2423
2424 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002425 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002426 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002427 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002428 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2429 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2430
2431 // ACTION_MOVE
2432 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002433 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002434 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2435 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2436
2437 // ACTION_UP
2438 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002439 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002440 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2441 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2442}
2443
2444TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2445 NotifyMotionArgs args;
2446 const Point centerPoint = mDevice->getCenterPoint();
2447
2448 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002449 mDevice->sendSlot(FIRST_SLOT);
2450 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002451 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002452 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002453 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2454 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2455
2456 // ACTION_POINTER_DOWN (Second slot)
2457 const Point secondPoint = centerPoint + Point(100, 100);
2458 mDevice->sendSlot(SECOND_SLOT);
2459 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002460 mDevice->sendDown(secondPoint);
2461 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002462 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002463 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002464
2465 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002466 mDevice->sendMove(secondPoint + Point(1, 1));
2467 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002468 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2469 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2470
2471 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002472 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002473 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002474 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002475 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002476
2477 // ACTION_UP
2478 mDevice->sendSlot(FIRST_SLOT);
2479 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002480 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002481 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2482 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2483}
2484
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002485/**
2486 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2487 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2488 * data?
2489 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2490 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2491 * for Pointer 0 only is generated after.
2492 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2493 * events, we will not miss any information.
2494 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2495 * event generated afterwards that contains the newest movement of pointer 0.
2496 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2497 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2498 * losing information about non-palm pointers.
2499 */
2500TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2501 NotifyMotionArgs args;
2502 const Point centerPoint = mDevice->getCenterPoint();
2503
2504 // ACTION_DOWN
2505 mDevice->sendSlot(FIRST_SLOT);
2506 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2507 mDevice->sendDown(centerPoint);
2508 mDevice->sendSync();
2509 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2510
2511 // ACTION_POINTER_DOWN (Second slot)
2512 const Point secondPoint = centerPoint + Point(100, 100);
2513 mDevice->sendSlot(SECOND_SLOT);
2514 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2515 mDevice->sendDown(secondPoint);
2516 mDevice->sendSync();
2517 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2518
2519 // ACTION_MOVE (First slot)
2520 mDevice->sendSlot(FIRST_SLOT);
2521 mDevice->sendMove(centerPoint + Point(5, 5));
2522 // ACTION_POINTER_UP (Second slot)
2523 mDevice->sendSlot(SECOND_SLOT);
2524 mDevice->sendPointerUp();
2525 // Send a single sync for the above 2 pointer updates
2526 mDevice->sendSync();
2527
2528 // First, we should get POINTER_UP for the second pointer
2529 assertReceivedMotion(ACTION_POINTER_1_UP,
2530 {/*first pointer */ centerPoint + Point(5, 5),
2531 /*second pointer*/ secondPoint});
2532
2533 // Next, the MOVE event for the first pointer
2534 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2535}
2536
2537/**
2538 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2539 * move, and then it will go up, all in the same frame.
2540 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2541 * gets sent to the listener.
2542 */
2543TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2544 NotifyMotionArgs args;
2545 const Point centerPoint = mDevice->getCenterPoint();
2546
2547 // ACTION_DOWN
2548 mDevice->sendSlot(FIRST_SLOT);
2549 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2550 mDevice->sendDown(centerPoint);
2551 mDevice->sendSync();
2552 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2553
2554 // ACTION_POINTER_DOWN (Second slot)
2555 const Point secondPoint = centerPoint + Point(100, 100);
2556 mDevice->sendSlot(SECOND_SLOT);
2557 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2558 mDevice->sendDown(secondPoint);
2559 mDevice->sendSync();
2560 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2561
2562 // ACTION_MOVE (First slot)
2563 mDevice->sendSlot(FIRST_SLOT);
2564 mDevice->sendMove(centerPoint + Point(5, 5));
2565 // ACTION_POINTER_UP (Second slot)
2566 mDevice->sendSlot(SECOND_SLOT);
2567 mDevice->sendMove(secondPoint + Point(6, 6));
2568 mDevice->sendPointerUp();
2569 // Send a single sync for the above 2 pointer updates
2570 mDevice->sendSync();
2571
2572 // First, we should get POINTER_UP for the second pointer
2573 // The movement of the second pointer during the liftoff frame is ignored.
2574 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2575 assertReceivedMotion(ACTION_POINTER_1_UP,
2576 {/*first pointer */ centerPoint + Point(5, 5),
2577 /*second pointer*/ secondPoint});
2578
2579 // Next, the MOVE event for the first pointer
2580 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2581}
2582
Arthur Hungaab25622020-01-16 11:22:11 +08002583TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2584 NotifyMotionArgs args;
2585 const Point centerPoint = mDevice->getCenterPoint();
2586
2587 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002588 mDevice->sendSlot(FIRST_SLOT);
2589 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002590 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002591 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002592 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2593 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2594
arthurhungcc7f9802020-04-30 17:55:40 +08002595 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002596 const Point secondPoint = centerPoint + Point(100, 100);
2597 mDevice->sendSlot(SECOND_SLOT);
2598 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2599 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002600 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002601 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002602 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002603
arthurhungcc7f9802020-04-30 17:55:40 +08002604 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002605 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002606 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002607 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2608 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2609
arthurhungcc7f9802020-04-30 17:55:40 +08002610 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2611 // a palm event.
2612 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002613 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002614 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002615 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002616 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002617 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002618
arthurhungcc7f9802020-04-30 17:55:40 +08002619 // Send up to second slot, expect first slot send moving.
2620 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002621 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002622 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2623 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002624
arthurhungcc7f9802020-04-30 17:55:40 +08002625 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002626 mDevice->sendSlot(FIRST_SLOT);
2627 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002628 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002629
arthurhungcc7f9802020-04-30 17:55:40 +08002630 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2631 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002632}
2633
Michael Wrightd02c5b62014-02-10 15:10:22 -08002634// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002635class InputDeviceTest : public testing::Test {
2636protected:
2637 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002638 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002639 static const int32_t DEVICE_ID;
2640 static const int32_t DEVICE_GENERATION;
2641 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002642 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002643 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002644
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002645 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002646 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002647 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002648 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002649 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002650
Chris Yea52ade12020-08-27 16:49:20 -07002651 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002652 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002653 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002654 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002655 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002656 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002657 InputDeviceIdentifier identifier;
2658 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002659 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002660 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002661 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002662 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002663 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002664 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002665 }
2666
Chris Yea52ade12020-08-27 16:49:20 -07002667 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002668 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002669 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002670 }
2671};
2672
2673const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002674const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002675const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002676const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2677const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002678const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002679 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002680const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002681
2682TEST_F(InputDeviceTest, ImmutableProperties) {
2683 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002684 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002685 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002686}
2687
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002688TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2689 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002690}
2691
Michael Wrightd02c5b62014-02-10 15:10:22 -08002692TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2693 // Configuration.
2694 InputReaderConfiguration config;
2695 mDevice->configure(ARBITRARY_TIME, &config, 0);
2696
2697 // Reset.
2698 mDevice->reset(ARBITRARY_TIME);
2699
2700 NotifyDeviceResetArgs resetArgs;
2701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2702 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2703 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2704
2705 // Metadata.
2706 ASSERT_TRUE(mDevice->isIgnored());
2707 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2708
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002709 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002710 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002711 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002712 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2713 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2714
2715 // State queries.
2716 ASSERT_EQ(0, mDevice->getMetaState());
2717
2718 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2719 << "Ignored device should return unknown key code state.";
2720 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2721 << "Ignored device should return unknown scan code state.";
2722 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2723 << "Ignored device should return unknown switch state.";
2724
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002725 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002726 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002727 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002728 << "Ignored device should never mark any key codes.";
2729 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2730 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2731}
2732
2733TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2734 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002735 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08002736
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002737 FakeInputMapper& mapper1 =
2738 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002739 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2740 mapper1.setMetaState(AMETA_ALT_ON);
2741 mapper1.addSupportedKeyCode(AKEYCODE_A);
2742 mapper1.addSupportedKeyCode(AKEYCODE_B);
2743 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2744 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2745 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2746 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2747 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002748
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002749 FakeInputMapper& mapper2 =
2750 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002751 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002752
2753 InputReaderConfiguration config;
2754 mDevice->configure(ARBITRARY_TIME, &config, 0);
2755
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002756 std::string propertyValue;
2757 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002758 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002759 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002760
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002761 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2762 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002763
2764 // Reset
2765 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002766 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2767 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002768
2769 NotifyDeviceResetArgs resetArgs;
2770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2771 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2772 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2773
2774 // Metadata.
2775 ASSERT_FALSE(mDevice->isIgnored());
2776 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2777
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002778 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002779 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002780 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002781 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2782 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2783
2784 // State queries.
2785 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2786 << "Should query mappers and combine meta states.";
2787
2788 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2789 << "Should return unknown key code state when source not supported.";
2790 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2791 << "Should return unknown scan code state when source not supported.";
2792 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2793 << "Should return unknown switch state when source not supported.";
2794
2795 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2796 << "Should query mapper when source is supported.";
2797 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2798 << "Should query mapper when source is supported.";
2799 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2800 << "Should query mapper when source is supported.";
2801
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002802 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002803 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002804 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002805 << "Should do nothing when source is unsupported.";
2806 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2807 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2808 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2809 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2810
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002811 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002812 << "Should query mapper when source is supported.";
2813 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2814 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2815 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2816 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2817
2818 // Event handling.
2819 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002820 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002821 mDevice->process(&event, 1);
2822
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002823 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2824 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002825}
2826
Arthur Hung2c9a3342019-07-23 14:18:59 +08002827// A single input device is associated with a specific display. Check that:
2828// 1. Device is disabled if the viewport corresponding to the associated display is not found
2829// 2. Device is disabled when setEnabled API is called
2830TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002831 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002832
2833 // First Configuration.
2834 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2835
2836 // Device should be enabled by default.
2837 ASSERT_TRUE(mDevice->isEnabled());
2838
2839 // Prepare associated info.
2840 constexpr uint8_t hdmi = 1;
2841 const std::string UNIQUE_ID = "local:1";
2842
2843 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2844 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2845 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2846 // Device should be disabled because it is associated with a specific display via
2847 // input port <-> display port association, but the corresponding display is not found
2848 ASSERT_FALSE(mDevice->isEnabled());
2849
2850 // Prepare displays.
2851 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002852 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2853 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002854 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2855 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2856 ASSERT_TRUE(mDevice->isEnabled());
2857
2858 // Device should be disabled after set disable.
2859 mFakePolicy->addDisabledDevice(mDevice->getId());
2860 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2861 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2862 ASSERT_FALSE(mDevice->isEnabled());
2863
2864 // Device should still be disabled even found the associated display.
2865 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2866 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2867 ASSERT_FALSE(mDevice->isEnabled());
2868}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002869
Christine Franks1ba71cc2021-04-07 14:37:42 -07002870TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2871 // Device should be enabled by default.
2872 mFakePolicy->clearViewports();
2873 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2874 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2875 ASSERT_TRUE(mDevice->isEnabled());
2876
2877 // Device should be disabled because it is associated with a specific display, but the
2878 // corresponding display is not found.
2879 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
Christine Franks2a2293c2022-01-18 11:51:16 -08002880 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002881 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2882 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2883 ASSERT_FALSE(mDevice->isEnabled());
2884
2885 // Device should be enabled when a display is found.
2886 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2887 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2888 NO_PORT, ViewportType::INTERNAL);
2889 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2890 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2891 ASSERT_TRUE(mDevice->isEnabled());
2892
2893 // Device should be disabled after set disable.
2894 mFakePolicy->addDisabledDevice(mDevice->getId());
2895 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2896 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2897 ASSERT_FALSE(mDevice->isEnabled());
2898
2899 // Device should still be disabled even found the associated display.
2900 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2901 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2902 ASSERT_FALSE(mDevice->isEnabled());
2903}
2904
Christine Franks2a2293c2022-01-18 11:51:16 -08002905TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2906 mFakePolicy->clearViewports();
2907 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2908 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2909
2910 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2911 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2912 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2913 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2914 NO_PORT, ViewportType::INTERNAL);
2915 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2916 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2917 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2918}
2919
Michael Wrightd02c5b62014-02-10 15:10:22 -08002920// --- InputMapperTest ---
2921
2922class InputMapperTest : public testing::Test {
2923protected:
2924 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002925 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002926 static const int32_t DEVICE_ID;
2927 static const int32_t DEVICE_GENERATION;
2928 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002929 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002930 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002931
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002932 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002933 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002934 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002935 std::unique_ptr<InstrumentedInputReader> mReader;
2936 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002937
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002938 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002939 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002940 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002941 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002942 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002943 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002944 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002945 }
2946
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002947 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002948 SetUp(DEVICE_CLASSES);
2949 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002950
Chris Yea52ade12020-08-27 16:49:20 -07002951 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002952 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002953 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002954 }
2955
2956 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002957 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002958 }
2959
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002960 void configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00002961 if (!changes ||
2962 (changes &
2963 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
2964 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002965 mReader->requestRefreshConfiguration(changes);
2966 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002967 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002968 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2969 }
2970
arthurhungdcef2dc2020-08-11 14:47:50 +08002971 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2972 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002973 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002974 InputDeviceIdentifier identifier;
2975 identifier.name = name;
2976 identifier.location = location;
2977 std::shared_ptr<InputDevice> device =
2978 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2979 identifier);
2980 mReader->pushNextDevice(device);
2981 mFakeEventHub->addDevice(eventHubId, name, classes);
2982 mReader->loopOnce();
2983 return device;
2984 }
2985
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002986 template <class T, typename... Args>
2987 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002988 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002989 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002990 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002991 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002992 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002993 }
2994
2995 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002996 int32_t orientation, const std::string& uniqueId,
2997 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002998 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2999 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003000 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3001 }
3002
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003003 void clearViewports() {
3004 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003005 }
3006
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003007 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
3008 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003009 RawEvent event;
3010 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003011 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003012 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003013 event.type = type;
3014 event.code = code;
3015 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003016 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08003017 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003018 }
3019
3020 static void assertMotionRange(const InputDeviceInfo& info,
3021 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3022 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003023 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003024 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3025 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3026 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3027 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3028 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3029 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3030 }
3031
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003032 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3033 float size, float touchMajor, float touchMinor, float toolMajor,
3034 float toolMinor, float orientation, float distance,
3035 float scaledAxisEpsilon = 1.f) {
3036 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3037 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003038 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3039 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003040 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3041 scaledAxisEpsilon);
3042 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3043 scaledAxisEpsilon);
3044 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3045 scaledAxisEpsilon);
3046 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3047 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003048 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3049 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3050 }
3051
Michael Wright17db18e2020-06-26 20:51:44 +01003052 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003053 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003054 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003055 ASSERT_NEAR(x, actualX, 1);
3056 ASSERT_NEAR(y, actualY, 1);
3057 }
3058};
3059
3060const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003061const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003062const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003063const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3064const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003065const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3066 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003067const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003068
3069// --- SwitchInputMapperTest ---
3070
3071class SwitchInputMapperTest : public InputMapperTest {
3072protected:
3073};
3074
3075TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003076 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003077
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003078 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003079}
3080
3081TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003082 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003083
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003084 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003085 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003087 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003088 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089}
3090
3091TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003092 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003093
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003094 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3095 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3096 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3097 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003098
3099 NotifySwitchArgs args;
3100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3101 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003102 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3103 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003104 args.switchMask);
3105 ASSERT_EQ(uint32_t(0), args.policyFlags);
3106}
3107
Chris Ye87143712020-11-10 05:05:58 +00003108// --- VibratorInputMapperTest ---
3109class VibratorInputMapperTest : public InputMapperTest {
3110protected:
3111 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3112};
3113
3114TEST_F(VibratorInputMapperTest, GetSources) {
3115 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3116
3117 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3118}
3119
3120TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3121 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3122
3123 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3124}
3125
3126TEST_F(VibratorInputMapperTest, Vibrate) {
3127 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003128 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003129 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3130
3131 VibrationElement pattern(2);
3132 VibrationSequence sequence(2);
3133 pattern.duration = std::chrono::milliseconds(200);
3134 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3135 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3136 sequence.addElement(pattern);
3137 pattern.duration = std::chrono::milliseconds(500);
3138 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3139 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3140 sequence.addElement(pattern);
3141
3142 std::vector<int64_t> timings = {0, 1};
3143 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3144
3145 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003146 // Start vibrating
3147 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003148 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003149 // Verify vibrator state listener was notified.
3150 mReader->loopOnce();
3151 NotifyVibratorStateArgs args;
3152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3153 ASSERT_EQ(DEVICE_ID, args.deviceId);
3154 ASSERT_TRUE(args.isOn);
3155 // Stop vibrating
3156 mapper.cancelVibrate(VIBRATION_TOKEN);
3157 ASSERT_FALSE(mapper.isVibrating());
3158 // Verify vibrator state listener was notified.
3159 mReader->loopOnce();
3160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3161 ASSERT_EQ(DEVICE_ID, args.deviceId);
3162 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003163}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003164
Chris Yef59a2f42020-10-16 12:55:26 -07003165// --- SensorInputMapperTest ---
3166
3167class SensorInputMapperTest : public InputMapperTest {
3168protected:
3169 static const int32_t ACCEL_RAW_MIN;
3170 static const int32_t ACCEL_RAW_MAX;
3171 static const int32_t ACCEL_RAW_FUZZ;
3172 static const int32_t ACCEL_RAW_FLAT;
3173 static const int32_t ACCEL_RAW_RESOLUTION;
3174
3175 static const int32_t GYRO_RAW_MIN;
3176 static const int32_t GYRO_RAW_MAX;
3177 static const int32_t GYRO_RAW_FUZZ;
3178 static const int32_t GYRO_RAW_FLAT;
3179 static const int32_t GYRO_RAW_RESOLUTION;
3180
3181 static const float GRAVITY_MS2_UNIT;
3182 static const float DEGREE_RADIAN_UNIT;
3183
3184 void prepareAccelAxes();
3185 void prepareGyroAxes();
3186 void setAccelProperties();
3187 void setGyroProperties();
3188 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3189};
3190
3191const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3192const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3193const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3194const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3195const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3196
3197const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3198const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3199const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3200const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3201const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3202
3203const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3204const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3205
3206void SensorInputMapperTest::prepareAccelAxes() {
3207 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3208 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3209 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3210 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3211 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3212 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3213}
3214
3215void SensorInputMapperTest::prepareGyroAxes() {
3216 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3217 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3218 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3219 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3220 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3221 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3222}
3223
3224void SensorInputMapperTest::setAccelProperties() {
3225 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3226 /* sensorDataIndex */ 0);
3227 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3228 /* sensorDataIndex */ 1);
3229 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3230 /* sensorDataIndex */ 2);
3231 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3232 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3233 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3234 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3235 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3236}
3237
3238void SensorInputMapperTest::setGyroProperties() {
3239 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3240 /* sensorDataIndex */ 0);
3241 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3242 /* sensorDataIndex */ 1);
3243 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3244 /* sensorDataIndex */ 2);
3245 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3246 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3247 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3248 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3249 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3250}
3251
3252TEST_F(SensorInputMapperTest, GetSources) {
3253 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3254
3255 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3256}
3257
3258TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3259 setAccelProperties();
3260 prepareAccelAxes();
3261 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3262
3263 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3264 std::chrono::microseconds(10000),
3265 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003266 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003267 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3268 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3269 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3270 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3271 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003272
3273 NotifySensorArgs args;
3274 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3275 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3276 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3277
3278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3279 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3280 ASSERT_EQ(args.deviceId, DEVICE_ID);
3281 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3282 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3283 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3284 ASSERT_EQ(args.values, values);
3285 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3286}
3287
3288TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3289 setGyroProperties();
3290 prepareGyroAxes();
3291 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3292
3293 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3294 std::chrono::microseconds(10000),
3295 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003296 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003297 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3298 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3299 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3300 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3301 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003302
3303 NotifySensorArgs args;
3304 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3305 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3306 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3307
3308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3309 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3310 ASSERT_EQ(args.deviceId, DEVICE_ID);
3311 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3312 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3313 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3314 ASSERT_EQ(args.values, values);
3315 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3316}
3317
Michael Wrightd02c5b62014-02-10 15:10:22 -08003318// --- KeyboardInputMapperTest ---
3319
3320class KeyboardInputMapperTest : public InputMapperTest {
3321protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003322 const std::string UNIQUE_ID = "local:0";
3323
3324 void prepareDisplay(int32_t orientation);
3325
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003326 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003327 int32_t originalKeyCode, int32_t rotatedKeyCode,
3328 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003329};
3330
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003331/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3332 * orientation.
3333 */
3334void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003335 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3336 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003337}
3338
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003339void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003340 int32_t originalScanCode, int32_t originalKeyCode,
3341 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003342 NotifyKeyArgs args;
3343
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003344 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3346 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3347 ASSERT_EQ(originalScanCode, args.scanCode);
3348 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003349 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003350
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003351 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3353 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3354 ASSERT_EQ(originalScanCode, args.scanCode);
3355 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003356 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003357}
3358
Michael Wrightd02c5b62014-02-10 15:10:22 -08003359TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003360 KeyboardInputMapper& mapper =
3361 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3362 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003363
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003364 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003365}
3366
3367TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3368 const int32_t USAGE_A = 0x070004;
3369 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003370 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3371 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003372 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3373 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3374 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003375
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003376 KeyboardInputMapper& mapper =
3377 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3378 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003379 // Initial metastate is AMETA_NONE.
3380 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003381
3382 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003383 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003384 NotifyKeyArgs args;
3385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3386 ASSERT_EQ(DEVICE_ID, args.deviceId);
3387 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3388 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3389 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3390 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3391 ASSERT_EQ(KEY_HOME, args.scanCode);
3392 ASSERT_EQ(AMETA_NONE, args.metaState);
3393 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3394 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3395 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3396
3397 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003398 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3400 ASSERT_EQ(DEVICE_ID, args.deviceId);
3401 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3402 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3403 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3404 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3405 ASSERT_EQ(KEY_HOME, args.scanCode);
3406 ASSERT_EQ(AMETA_NONE, args.metaState);
3407 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3408 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3409 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3410
3411 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003412 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3413 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3415 ASSERT_EQ(DEVICE_ID, args.deviceId);
3416 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3417 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3418 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3419 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3420 ASSERT_EQ(0, args.scanCode);
3421 ASSERT_EQ(AMETA_NONE, args.metaState);
3422 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3423 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3424 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3425
3426 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003427 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3428 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3430 ASSERT_EQ(DEVICE_ID, args.deviceId);
3431 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3432 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3433 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3434 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3435 ASSERT_EQ(0, args.scanCode);
3436 ASSERT_EQ(AMETA_NONE, args.metaState);
3437 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3438 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3439 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3440
3441 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003442 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3443 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3445 ASSERT_EQ(DEVICE_ID, args.deviceId);
3446 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3447 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3448 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3449 ASSERT_EQ(0, args.keyCode);
3450 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3451 ASSERT_EQ(AMETA_NONE, args.metaState);
3452 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3453 ASSERT_EQ(0U, args.policyFlags);
3454 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3455
3456 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3458 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3460 ASSERT_EQ(DEVICE_ID, args.deviceId);
3461 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3462 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3463 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3464 ASSERT_EQ(0, args.keyCode);
3465 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3466 ASSERT_EQ(AMETA_NONE, args.metaState);
3467 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3468 ASSERT_EQ(0U, args.policyFlags);
3469 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3470}
3471
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003472/**
3473 * Ensure that the readTime is set to the time when the EV_KEY is received.
3474 */
3475TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3476 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3477
3478 KeyboardInputMapper& mapper =
3479 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3480 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3481 NotifyKeyArgs args;
3482
3483 // Key down
3484 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3486 ASSERT_EQ(12, args.readTime);
3487
3488 // Key up
3489 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3491 ASSERT_EQ(15, args.readTime);
3492}
3493
Michael Wrightd02c5b62014-02-10 15:10:22 -08003494TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003495 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3496 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003497 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3498 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3499 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003500
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003501 KeyboardInputMapper& mapper =
3502 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3503 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003504
Arthur Hung95f68612022-04-07 14:08:22 +08003505 // Initial metastate is AMETA_NONE.
3506 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003507
3508 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003509 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003510 NotifyKeyArgs args;
3511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3512 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003513 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003514 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003515
3516 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003517 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3519 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003520 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003521
3522 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003523 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3525 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003526 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003527
3528 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003529 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3531 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003532 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003533 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003534}
3535
3536TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003537 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3538 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3539 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3540 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003541
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003542 KeyboardInputMapper& mapper =
3543 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3544 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003545
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003546 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003547 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3548 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3549 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3550 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3551 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3552 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3553 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3554 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3555}
3556
3557TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003558 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3559 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3560 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3561 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003562
Michael Wrightd02c5b62014-02-10 15:10:22 -08003563 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003564 KeyboardInputMapper& mapper =
3565 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3566 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003567
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003568 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003569 ASSERT_NO_FATAL_FAILURE(
3570 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3571 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3572 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3573 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3574 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3575 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3576 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003577
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003578 clearViewports();
3579 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003580 ASSERT_NO_FATAL_FAILURE(
3581 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3582 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3583 AKEYCODE_DPAD_UP, DISPLAY_ID));
3584 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3585 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3586 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3587 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003588
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003589 clearViewports();
3590 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003591 ASSERT_NO_FATAL_FAILURE(
3592 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3593 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3594 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3595 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3596 AKEYCODE_DPAD_UP, DISPLAY_ID));
3597 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3598 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003599
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003600 clearViewports();
3601 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003602 ASSERT_NO_FATAL_FAILURE(
3603 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3604 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3605 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3606 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3607 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3608 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3609 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003610
3611 // Special case: if orientation changes while key is down, we still emit the same keycode
3612 // in the key up as we did in the key down.
3613 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003614 clearViewports();
3615 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003616 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3618 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3619 ASSERT_EQ(KEY_UP, args.scanCode);
3620 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3621
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003622 clearViewports();
3623 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003624 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3626 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3627 ASSERT_EQ(KEY_UP, args.scanCode);
3628 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3629}
3630
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003631TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3632 // If the keyboard is not orientation aware,
3633 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003634 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003635
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003636 KeyboardInputMapper& mapper =
3637 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3638 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003639 NotifyKeyArgs args;
3640
3641 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003642 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003644 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3646 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3647
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003648 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003649 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003651 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3653 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3654}
3655
3656TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3657 // If the keyboard is orientation aware,
3658 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003659 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003660
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003661 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003662 KeyboardInputMapper& mapper =
3663 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3664 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003665 NotifyKeyArgs args;
3666
3667 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3668 // ^--- already checked by the previous test
3669
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003670 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003671 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003672 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003674 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3676 ASSERT_EQ(DISPLAY_ID, args.displayId);
3677
3678 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003679 clearViewports();
3680 setDisplayInfoAndReconfigure(newDisplayId, 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(newDisplayId, args.displayId);
3687}
3688
Michael Wrightd02c5b62014-02-10 15:10:22 -08003689TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003690 KeyboardInputMapper& mapper =
3691 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3692 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003693
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003694 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003695 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003696
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003697 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003698 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003699}
3700
Philip Junker4af3b3d2021-12-14 10:36:55 +01003701TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3702 KeyboardInputMapper& mapper =
3703 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3704 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3705
3706 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3707 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3708 << "If a mapping is available, the result is equal to the mapping";
3709
3710 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3711 << "If no mapping is available, the result is the key location";
3712}
3713
Michael Wrightd02c5b62014-02-10 15:10:22 -08003714TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003715 KeyboardInputMapper& mapper =
3716 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3717 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003718
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003719 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003720 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003721
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003722 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003723 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003724}
3725
3726TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003727 KeyboardInputMapper& mapper =
3728 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3729 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003730
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003731 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003732
Michael Wrightd02c5b62014-02-10 15:10:22 -08003733 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003734 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003735 ASSERT_TRUE(flags[0]);
3736 ASSERT_FALSE(flags[1]);
3737}
3738
3739TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003740 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3741 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3742 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3743 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3744 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3745 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003746
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003747 KeyboardInputMapper& mapper =
3748 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3749 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003750 // Initial metastate is AMETA_NONE.
3751 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003752
3753 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003754 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3755 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3756 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003757
3758 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003759 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3760 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003761 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3762 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3763 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003764 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003765
3766 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003767 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3768 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003769 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3770 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3771 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003772 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003773
3774 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003775 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3776 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003777 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3778 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3779 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003780 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003781
3782 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003783 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3784 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003785 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3786 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3787 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003788 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003789
3790 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003791 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3792 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003793 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3794 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3795 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003796 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003797
3798 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3800 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003801 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3802 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3803 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003804 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003805}
3806
Chris Yea52ade12020-08-27 16:49:20 -07003807TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3808 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3809 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3810 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3811 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3812
3813 KeyboardInputMapper& mapper =
3814 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3815 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3816
Chris Yea52ade12020-08-27 16:49:20 -07003817 // Meta state should be AMETA_NONE after reset
3818 mapper.reset(ARBITRARY_TIME);
3819 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3820 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3821 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3822 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3823
3824 NotifyKeyArgs args;
3825 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003826 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3828 ASSERT_EQ(AMETA_NONE, args.metaState);
3829 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3830 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3831 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3832
3833 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003834 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3836 ASSERT_EQ(AMETA_NONE, args.metaState);
3837 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3838 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3839 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3840}
3841
Arthur Hung2c9a3342019-07-23 14:18:59 +08003842TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3843 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003844 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3845 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3846 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3847 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003848
3849 // keyboard 2.
3850 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003851 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003852 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003853 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003854 std::shared_ptr<InputDevice> device2 =
3855 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003856 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003857
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003858 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3859 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3860 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3861 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003862
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003863 KeyboardInputMapper& mapper =
3864 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3865 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003866
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003867 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003868 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003869 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003870 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3871 device2->reset(ARBITRARY_TIME);
3872
3873 // Prepared displays and associated info.
3874 constexpr uint8_t hdmi1 = 0;
3875 constexpr uint8_t hdmi2 = 1;
3876 const std::string SECONDARY_UNIQUE_ID = "local:1";
3877
3878 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3879 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3880
3881 // No associated display viewport found, should disable the device.
3882 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3883 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3884 ASSERT_FALSE(device2->isEnabled());
3885
3886 // Prepare second display.
3887 constexpr int32_t newDisplayId = 2;
3888 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003889 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003890 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003891 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003892 // Default device will reconfigure above, need additional reconfiguration for another device.
3893 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3894 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3895
3896 // Device should be enabled after the associated display is found.
3897 ASSERT_TRUE(mDevice->isEnabled());
3898 ASSERT_TRUE(device2->isEnabled());
3899
3900 // Test pad key events
3901 ASSERT_NO_FATAL_FAILURE(
3902 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3903 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3904 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3905 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3906 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3907 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3908 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3909
3910 ASSERT_NO_FATAL_FAILURE(
3911 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3912 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3913 AKEYCODE_DPAD_RIGHT, newDisplayId));
3914 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3915 AKEYCODE_DPAD_DOWN, newDisplayId));
3916 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3917 AKEYCODE_DPAD_LEFT, newDisplayId));
3918}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003919
arthurhungc903df12020-08-11 15:08:42 +08003920TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3921 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3922 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3923 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3924 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3925 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3926 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3927
3928 KeyboardInputMapper& mapper =
3929 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3930 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003931 // Initial metastate is AMETA_NONE.
3932 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003933
3934 // Initialization should have turned all of the lights off.
3935 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3936 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3937 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3938
3939 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003940 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3941 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003942 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3943 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3944
3945 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003946 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3947 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003948 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3949 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3950
3951 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003952 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3953 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003954 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3955 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3956
3957 mFakeEventHub->removeDevice(EVENTHUB_ID);
3958 mReader->loopOnce();
3959
3960 // keyboard 2 should default toggle keys.
3961 const std::string USB2 = "USB2";
3962 const std::string DEVICE_NAME2 = "KEYBOARD2";
3963 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3964 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3965 std::shared_ptr<InputDevice> device2 =
3966 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003967 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003968 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3969 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3970 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3971 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3972 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3973 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3974
arthurhung6fe95782020-10-05 22:41:16 +08003975 KeyboardInputMapper& mapper2 =
3976 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3977 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003978 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3979 device2->reset(ARBITRARY_TIME);
3980
3981 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3982 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3983 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003984 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3985 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003986}
3987
Arthur Hungcb40a002021-08-03 14:31:01 +00003988TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3989 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3990 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3991 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3992
3993 // Suppose we have two mappers. (DPAD + KEYBOARD)
3994 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3995 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3996 KeyboardInputMapper& mapper =
3997 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3998 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003999 // Initial metastate is AMETA_NONE.
4000 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004001
4002 mReader->toggleCapsLockState(DEVICE_ID);
4003 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4004}
4005
Arthur Hungfb3cc112022-04-13 07:39:50 +00004006TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4007 // keyboard 1.
4008 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4009 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4010 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4011 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4012 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4013 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4014
4015 KeyboardInputMapper& mapper1 =
4016 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4017 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4018
4019 // keyboard 2.
4020 const std::string USB2 = "USB2";
4021 const std::string DEVICE_NAME2 = "KEYBOARD2";
4022 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4023 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4024 std::shared_ptr<InputDevice> device2 =
4025 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4026 ftl::Flags<InputDeviceClass>(0));
4027 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4028 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4029 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4030 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4031 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4032 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4033
4034 KeyboardInputMapper& mapper2 =
4035 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4036 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4037 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4038 device2->reset(ARBITRARY_TIME);
4039
Arthur Hung95f68612022-04-07 14:08:22 +08004040 // Initial metastate is AMETA_NONE.
4041 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4042 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4043
4044 // Toggle num lock on and off.
4045 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4046 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004047 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4048 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4049 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4050
4051 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4052 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4053 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4054 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4055 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4056
4057 // Toggle caps lock on and off.
4058 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4059 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4060 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4061 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4062 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4063
4064 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4065 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4066 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4067 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4068 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4069
4070 // Toggle scroll lock on and off.
4071 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4072 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4073 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4074 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4075 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4076
4077 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4078 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4079 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4080 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4081 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4082}
4083
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004084// --- KeyboardInputMapperTest_ExternalDevice ---
4085
4086class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4087protected:
Chris Yea52ade12020-08-27 16:49:20 -07004088 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004089};
4090
4091TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004092 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4093 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004094
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004095 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4096 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4097 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4098 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004099
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004100 KeyboardInputMapper& mapper =
4101 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4102 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004103
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004104 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004105 NotifyKeyArgs args;
4106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4107 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4108
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004109 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4111 ASSERT_EQ(uint32_t(0), args.policyFlags);
4112
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004113 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4115 ASSERT_EQ(uint32_t(0), args.policyFlags);
4116
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004117 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4119 ASSERT_EQ(uint32_t(0), args.policyFlags);
4120
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004121 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4123 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4124
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004125 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4127 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4128}
4129
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004130TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004131 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004132
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004133 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4134 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4135 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004136
Powei Fengd041c5d2019-05-03 17:11:33 -07004137 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004138 KeyboardInputMapper& mapper =
4139 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4140 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004141
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004142 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004143 NotifyKeyArgs args;
4144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4145 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4146
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004147 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4149 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4150
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004151 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4153 ASSERT_EQ(uint32_t(0), args.policyFlags);
4154
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004155 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4157 ASSERT_EQ(uint32_t(0), args.policyFlags);
4158
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004159 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4161 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4162
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004163 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4165 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4166}
4167
Michael Wrightd02c5b62014-02-10 15:10:22 -08004168// --- CursorInputMapperTest ---
4169
4170class CursorInputMapperTest : public InputMapperTest {
4171protected:
4172 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4173
Michael Wright17db18e2020-06-26 20:51:44 +01004174 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004175
Chris Yea52ade12020-08-27 16:49:20 -07004176 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004177 InputMapperTest::SetUp();
4178
Michael Wright17db18e2020-06-26 20:51:44 +01004179 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004180 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004181 }
4182
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004183 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4184 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004185
4186 void prepareDisplay(int32_t orientation) {
4187 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004188 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004189 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4190 orientation, uniqueId, NO_PORT, viewportType);
4191 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004192
4193 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4194 float pressure) {
4195 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4196 0.0f, 0.0f, 0.0f, EPSILON));
4197 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004198};
4199
4200const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4201
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004202void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4203 int32_t originalY, int32_t rotatedX,
4204 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004205 NotifyMotionArgs args;
4206
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004207 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4208 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4209 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4211 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004212 ASSERT_NO_FATAL_FAILURE(
4213 assertCursorPointerCoords(args.pointerCoords[0],
4214 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4215 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004216}
4217
4218TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004219 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004220 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004221
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004222 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004223}
4224
4225TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004226 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004227 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004228
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004229 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230}
4231
4232TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004233 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004234 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004235
4236 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004237 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004238
4239 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004240 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4241 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4243 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4244
4245 // When the bounds are set, then there should be a valid motion range.
4246 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4247
4248 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004249 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004250
4251 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4252 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4253 1, 800 - 1, 0.0f, 0.0f));
4254 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4255 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4256 2, 480 - 1, 0.0f, 0.0f));
4257 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4258 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4259 0.0f, 1.0f, 0.0f, 0.0f));
4260}
4261
4262TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004263 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004264 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004265
4266 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004267 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004268
4269 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4270 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4271 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4272 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4273 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4274 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4275 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4276 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4277 0.0f, 1.0f, 0.0f, 0.0f));
4278}
4279
4280TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004281 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004282 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283
arthurhungdcef2dc2020-08-11 14:47:50 +08004284 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004285
4286 NotifyMotionArgs args;
4287
4288 // Button press.
4289 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004290 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4291 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4293 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4294 ASSERT_EQ(DEVICE_ID, args.deviceId);
4295 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4296 ASSERT_EQ(uint32_t(0), args.policyFlags);
4297 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4298 ASSERT_EQ(0, args.flags);
4299 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4300 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4301 ASSERT_EQ(0, args.edgeFlags);
4302 ASSERT_EQ(uint32_t(1), args.pointerCount);
4303 ASSERT_EQ(0, args.pointerProperties[0].id);
4304 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004305 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004306 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4307 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4308 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4309
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4311 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4312 ASSERT_EQ(DEVICE_ID, args.deviceId);
4313 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4314 ASSERT_EQ(uint32_t(0), args.policyFlags);
4315 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4316 ASSERT_EQ(0, args.flags);
4317 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4318 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4319 ASSERT_EQ(0, args.edgeFlags);
4320 ASSERT_EQ(uint32_t(1), args.pointerCount);
4321 ASSERT_EQ(0, args.pointerProperties[0].id);
4322 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004323 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004324 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4325 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4326 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4327
Michael Wrightd02c5b62014-02-10 15:10:22 -08004328 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004329 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4330 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4332 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4333 ASSERT_EQ(DEVICE_ID, args.deviceId);
4334 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4335 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004336 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4337 ASSERT_EQ(0, args.flags);
4338 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4339 ASSERT_EQ(0, args.buttonState);
4340 ASSERT_EQ(0, args.edgeFlags);
4341 ASSERT_EQ(uint32_t(1), args.pointerCount);
4342 ASSERT_EQ(0, args.pointerProperties[0].id);
4343 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004344 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004345 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4346 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4347 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4348
4349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4350 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4351 ASSERT_EQ(DEVICE_ID, args.deviceId);
4352 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4353 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004354 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4355 ASSERT_EQ(0, args.flags);
4356 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4357 ASSERT_EQ(0, args.buttonState);
4358 ASSERT_EQ(0, args.edgeFlags);
4359 ASSERT_EQ(uint32_t(1), args.pointerCount);
4360 ASSERT_EQ(0, args.pointerProperties[0].id);
4361 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004362 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004363 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4364 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4365 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4366}
4367
4368TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004369 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004370 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004371
4372 NotifyMotionArgs args;
4373
4374 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004375 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4378 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004379 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4380 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4381 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004382
4383 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004384 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4385 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4387 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004388 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4389 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004390}
4391
4392TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004393 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004394 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004395
4396 NotifyMotionArgs args;
4397
4398 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004399 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4400 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4402 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004403 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004404
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4406 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004407 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004408
Michael Wrightd02c5b62014-02-10 15:10:22 -08004409 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004410 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4411 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004413 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004414 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004415
4416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004417 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004418 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004419}
4420
4421TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004422 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004423 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004424
4425 NotifyMotionArgs args;
4426
4427 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004428 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4429 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4430 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4431 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4433 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004434 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4435 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4436 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004437
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4439 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004440 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4441 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4442 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004443
Michael Wrightd02c5b62014-02-10 15:10:22 -08004444 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004445 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4447 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4449 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004450 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4451 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4452 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004453
4454 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004455 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4456 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004458 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004459 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004460
4461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004462 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004463 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464}
4465
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004466TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004467 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004468 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4469 // need to be rotated.
4470 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004471 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004472
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004473 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004474 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4475 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4476 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4477 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4478 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4479 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4480 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4481 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4482}
4483
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004484TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004485 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004486 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4487 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004488 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004489
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004490 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004491 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4492 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4493 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4494 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4495 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4496 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4497 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4498 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4499
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004500 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004501 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4502 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4503 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4504 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4505 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4506 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4507 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4508 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004509
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004510 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004511 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4512 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4513 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4514 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4515 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4516 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4517 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4518 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4519
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004520 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004521 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4522 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4523 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4524 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4525 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4526 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4527 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4528 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004529}
4530
4531TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004532 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004533 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004534
4535 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4536 mFakePointerController->setPosition(100, 200);
4537 mFakePointerController->setButtonState(0);
4538
4539 NotifyMotionArgs motionArgs;
4540 NotifyKeyArgs keyArgs;
4541
4542 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004543 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4544 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4546 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4547 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4548 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004549 ASSERT_NO_FATAL_FAILURE(
4550 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004551
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4553 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4554 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4555 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004556 ASSERT_NO_FATAL_FAILURE(
4557 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004558
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004559 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4560 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004562 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004563 ASSERT_EQ(0, motionArgs.buttonState);
4564 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004565 ASSERT_NO_FATAL_FAILURE(
4566 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004567
4568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004569 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004570 ASSERT_EQ(0, motionArgs.buttonState);
4571 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004572 ASSERT_NO_FATAL_FAILURE(
4573 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004574
4575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004576 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004577 ASSERT_EQ(0, motionArgs.buttonState);
4578 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004579 ASSERT_NO_FATAL_FAILURE(
4580 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004581
4582 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004583 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4584 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4585 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4587 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4588 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4589 motionArgs.buttonState);
4590 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4591 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004592 ASSERT_NO_FATAL_FAILURE(
4593 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004594
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4596 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4597 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4598 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4599 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004600 ASSERT_NO_FATAL_FAILURE(
4601 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004602
4603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4604 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4605 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4606 motionArgs.buttonState);
4607 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4608 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004609 ASSERT_NO_FATAL_FAILURE(
4610 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004611
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004612 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4613 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004615 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004616 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4617 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004618 ASSERT_NO_FATAL_FAILURE(
4619 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004620
4621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004622 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004623 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4624 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004625 ASSERT_NO_FATAL_FAILURE(
4626 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004627
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004628 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4629 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004631 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4632 ASSERT_EQ(0, motionArgs.buttonState);
4633 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004634 ASSERT_NO_FATAL_FAILURE(
4635 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004636 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4637 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004638
4639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004640 ASSERT_EQ(0, motionArgs.buttonState);
4641 ASSERT_EQ(0, mFakePointerController->getButtonState());
4642 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004643 ASSERT_NO_FATAL_FAILURE(
4644 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004645
Michael Wrightd02c5b62014-02-10 15:10:22 -08004646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4647 ASSERT_EQ(0, motionArgs.buttonState);
4648 ASSERT_EQ(0, mFakePointerController->getButtonState());
4649 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004650 ASSERT_NO_FATAL_FAILURE(
4651 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004652
4653 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004654 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4655 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4657 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4658 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004659
Michael Wrightd02c5b62014-02-10 15:10:22 -08004660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004661 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004662 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4663 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004664 ASSERT_NO_FATAL_FAILURE(
4665 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004666
4667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4668 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4669 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4670 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004671 ASSERT_NO_FATAL_FAILURE(
4672 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004673
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004674 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4675 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004677 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004678 ASSERT_EQ(0, motionArgs.buttonState);
4679 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004680 ASSERT_NO_FATAL_FAILURE(
4681 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004682
4683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004684 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004685 ASSERT_EQ(0, motionArgs.buttonState);
4686 ASSERT_EQ(0, mFakePointerController->getButtonState());
4687
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004688 ASSERT_NO_FATAL_FAILURE(
4689 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4691 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4692 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4693
4694 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004695 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4696 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4698 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4699 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004700
Michael Wrightd02c5b62014-02-10 15:10:22 -08004701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004702 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004703 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4704 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004705 ASSERT_NO_FATAL_FAILURE(
4706 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004707
4708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4709 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4710 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4711 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004712 ASSERT_NO_FATAL_FAILURE(
4713 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004714
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004715 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4716 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004718 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004719 ASSERT_EQ(0, motionArgs.buttonState);
4720 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004721 ASSERT_NO_FATAL_FAILURE(
4722 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004723
4724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4725 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4726 ASSERT_EQ(0, motionArgs.buttonState);
4727 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004728 ASSERT_NO_FATAL_FAILURE(
4729 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004730
Michael Wrightd02c5b62014-02-10 15:10:22 -08004731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4732 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4733 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4734
4735 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004736 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4737 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4739 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4740 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004741
Michael Wrightd02c5b62014-02-10 15:10:22 -08004742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004743 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004744 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4745 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004746 ASSERT_NO_FATAL_FAILURE(
4747 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004748
4749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4750 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4751 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4752 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004753 ASSERT_NO_FATAL_FAILURE(
4754 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004755
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004756 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4757 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004759 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004760 ASSERT_EQ(0, motionArgs.buttonState);
4761 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004762 ASSERT_NO_FATAL_FAILURE(
4763 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004764
4765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4766 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4767 ASSERT_EQ(0, motionArgs.buttonState);
4768 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004769 ASSERT_NO_FATAL_FAILURE(
4770 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004771
Michael Wrightd02c5b62014-02-10 15:10:22 -08004772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4773 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4774 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4775
4776 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004777 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4778 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4780 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4781 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004782
Michael Wrightd02c5b62014-02-10 15:10:22 -08004783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004784 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004785 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4786 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004787 ASSERT_NO_FATAL_FAILURE(
4788 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004789
4790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4791 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4792 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4793 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004794 ASSERT_NO_FATAL_FAILURE(
4795 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004796
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004797 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4798 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004800 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004801 ASSERT_EQ(0, motionArgs.buttonState);
4802 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004803 ASSERT_NO_FATAL_FAILURE(
4804 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004805
4806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4807 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4808 ASSERT_EQ(0, motionArgs.buttonState);
4809 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004810 ASSERT_NO_FATAL_FAILURE(
4811 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004812
Michael Wrightd02c5b62014-02-10 15:10:22 -08004813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4814 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4815 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4816}
4817
4818TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004819 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004820 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004821
4822 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4823 mFakePointerController->setPosition(100, 200);
4824 mFakePointerController->setButtonState(0);
4825
4826 NotifyMotionArgs args;
4827
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004828 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004832 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4833 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4834 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4835 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 +01004836 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004837}
4838
4839TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004840 addConfigurationProperty("cursor.mode", "pointer");
4841 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004842 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004843
4844 NotifyDeviceResetArgs resetArgs;
4845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4846 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4847 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4848
4849 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4850 mFakePointerController->setPosition(100, 200);
4851 mFakePointerController->setButtonState(0);
4852
4853 NotifyMotionArgs args;
4854
4855 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004856 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4857 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4858 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4860 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4861 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4862 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4863 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 +01004864 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004865
4866 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004867 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
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_DOWN, args.action);
4872 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4873 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4875 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4876 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4877 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4878 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4879
4880 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004881 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4882 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4884 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4885 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4886 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4887 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4889 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4890 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4891 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4892 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4893
4894 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004895 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4896 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4897 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4899 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4900 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4901 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4902 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 +01004903 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004904
4905 // Disable pointer capture and check that the device generation got bumped
4906 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004907 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004908 mFakePolicy->setPointerCapture(false);
4909 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004910 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004911
4912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4913 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4914 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4915
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004916 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4917 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4918 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4920 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004921 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4922 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4923 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 +01004924 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004925}
4926
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004927/**
4928 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4929 * pointer acceleration or speed processing should not be applied.
4930 */
4931TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4932 addConfigurationProperty("cursor.mode", "pointer");
4933 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4934 100.f /*high threshold*/, 10.f /*acceleration*/);
4935 mFakePolicy->setVelocityControlParams(testParams);
4936 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4937
4938 NotifyDeviceResetArgs resetArgs;
4939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4940 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4941 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4942
4943 NotifyMotionArgs args;
4944
4945 // Move and verify scale is applied.
4946 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4947 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4948 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4950 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4951 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4952 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4953 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4954 ASSERT_GT(relX, 10);
4955 ASSERT_GT(relY, 20);
4956
4957 // Enable Pointer Capture
4958 mFakePolicy->setPointerCapture(true);
4959 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4960 NotifyPointerCaptureChangedArgs captureArgs;
4961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4962 ASSERT_TRUE(captureArgs.request.enable);
4963
4964 // Move and verify scale is not applied.
4965 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4966 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4967 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4969 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4970 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4971 ASSERT_EQ(10, args.pointerCoords[0].getX());
4972 ASSERT_EQ(20, args.pointerCoords[0].getY());
4973}
4974
Prabir Pradhan208360b2022-06-24 18:37:04 +00004975TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4976 addConfigurationProperty("cursor.mode", "pointer");
4977 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4978
4979 NotifyDeviceResetArgs resetArgs;
4980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4981 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4982 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4983
4984 // Ensure the display is rotated.
4985 prepareDisplay(DISPLAY_ORIENTATION_90);
4986
4987 NotifyMotionArgs args;
4988
4989 // Verify that the coordinates are rotated.
4990 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4991 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4992 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4994 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4995 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4996 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4997 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4998
4999 // Enable Pointer Capture.
5000 mFakePolicy->setPointerCapture(true);
5001 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5002 NotifyPointerCaptureChangedArgs captureArgs;
5003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5004 ASSERT_TRUE(captureArgs.request.enable);
5005
5006 // Move and verify rotation is not applied.
5007 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5008 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5009 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5011 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5012 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5013 ASSERT_EQ(10, args.pointerCoords[0].getX());
5014 ASSERT_EQ(20, args.pointerCoords[0].getY());
5015}
5016
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005017TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005018 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005019
Garfield Tan888a6a42020-01-09 11:39:16 -08005020 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005021 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08005022 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
5023 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00005024 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
5025 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08005026 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
5027 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5028
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005029 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5030 mFakePointerController->setPosition(100, 200);
5031 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005032
5033 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005034 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5035 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5036 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5038 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5039 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5040 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5041 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 +01005042 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005043 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
5044}
5045
Michael Wrightd02c5b62014-02-10 15:10:22 -08005046// --- TouchInputMapperTest ---
5047
5048class TouchInputMapperTest : public InputMapperTest {
5049protected:
5050 static const int32_t RAW_X_MIN;
5051 static const int32_t RAW_X_MAX;
5052 static const int32_t RAW_Y_MIN;
5053 static const int32_t RAW_Y_MAX;
5054 static const int32_t RAW_TOUCH_MIN;
5055 static const int32_t RAW_TOUCH_MAX;
5056 static const int32_t RAW_TOOL_MIN;
5057 static const int32_t RAW_TOOL_MAX;
5058 static const int32_t RAW_PRESSURE_MIN;
5059 static const int32_t RAW_PRESSURE_MAX;
5060 static const int32_t RAW_ORIENTATION_MIN;
5061 static const int32_t RAW_ORIENTATION_MAX;
5062 static const int32_t RAW_DISTANCE_MIN;
5063 static const int32_t RAW_DISTANCE_MAX;
5064 static const int32_t RAW_TILT_MIN;
5065 static const int32_t RAW_TILT_MAX;
5066 static const int32_t RAW_ID_MIN;
5067 static const int32_t RAW_ID_MAX;
5068 static const int32_t RAW_SLOT_MIN;
5069 static const int32_t RAW_SLOT_MAX;
5070 static const float X_PRECISION;
5071 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005072 static const float X_PRECISION_VIRTUAL;
5073 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005074
5075 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005076 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005077
5078 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5079
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005080 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005081 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005082
Michael Wrightd02c5b62014-02-10 15:10:22 -08005083 enum Axes {
5084 POSITION = 1 << 0,
5085 TOUCH = 1 << 1,
5086 TOOL = 1 << 2,
5087 PRESSURE = 1 << 3,
5088 ORIENTATION = 1 << 4,
5089 MINOR = 1 << 5,
5090 ID = 1 << 6,
5091 DISTANCE = 1 << 7,
5092 TILT = 1 << 8,
5093 SLOT = 1 << 9,
5094 TOOL_TYPE = 1 << 10,
5095 };
5096
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005097 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5098 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005099 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005100 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005101 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005102 int32_t toRawX(float displayX);
5103 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005104 int32_t toRotatedRawX(float displayX);
5105 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005106 float toCookedX(float rawX, float rawY);
5107 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005108 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005109 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005110 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005111 float toDisplayY(int32_t rawY, int32_t displayHeight);
5112
Michael Wrightd02c5b62014-02-10 15:10:22 -08005113};
5114
5115const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5116const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5117const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5118const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5119const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5120const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5121const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5122const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005123const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5124const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005125const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5126const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5127const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5128const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5129const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5130const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5131const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5132const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5133const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5134const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5135const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5136const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005137const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5138 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5139const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5140 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005141const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5142 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005143
5144const float TouchInputMapperTest::GEOMETRIC_SCALE =
5145 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5146 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5147
5148const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5149 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5150 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5151};
5152
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005153void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005154 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5155 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005156}
5157
5158void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5159 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5160 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005161}
5162
Santos Cordonfa5cf462017-04-05 10:37:00 -07005163void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005164 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5165 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5166 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005167}
5168
Michael Wrightd02c5b62014-02-10 15:10:22 -08005169void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005170 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5171 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5172 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5173 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005174}
5175
Jason Gerecke489fda82012-09-07 17:19:40 -07005176void TouchInputMapperTest::prepareLocationCalibration() {
5177 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5178}
5179
Michael Wrightd02c5b62014-02-10 15:10:22 -08005180int32_t TouchInputMapperTest::toRawX(float displayX) {
5181 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5182}
5183
5184int32_t TouchInputMapperTest::toRawY(float displayY) {
5185 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5186}
5187
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005188int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5189 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5190}
5191
5192int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5193 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5194}
5195
Jason Gerecke489fda82012-09-07 17:19:40 -07005196float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5197 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5198 return rawX;
5199}
5200
5201float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5202 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5203 return rawY;
5204}
5205
Michael Wrightd02c5b62014-02-10 15:10:22 -08005206float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005207 return toDisplayX(rawX, DISPLAY_WIDTH);
5208}
5209
5210float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5211 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005212}
5213
5214float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005215 return toDisplayY(rawY, DISPLAY_HEIGHT);
5216}
5217
5218float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5219 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005220}
5221
5222
5223// --- SingleTouchInputMapperTest ---
5224
5225class SingleTouchInputMapperTest : public TouchInputMapperTest {
5226protected:
5227 void prepareButtons();
5228 void prepareAxes(int axes);
5229
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005230 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5231 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5232 void processUp(SingleTouchInputMapper& mappery);
5233 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5234 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5235 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5236 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5237 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5238 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005239};
5240
5241void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005242 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005243}
5244
5245void SingleTouchInputMapperTest::prepareAxes(int axes) {
5246 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005247 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5248 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005249 }
5250 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005251 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5252 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005253 }
5254 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005255 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5256 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005257 }
5258 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005259 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5260 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005261 }
5262 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005263 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5264 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005265 }
5266}
5267
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005268void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005269 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5270 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5271 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005272}
5273
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005274void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005275 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5276 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005277}
5278
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005279void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005280 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005281}
5282
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005283void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005284 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005285}
5286
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005287void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5288 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005289 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005290}
5291
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005292void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005293 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005294}
5295
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005296void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5297 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005298 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5299 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005300}
5301
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005302void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5303 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005304 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005305}
5306
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005307void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005308 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005309}
5310
Michael Wrightd02c5b62014-02-10 15:10:22 -08005311TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005312 prepareButtons();
5313 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005314 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005315
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005316 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005317}
5318
5319TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005320 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
5321 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005322 prepareButtons();
5323 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005324 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005325
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005326 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005327}
5328
5329TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005330 prepareButtons();
5331 prepareAxes(POSITION);
5332 addConfigurationProperty("touch.deviceType", "touchPad");
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_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005339 prepareButtons();
5340 prepareAxes(POSITION);
5341 addConfigurationProperty("touch.deviceType", "touchScreen");
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_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005345}
5346
5347TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005348 addConfigurationProperty("touch.deviceType", "touchScreen");
5349 prepareDisplay(DISPLAY_ORIENTATION_0);
5350 prepareButtons();
5351 prepareAxes(POSITION);
5352 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005353 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005354
5355 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005356 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005357
5358 // Virtual key is down.
5359 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5360 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5361 processDown(mapper, x, y);
5362 processSync(mapper);
5363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5364
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005365 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005366
5367 // Virtual key is up.
5368 processUp(mapper);
5369 processSync(mapper);
5370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5371
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005372 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005373}
5374
5375TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005376 addConfigurationProperty("touch.deviceType", "touchScreen");
5377 prepareDisplay(DISPLAY_ORIENTATION_0);
5378 prepareButtons();
5379 prepareAxes(POSITION);
5380 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005381 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005382
5383 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005384 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005385
5386 // Virtual key is down.
5387 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5388 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5389 processDown(mapper, x, y);
5390 processSync(mapper);
5391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5392
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005393 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005394
5395 // Virtual key is up.
5396 processUp(mapper);
5397 processSync(mapper);
5398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5399
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005400 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005401}
5402
5403TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005404 addConfigurationProperty("touch.deviceType", "touchScreen");
5405 prepareDisplay(DISPLAY_ORIENTATION_0);
5406 prepareButtons();
5407 prepareAxes(POSITION);
5408 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005409 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005410
Michael Wrightd02c5b62014-02-10 15:10:22 -08005411 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005412 ASSERT_TRUE(
5413 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005414 ASSERT_TRUE(flags[0]);
5415 ASSERT_FALSE(flags[1]);
5416}
5417
5418TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005419 addConfigurationProperty("touch.deviceType", "touchScreen");
5420 prepareDisplay(DISPLAY_ORIENTATION_0);
5421 prepareButtons();
5422 prepareAxes(POSITION);
5423 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005424 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005425
arthurhungdcef2dc2020-08-11 14:47:50 +08005426 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005427
5428 NotifyKeyArgs args;
5429
5430 // Press virtual key.
5431 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5432 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5433 processDown(mapper, x, y);
5434 processSync(mapper);
5435
5436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5437 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5438 ASSERT_EQ(DEVICE_ID, args.deviceId);
5439 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5440 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5441 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5442 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5443 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5444 ASSERT_EQ(KEY_HOME, args.scanCode);
5445 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5446 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5447
5448 // Release virtual key.
5449 processUp(mapper);
5450 processSync(mapper);
5451
5452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5453 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5454 ASSERT_EQ(DEVICE_ID, args.deviceId);
5455 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5456 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5457 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5458 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5459 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5460 ASSERT_EQ(KEY_HOME, args.scanCode);
5461 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5462 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5463
5464 // Should not have sent any motions.
5465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5466}
5467
5468TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005469 addConfigurationProperty("touch.deviceType", "touchScreen");
5470 prepareDisplay(DISPLAY_ORIENTATION_0);
5471 prepareButtons();
5472 prepareAxes(POSITION);
5473 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005474 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005475
arthurhungdcef2dc2020-08-11 14:47:50 +08005476 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005477
5478 NotifyKeyArgs keyArgs;
5479
5480 // Press virtual key.
5481 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5482 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5483 processDown(mapper, x, y);
5484 processSync(mapper);
5485
5486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5487 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5488 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5489 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5490 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5491 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5492 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5493 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5494 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5495 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5496 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5497
5498 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5499 // into the display area.
5500 y -= 100;
5501 processMove(mapper, x, y);
5502 processSync(mapper);
5503
5504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5505 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5506 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5507 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5508 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5509 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5510 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5511 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5512 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5513 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5514 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5515 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5516
5517 NotifyMotionArgs motionArgs;
5518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5519 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5520 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5521 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5522 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5523 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5524 ASSERT_EQ(0, motionArgs.flags);
5525 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5526 ASSERT_EQ(0, motionArgs.buttonState);
5527 ASSERT_EQ(0, motionArgs.edgeFlags);
5528 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5529 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5530 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5531 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5532 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5533 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5534 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5535 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5536
5537 // Keep moving out of bounds. Should generate a pointer move.
5538 y -= 50;
5539 processMove(mapper, x, y);
5540 processSync(mapper);
5541
5542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5543 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5544 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5545 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5546 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5547 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5548 ASSERT_EQ(0, motionArgs.flags);
5549 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5550 ASSERT_EQ(0, motionArgs.buttonState);
5551 ASSERT_EQ(0, motionArgs.edgeFlags);
5552 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5553 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5554 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5556 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5557 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5558 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5559 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5560
5561 // Release out of bounds. Should generate a pointer up.
5562 processUp(mapper);
5563 processSync(mapper);
5564
5565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5566 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5567 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5568 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5569 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5570 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5571 ASSERT_EQ(0, motionArgs.flags);
5572 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5573 ASSERT_EQ(0, motionArgs.buttonState);
5574 ASSERT_EQ(0, motionArgs.edgeFlags);
5575 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5576 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5577 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5579 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5580 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5581 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5582 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5583
5584 // Should not have sent any more keys or motions.
5585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5587}
5588
5589TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005590 addConfigurationProperty("touch.deviceType", "touchScreen");
5591 prepareDisplay(DISPLAY_ORIENTATION_0);
5592 prepareButtons();
5593 prepareAxes(POSITION);
5594 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005595 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005596
arthurhungdcef2dc2020-08-11 14:47:50 +08005597 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005598
5599 NotifyMotionArgs motionArgs;
5600
5601 // Initially go down out of bounds.
5602 int32_t x = -10;
5603 int32_t y = -10;
5604 processDown(mapper, x, y);
5605 processSync(mapper);
5606
5607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5608
5609 // Move into the display area. Should generate a pointer down.
5610 x = 50;
5611 y = 75;
5612 processMove(mapper, x, y);
5613 processSync(mapper);
5614
5615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5616 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5617 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5618 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5619 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5620 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5621 ASSERT_EQ(0, motionArgs.flags);
5622 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5623 ASSERT_EQ(0, motionArgs.buttonState);
5624 ASSERT_EQ(0, motionArgs.edgeFlags);
5625 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5626 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5627 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5628 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5629 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5630 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5631 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5632 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5633
5634 // Release. Should generate a pointer up.
5635 processUp(mapper);
5636 processSync(mapper);
5637
5638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5639 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5640 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5641 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5642 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5643 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5644 ASSERT_EQ(0, motionArgs.flags);
5645 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5646 ASSERT_EQ(0, motionArgs.buttonState);
5647 ASSERT_EQ(0, motionArgs.edgeFlags);
5648 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5649 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5650 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5651 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5652 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5653 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5654 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5655 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5656
5657 // Should not have sent any more keys or motions.
5658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5660}
5661
Santos Cordonfa5cf462017-04-05 10:37:00 -07005662TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005663 addConfigurationProperty("touch.deviceType", "touchScreen");
5664 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5665
5666 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5667 prepareButtons();
5668 prepareAxes(POSITION);
5669 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005670 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005671
arthurhungdcef2dc2020-08-11 14:47:50 +08005672 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005673
5674 NotifyMotionArgs motionArgs;
5675
5676 // Down.
5677 int32_t x = 100;
5678 int32_t y = 125;
5679 processDown(mapper, x, y);
5680 processSync(mapper);
5681
5682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5683 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5684 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5685 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5686 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5687 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5688 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5689 ASSERT_EQ(0, motionArgs.flags);
5690 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5691 ASSERT_EQ(0, motionArgs.buttonState);
5692 ASSERT_EQ(0, motionArgs.edgeFlags);
5693 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5694 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5695 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5697 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5698 1, 0, 0, 0, 0, 0, 0, 0));
5699 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5700 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5701 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5702
5703 // Move.
5704 x += 50;
5705 y += 75;
5706 processMove(mapper, x, y);
5707 processSync(mapper);
5708
5709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5710 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5711 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5712 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5713 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5714 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5715 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5716 ASSERT_EQ(0, motionArgs.flags);
5717 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5718 ASSERT_EQ(0, motionArgs.buttonState);
5719 ASSERT_EQ(0, motionArgs.edgeFlags);
5720 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5721 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5722 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5723 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5724 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5725 1, 0, 0, 0, 0, 0, 0, 0));
5726 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5727 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5728 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5729
5730 // Up.
5731 processUp(mapper);
5732 processSync(mapper);
5733
5734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5735 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5736 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5737 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5738 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5739 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5740 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5741 ASSERT_EQ(0, motionArgs.flags);
5742 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5743 ASSERT_EQ(0, motionArgs.buttonState);
5744 ASSERT_EQ(0, motionArgs.edgeFlags);
5745 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5746 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5747 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5748 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5749 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5750 1, 0, 0, 0, 0, 0, 0, 0));
5751 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5752 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5753 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5754
5755 // Should not have sent any more keys or motions.
5756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5758}
5759
Michael Wrightd02c5b62014-02-10 15:10:22 -08005760TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005761 addConfigurationProperty("touch.deviceType", "touchScreen");
5762 prepareDisplay(DISPLAY_ORIENTATION_0);
5763 prepareButtons();
5764 prepareAxes(POSITION);
5765 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005766 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005767
arthurhungdcef2dc2020-08-11 14:47:50 +08005768 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005769
5770 NotifyMotionArgs motionArgs;
5771
5772 // Down.
5773 int32_t x = 100;
5774 int32_t y = 125;
5775 processDown(mapper, x, y);
5776 processSync(mapper);
5777
5778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5779 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5780 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5781 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5782 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5783 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5784 ASSERT_EQ(0, motionArgs.flags);
5785 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5786 ASSERT_EQ(0, motionArgs.buttonState);
5787 ASSERT_EQ(0, motionArgs.edgeFlags);
5788 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5789 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5790 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5791 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5792 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5793 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5794 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5795 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5796
5797 // Move.
5798 x += 50;
5799 y += 75;
5800 processMove(mapper, x, y);
5801 processSync(mapper);
5802
5803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5804 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5805 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5806 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5807 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5808 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5809 ASSERT_EQ(0, motionArgs.flags);
5810 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5811 ASSERT_EQ(0, motionArgs.buttonState);
5812 ASSERT_EQ(0, motionArgs.edgeFlags);
5813 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5814 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5815 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5816 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5817 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5818 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5819 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5820 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5821
5822 // Up.
5823 processUp(mapper);
5824 processSync(mapper);
5825
5826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5827 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5828 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5829 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5830 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5831 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5832 ASSERT_EQ(0, motionArgs.flags);
5833 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5834 ASSERT_EQ(0, motionArgs.buttonState);
5835 ASSERT_EQ(0, motionArgs.edgeFlags);
5836 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5837 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5838 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5839 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5840 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5841 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5842 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5843 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5844
5845 // Should not have sent any more keys or motions.
5846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5848}
5849
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005850TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005851 addConfigurationProperty("touch.deviceType", "touchScreen");
5852 prepareButtons();
5853 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005854 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5855 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005856 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005857
5858 NotifyMotionArgs args;
5859
5860 // Rotation 90.
5861 prepareDisplay(DISPLAY_ORIENTATION_90);
5862 processDown(mapper, toRawX(50), toRawY(75));
5863 processSync(mapper);
5864
5865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5866 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5867 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5868
5869 processUp(mapper);
5870 processSync(mapper);
5871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5872}
5873
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005874TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005875 addConfigurationProperty("touch.deviceType", "touchScreen");
5876 prepareButtons();
5877 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005878 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5879 // orientation-aware are affected by display rotation.
5880 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005881 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005882
5883 NotifyMotionArgs args;
5884
5885 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005886 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005887 prepareDisplay(DISPLAY_ORIENTATION_0);
5888 processDown(mapper, toRawX(50), toRawY(75));
5889 processSync(mapper);
5890
5891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5892 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5893 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5894
5895 processUp(mapper);
5896 processSync(mapper);
5897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5898
5899 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005900 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005901 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005902 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005903 processSync(mapper);
5904
5905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5906 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5907 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5908
5909 processUp(mapper);
5910 processSync(mapper);
5911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5912
5913 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005914 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005915 prepareDisplay(DISPLAY_ORIENTATION_180);
5916 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5917 processSync(mapper);
5918
5919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5920 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5921 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5922
5923 processUp(mapper);
5924 processSync(mapper);
5925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5926
5927 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005928 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005929 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005930 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005931 processSync(mapper);
5932
5933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5934 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5935 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5936
5937 processUp(mapper);
5938 processSync(mapper);
5939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5940}
5941
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005942TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5943 addConfigurationProperty("touch.deviceType", "touchScreen");
5944 prepareButtons();
5945 prepareAxes(POSITION);
5946 addConfigurationProperty("touch.orientationAware", "1");
5947 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5948 clearViewports();
5949 prepareDisplay(DISPLAY_ORIENTATION_0);
5950 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5951 NotifyMotionArgs args;
5952
5953 // Orientation 0.
5954 processDown(mapper, toRawX(50), toRawY(75));
5955 processSync(mapper);
5956
5957 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5958 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5959 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5960
5961 processUp(mapper);
5962 processSync(mapper);
5963 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5964}
5965
5966TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5967 addConfigurationProperty("touch.deviceType", "touchScreen");
5968 prepareButtons();
5969 prepareAxes(POSITION);
5970 addConfigurationProperty("touch.orientationAware", "1");
5971 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5972 clearViewports();
5973 prepareDisplay(DISPLAY_ORIENTATION_0);
5974 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5975 NotifyMotionArgs args;
5976
5977 // Orientation 90.
5978 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5979 processSync(mapper);
5980
5981 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5982 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5983 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5984
5985 processUp(mapper);
5986 processSync(mapper);
5987 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5988}
5989
5990TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5991 addConfigurationProperty("touch.deviceType", "touchScreen");
5992 prepareButtons();
5993 prepareAxes(POSITION);
5994 addConfigurationProperty("touch.orientationAware", "1");
5995 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5996 clearViewports();
5997 prepareDisplay(DISPLAY_ORIENTATION_0);
5998 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5999 NotifyMotionArgs args;
6000
6001 // Orientation 180.
6002 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6003 processSync(mapper);
6004
6005 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6006 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6007 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6008
6009 processUp(mapper);
6010 processSync(mapper);
6011 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6012}
6013
6014TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6015 addConfigurationProperty("touch.deviceType", "touchScreen");
6016 prepareButtons();
6017 prepareAxes(POSITION);
6018 addConfigurationProperty("touch.orientationAware", "1");
6019 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6020 clearViewports();
6021 prepareDisplay(DISPLAY_ORIENTATION_0);
6022 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6023 NotifyMotionArgs args;
6024
6025 // Orientation 270.
6026 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6027 processSync(mapper);
6028
6029 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6030 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6031 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6032
6033 processUp(mapper);
6034 processSync(mapper);
6035 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6036}
6037
6038TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6039 addConfigurationProperty("touch.deviceType", "touchScreen");
6040 prepareButtons();
6041 prepareAxes(POSITION);
6042 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6043 // orientation-aware are affected by display rotation.
6044 addConfigurationProperty("touch.orientationAware", "0");
6045 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6046 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6047
6048 NotifyMotionArgs args;
6049
6050 // Orientation 90, Rotation 0.
6051 clearViewports();
6052 prepareDisplay(DISPLAY_ORIENTATION_0);
6053 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6054 processSync(mapper);
6055
6056 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6057 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6058 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6059
6060 processUp(mapper);
6061 processSync(mapper);
6062 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6063
6064 // Orientation 90, Rotation 90.
6065 clearViewports();
6066 prepareDisplay(DISPLAY_ORIENTATION_90);
6067 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6068 processSync(mapper);
6069
6070 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6071 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6072 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6073
6074 processUp(mapper);
6075 processSync(mapper);
6076 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6077
6078 // Orientation 90, Rotation 180.
6079 clearViewports();
6080 prepareDisplay(DISPLAY_ORIENTATION_180);
6081 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6082 processSync(mapper);
6083
6084 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6085 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6086 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6087
6088 processUp(mapper);
6089 processSync(mapper);
6090 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6091
6092 // Orientation 90, Rotation 270.
6093 clearViewports();
6094 prepareDisplay(DISPLAY_ORIENTATION_270);
6095 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6096 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6097 processSync(mapper);
6098
6099 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6100 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6101 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6102
6103 processUp(mapper);
6104 processSync(mapper);
6105 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6106}
6107
Michael Wrightd02c5b62014-02-10 15:10:22 -08006108TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006109 addConfigurationProperty("touch.deviceType", "touchScreen");
6110 prepareDisplay(DISPLAY_ORIENTATION_0);
6111 prepareButtons();
6112 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006113 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006114
6115 // These calculations are based on the input device calibration documentation.
6116 int32_t rawX = 100;
6117 int32_t rawY = 200;
6118 int32_t rawPressure = 10;
6119 int32_t rawToolMajor = 12;
6120 int32_t rawDistance = 2;
6121 int32_t rawTiltX = 30;
6122 int32_t rawTiltY = 110;
6123
6124 float x = toDisplayX(rawX);
6125 float y = toDisplayY(rawY);
6126 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6127 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6128 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6129 float distance = float(rawDistance);
6130
6131 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6132 float tiltScale = M_PI / 180;
6133 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6134 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6135 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6136 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6137
6138 processDown(mapper, rawX, rawY);
6139 processPressure(mapper, rawPressure);
6140 processToolMajor(mapper, rawToolMajor);
6141 processDistance(mapper, rawDistance);
6142 processTilt(mapper, rawTiltX, rawTiltY);
6143 processSync(mapper);
6144
6145 NotifyMotionArgs args;
6146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6147 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6148 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6149 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6150}
6151
Jason Gerecke489fda82012-09-07 17:19:40 -07006152TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006153 addConfigurationProperty("touch.deviceType", "touchScreen");
6154 prepareDisplay(DISPLAY_ORIENTATION_0);
6155 prepareLocationCalibration();
6156 prepareButtons();
6157 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006158 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006159
6160 int32_t rawX = 100;
6161 int32_t rawY = 200;
6162
6163 float x = toDisplayX(toCookedX(rawX, rawY));
6164 float y = toDisplayY(toCookedY(rawX, rawY));
6165
6166 processDown(mapper, rawX, rawY);
6167 processSync(mapper);
6168
6169 NotifyMotionArgs args;
6170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6171 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6172 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6173}
6174
Michael Wrightd02c5b62014-02-10 15:10:22 -08006175TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006176 addConfigurationProperty("touch.deviceType", "touchScreen");
6177 prepareDisplay(DISPLAY_ORIENTATION_0);
6178 prepareButtons();
6179 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006180 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006181
6182 NotifyMotionArgs motionArgs;
6183 NotifyKeyArgs keyArgs;
6184
6185 processDown(mapper, 100, 200);
6186 processSync(mapper);
6187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6188 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6189 ASSERT_EQ(0, motionArgs.buttonState);
6190
6191 // press BTN_LEFT, release BTN_LEFT
6192 processKey(mapper, BTN_LEFT, 1);
6193 processSync(mapper);
6194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6195 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6196 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6197
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6199 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6200 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6201
Michael Wrightd02c5b62014-02-10 15:10:22 -08006202 processKey(mapper, BTN_LEFT, 0);
6203 processSync(mapper);
6204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006205 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006206 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006207
6208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006209 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006210 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006211
6212 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6213 processKey(mapper, BTN_RIGHT, 1);
6214 processKey(mapper, BTN_MIDDLE, 1);
6215 processSync(mapper);
6216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6217 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6218 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6219 motionArgs.buttonState);
6220
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6222 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6223 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6224
6225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6226 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6227 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6228 motionArgs.buttonState);
6229
Michael Wrightd02c5b62014-02-10 15:10:22 -08006230 processKey(mapper, BTN_RIGHT, 0);
6231 processSync(mapper);
6232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006233 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006234 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006235
6236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006237 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006238 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006239
6240 processKey(mapper, BTN_MIDDLE, 0);
6241 processSync(mapper);
6242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006243 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006244 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006245
6246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006247 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006248 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006249
6250 // press BTN_BACK, release BTN_BACK
6251 processKey(mapper, BTN_BACK, 1);
6252 processSync(mapper);
6253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6254 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6255 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006256
Michael Wrightd02c5b62014-02-10 15:10:22 -08006257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006258 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006259 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6260
6261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6262 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6263 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006264
6265 processKey(mapper, BTN_BACK, 0);
6266 processSync(mapper);
6267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006268 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006269 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006270
6271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006272 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006273 ASSERT_EQ(0, motionArgs.buttonState);
6274
Michael Wrightd02c5b62014-02-10 15:10:22 -08006275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6276 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6277 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6278
6279 // press BTN_SIDE, release BTN_SIDE
6280 processKey(mapper, BTN_SIDE, 1);
6281 processSync(mapper);
6282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6283 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6284 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006285
Michael Wrightd02c5b62014-02-10 15:10:22 -08006286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006287 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006288 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6289
6290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6291 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6292 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006293
6294 processKey(mapper, BTN_SIDE, 0);
6295 processSync(mapper);
6296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006297 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006298 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006299
6300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006301 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006302 ASSERT_EQ(0, motionArgs.buttonState);
6303
Michael Wrightd02c5b62014-02-10 15:10:22 -08006304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6305 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6306 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6307
6308 // press BTN_FORWARD, release BTN_FORWARD
6309 processKey(mapper, BTN_FORWARD, 1);
6310 processSync(mapper);
6311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6312 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6313 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006314
Michael Wrightd02c5b62014-02-10 15:10:22 -08006315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006316 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006317 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6318
6319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6320 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6321 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006322
6323 processKey(mapper, BTN_FORWARD, 0);
6324 processSync(mapper);
6325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006326 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006327 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006328
6329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006330 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006331 ASSERT_EQ(0, motionArgs.buttonState);
6332
Michael Wrightd02c5b62014-02-10 15:10:22 -08006333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6334 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6335 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6336
6337 // press BTN_EXTRA, release BTN_EXTRA
6338 processKey(mapper, BTN_EXTRA, 1);
6339 processSync(mapper);
6340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6341 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6342 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006343
Michael Wrightd02c5b62014-02-10 15:10:22 -08006344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006345 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006346 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6347
6348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6349 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6350 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006351
6352 processKey(mapper, BTN_EXTRA, 0);
6353 processSync(mapper);
6354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006355 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006356 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006357
6358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006359 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006360 ASSERT_EQ(0, motionArgs.buttonState);
6361
Michael Wrightd02c5b62014-02-10 15:10:22 -08006362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6363 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6364 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6365
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6367
Michael Wrightd02c5b62014-02-10 15:10:22 -08006368 // press BTN_STYLUS, release BTN_STYLUS
6369 processKey(mapper, BTN_STYLUS, 1);
6370 processSync(mapper);
6371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6372 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006373 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6374
6375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6376 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6377 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006378
6379 processKey(mapper, BTN_STYLUS, 0);
6380 processSync(mapper);
6381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006382 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006383 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006384
6385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006386 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006387 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006388
6389 // press BTN_STYLUS2, release BTN_STYLUS2
6390 processKey(mapper, BTN_STYLUS2, 1);
6391 processSync(mapper);
6392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6393 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006394 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6395
6396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6397 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6398 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006399
6400 processKey(mapper, BTN_STYLUS2, 0);
6401 processSync(mapper);
6402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006403 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006404 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006405
6406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006407 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006408 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006409
6410 // release touch
6411 processUp(mapper);
6412 processSync(mapper);
6413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6414 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6415 ASSERT_EQ(0, motionArgs.buttonState);
6416}
6417
6418TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006419 addConfigurationProperty("touch.deviceType", "touchScreen");
6420 prepareDisplay(DISPLAY_ORIENTATION_0);
6421 prepareButtons();
6422 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006423 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006424
6425 NotifyMotionArgs motionArgs;
6426
6427 // default tool type is finger
6428 processDown(mapper, 100, 200);
6429 processSync(mapper);
6430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6431 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6432 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6433
6434 // eraser
6435 processKey(mapper, BTN_TOOL_RUBBER, 1);
6436 processSync(mapper);
6437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6438 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6439 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6440
6441 // stylus
6442 processKey(mapper, BTN_TOOL_RUBBER, 0);
6443 processKey(mapper, BTN_TOOL_PEN, 1);
6444 processSync(mapper);
6445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6446 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6447 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6448
6449 // brush
6450 processKey(mapper, BTN_TOOL_PEN, 0);
6451 processKey(mapper, BTN_TOOL_BRUSH, 1);
6452 processSync(mapper);
6453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6454 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6455 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6456
6457 // pencil
6458 processKey(mapper, BTN_TOOL_BRUSH, 0);
6459 processKey(mapper, BTN_TOOL_PENCIL, 1);
6460 processSync(mapper);
6461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6462 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6463 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6464
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006465 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006466 processKey(mapper, BTN_TOOL_PENCIL, 0);
6467 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6468 processSync(mapper);
6469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6470 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6471 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6472
6473 // mouse
6474 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6475 processKey(mapper, BTN_TOOL_MOUSE, 1);
6476 processSync(mapper);
6477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6478 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6479 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6480
6481 // lens
6482 processKey(mapper, BTN_TOOL_MOUSE, 0);
6483 processKey(mapper, BTN_TOOL_LENS, 1);
6484 processSync(mapper);
6485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6486 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6487 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6488
6489 // double-tap
6490 processKey(mapper, BTN_TOOL_LENS, 0);
6491 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6492 processSync(mapper);
6493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6494 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6495 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6496
6497 // triple-tap
6498 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6499 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6500 processSync(mapper);
6501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6502 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6503 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6504
6505 // quad-tap
6506 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6507 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6508 processSync(mapper);
6509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6510 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6511 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6512
6513 // finger
6514 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6515 processKey(mapper, BTN_TOOL_FINGER, 1);
6516 processSync(mapper);
6517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6518 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6519 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6520
6521 // stylus trumps finger
6522 processKey(mapper, BTN_TOOL_PEN, 1);
6523 processSync(mapper);
6524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6525 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6526 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6527
6528 // eraser trumps stylus
6529 processKey(mapper, BTN_TOOL_RUBBER, 1);
6530 processSync(mapper);
6531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6532 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6533 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6534
6535 // mouse trumps eraser
6536 processKey(mapper, BTN_TOOL_MOUSE, 1);
6537 processSync(mapper);
6538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6539 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6540 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6541
6542 // back to default tool type
6543 processKey(mapper, BTN_TOOL_MOUSE, 0);
6544 processKey(mapper, BTN_TOOL_RUBBER, 0);
6545 processKey(mapper, BTN_TOOL_PEN, 0);
6546 processKey(mapper, BTN_TOOL_FINGER, 0);
6547 processSync(mapper);
6548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6549 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6550 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6551}
6552
6553TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006554 addConfigurationProperty("touch.deviceType", "touchScreen");
6555 prepareDisplay(DISPLAY_ORIENTATION_0);
6556 prepareButtons();
6557 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006558 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006559 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006560
6561 NotifyMotionArgs motionArgs;
6562
6563 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6564 processKey(mapper, BTN_TOOL_FINGER, 1);
6565 processMove(mapper, 100, 200);
6566 processSync(mapper);
6567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6568 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6569 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6570 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6571
6572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6573 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6575 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6576
6577 // move a little
6578 processMove(mapper, 150, 250);
6579 processSync(mapper);
6580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6581 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6583 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6584
6585 // down when BTN_TOUCH is pressed, pressure defaults to 1
6586 processKey(mapper, BTN_TOUCH, 1);
6587 processSync(mapper);
6588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6589 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6590 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6591 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6592
6593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6594 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6596 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6597
6598 // up when BTN_TOUCH is released, hover restored
6599 processKey(mapper, BTN_TOUCH, 0);
6600 processSync(mapper);
6601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6602 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6604 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6605
6606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6607 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6608 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6609 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6610
6611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6612 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6614 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6615
6616 // exit hover when pointer goes away
6617 processKey(mapper, BTN_TOOL_FINGER, 0);
6618 processSync(mapper);
6619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6620 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6622 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6623}
6624
6625TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006626 addConfigurationProperty("touch.deviceType", "touchScreen");
6627 prepareDisplay(DISPLAY_ORIENTATION_0);
6628 prepareButtons();
6629 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006630 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006631
6632 NotifyMotionArgs motionArgs;
6633
6634 // initially hovering because pressure is 0
6635 processDown(mapper, 100, 200);
6636 processPressure(mapper, 0);
6637 processSync(mapper);
6638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6639 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6640 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6641 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6642
6643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6644 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6645 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6646 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6647
6648 // move a little
6649 processMove(mapper, 150, 250);
6650 processSync(mapper);
6651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6652 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6654 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6655
6656 // down when pressure is non-zero
6657 processPressure(mapper, RAW_PRESSURE_MAX);
6658 processSync(mapper);
6659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6660 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6662 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6663
6664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6665 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6666 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6667 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6668
6669 // up when pressure becomes 0, hover restored
6670 processPressure(mapper, 0);
6671 processSync(mapper);
6672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6673 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6674 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6675 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6676
6677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6678 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6679 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6680 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6681
6682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6683 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6684 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6685 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6686
6687 // exit hover when pointer goes away
6688 processUp(mapper);
6689 processSync(mapper);
6690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6691 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6692 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6693 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6694}
6695
lilinnan687e58f2022-07-19 16:00:50 +08006696TEST_F(SingleTouchInputMapperTest,
6697 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6698 addConfigurationProperty("touch.deviceType", "touchScreen");
6699 prepareDisplay(DISPLAY_ORIENTATION_0);
6700 prepareButtons();
6701 prepareAxes(POSITION);
6702 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6703 NotifyMotionArgs motionArgs;
6704
6705 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006706 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006707 processSync(mapper);
6708
6709 // We should receive a down event
6710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6711 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6712
6713 // Change display id
6714 clearViewports();
6715 prepareSecondaryDisplay(ViewportType::INTERNAL);
6716
6717 // We should receive a cancel event
6718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6719 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6720 // Then receive reset called
6721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6722}
6723
Prabir Pradhan5632d622021-09-06 07:57:20 -07006724// --- TouchDisplayProjectionTest ---
6725
6726class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6727public:
6728 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6729 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6730 // rotated equivalent of the given un-rotated physical display bounds.
6731 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6732 uint32_t inverseRotationFlags;
6733 auto width = DISPLAY_WIDTH;
6734 auto height = DISPLAY_HEIGHT;
6735 switch (orientation) {
6736 case DISPLAY_ORIENTATION_90:
6737 inverseRotationFlags = ui::Transform::ROT_270;
6738 std::swap(width, height);
6739 break;
6740 case DISPLAY_ORIENTATION_180:
6741 inverseRotationFlags = ui::Transform::ROT_180;
6742 break;
6743 case DISPLAY_ORIENTATION_270:
6744 inverseRotationFlags = ui::Transform::ROT_90;
6745 std::swap(width, height);
6746 break;
6747 case DISPLAY_ORIENTATION_0:
6748 inverseRotationFlags = ui::Transform::ROT_0;
6749 break;
6750 default:
6751 FAIL() << "Invalid orientation: " << orientation;
6752 }
6753
6754 const ui::Transform rotation(inverseRotationFlags, width, height);
6755 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6756
6757 std::optional<DisplayViewport> internalViewport =
6758 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6759 DisplayViewport& v = *internalViewport;
6760 v.displayId = DISPLAY_ID;
6761 v.orientation = orientation;
6762
6763 v.logicalLeft = 0;
6764 v.logicalTop = 0;
6765 v.logicalRight = 100;
6766 v.logicalBottom = 100;
6767
6768 v.physicalLeft = rotatedPhysicalDisplay.left;
6769 v.physicalTop = rotatedPhysicalDisplay.top;
6770 v.physicalRight = rotatedPhysicalDisplay.right;
6771 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6772
6773 v.deviceWidth = width;
6774 v.deviceHeight = height;
6775
6776 v.isActive = true;
6777 v.uniqueId = UNIQUE_ID;
6778 v.type = ViewportType::INTERNAL;
6779 mFakePolicy->updateViewport(v);
6780 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6781 }
6782
6783 void assertReceivedMove(const Point& point) {
6784 NotifyMotionArgs motionArgs;
6785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6786 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6787 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6789 1, 0, 0, 0, 0, 0, 0, 0));
6790 }
6791};
6792
6793TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6794 addConfigurationProperty("touch.deviceType", "touchScreen");
6795 prepareDisplay(DISPLAY_ORIENTATION_0);
6796
6797 prepareButtons();
6798 prepareAxes(POSITION);
6799 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6800
6801 NotifyMotionArgs motionArgs;
6802
6803 // Configure the DisplayViewport such that the logical display maps to a subsection of
6804 // the display panel called the physical display. Here, the physical display is bounded by the
6805 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6806 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6807 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6808 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6809
6810 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6811 DISPLAY_ORIENTATION_270}) {
6812 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6813
6814 // Touches outside the physical display should be ignored, and should not generate any
6815 // events. Ensure touches at the following points that lie outside of the physical display
6816 // area do not generate any events.
6817 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6818 processDown(mapper, toRawX(point.x), toRawY(point.y));
6819 processSync(mapper);
6820 processUp(mapper);
6821 processSync(mapper);
6822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6823 << "Unexpected event generated for touch outside physical display at point: "
6824 << point.x << ", " << point.y;
6825 }
6826 }
6827}
6828
6829TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6830 addConfigurationProperty("touch.deviceType", "touchScreen");
6831 prepareDisplay(DISPLAY_ORIENTATION_0);
6832
6833 prepareButtons();
6834 prepareAxes(POSITION);
6835 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6836
6837 NotifyMotionArgs motionArgs;
6838
6839 // Configure the DisplayViewport such that the logical display maps to a subsection of
6840 // the display panel called the physical display. Here, the physical display is bounded by the
6841 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6842 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6843
6844 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6845 DISPLAY_ORIENTATION_270}) {
6846 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6847
6848 // Touches that start outside the physical display should be ignored until it enters the
6849 // physical display bounds, at which point it should generate a down event. Start a touch at
6850 // the point (5, 100), which is outside the physical display bounds.
6851 static const Point kOutsidePoint{5, 100};
6852 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6853 processSync(mapper);
6854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6855
6856 // Move the touch into the physical display area. This should generate a pointer down.
6857 processMove(mapper, toRawX(11), toRawY(21));
6858 processSync(mapper);
6859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6860 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6861 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6862 ASSERT_NO_FATAL_FAILURE(
6863 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6864
6865 // Move the touch inside the physical display area. This should generate a pointer move.
6866 processMove(mapper, toRawX(69), toRawY(159));
6867 processSync(mapper);
6868 assertReceivedMove({69, 159});
6869
6870 // Move outside the physical display area. Since the pointer is already down, this should
6871 // now continue generating events.
6872 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6873 processSync(mapper);
6874 assertReceivedMove(kOutsidePoint);
6875
6876 // Release. This should generate a pointer up.
6877 processUp(mapper);
6878 processSync(mapper);
6879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6880 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6881 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6882 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6883
6884 // Ensure no more events were generated.
6885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6887 }
6888}
6889
Michael Wrightd02c5b62014-02-10 15:10:22 -08006890// --- MultiTouchInputMapperTest ---
6891
6892class MultiTouchInputMapperTest : public TouchInputMapperTest {
6893protected:
6894 void prepareAxes(int axes);
6895
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006896 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6897 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6898 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6899 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6900 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6901 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6902 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6903 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6904 void processId(MultiTouchInputMapper& mapper, int32_t id);
6905 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6906 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6907 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6908 void processMTSync(MultiTouchInputMapper& mapper);
6909 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006910};
6911
6912void MultiTouchInputMapperTest::prepareAxes(int axes) {
6913 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006914 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6915 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006916 }
6917 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006918 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6919 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006920 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006921 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6922 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006923 }
6924 }
6925 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006926 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6927 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006928 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08006929 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006930 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006931 }
6932 }
6933 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006934 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6935 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006936 }
6937 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006938 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6939 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006940 }
6941 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006942 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6943 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006944 }
6945 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006946 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6947 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006948 }
6949 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006950 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6951 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006952 }
6953 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006954 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006955 }
6956}
6957
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006958void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6959 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006960 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6961 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006962}
6963
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006964void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6965 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006966 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006967}
6968
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006969void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6970 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006971 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006972}
6973
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006974void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006975 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006976}
6977
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006978void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006979 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006980}
6981
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006982void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6983 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006984 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006985}
6986
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006987void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006988 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006989}
6990
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006991void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006992 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006993}
6994
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006995void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006996 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006997}
6998
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006999void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007000 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007001}
7002
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007003void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007004 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007005}
7006
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007007void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7008 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007009 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007010}
7011
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007012void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007013 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007014}
7015
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007016void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007017 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007018}
7019
Michael Wrightd02c5b62014-02-10 15:10:22 -08007020TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007021 addConfigurationProperty("touch.deviceType", "touchScreen");
7022 prepareDisplay(DISPLAY_ORIENTATION_0);
7023 prepareAxes(POSITION);
7024 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007025 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007026
arthurhungdcef2dc2020-08-11 14:47:50 +08007027 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007028
7029 NotifyMotionArgs motionArgs;
7030
7031 // Two fingers down at once.
7032 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7033 processPosition(mapper, x1, y1);
7034 processMTSync(mapper);
7035 processPosition(mapper, x2, y2);
7036 processMTSync(mapper);
7037 processSync(mapper);
7038
7039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7040 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7041 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7042 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7043 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7044 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7045 ASSERT_EQ(0, motionArgs.flags);
7046 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7047 ASSERT_EQ(0, motionArgs.buttonState);
7048 ASSERT_EQ(0, motionArgs.edgeFlags);
7049 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7050 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7051 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7052 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7053 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7054 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7055 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7056 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7057
7058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7059 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7060 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7061 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7062 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007063 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007064 ASSERT_EQ(0, motionArgs.flags);
7065 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7066 ASSERT_EQ(0, motionArgs.buttonState);
7067 ASSERT_EQ(0, motionArgs.edgeFlags);
7068 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7069 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7070 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7071 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7072 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7073 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7074 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7075 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7076 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7077 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7078 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7079 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7080
7081 // Move.
7082 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7083 processPosition(mapper, x1, y1);
7084 processMTSync(mapper);
7085 processPosition(mapper, x2, y2);
7086 processMTSync(mapper);
7087 processSync(mapper);
7088
7089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7090 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7091 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7092 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7093 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7094 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7095 ASSERT_EQ(0, motionArgs.flags);
7096 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7097 ASSERT_EQ(0, motionArgs.buttonState);
7098 ASSERT_EQ(0, motionArgs.edgeFlags);
7099 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7100 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7101 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7102 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7103 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7104 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7105 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7106 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7107 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7108 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7109 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7110 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7111
7112 // First finger up.
7113 x2 += 15; y2 -= 20;
7114 processPosition(mapper, x2, y2);
7115 processMTSync(mapper);
7116 processSync(mapper);
7117
7118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7119 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7120 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7121 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7122 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007123 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007124 ASSERT_EQ(0, motionArgs.flags);
7125 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7126 ASSERT_EQ(0, motionArgs.buttonState);
7127 ASSERT_EQ(0, motionArgs.edgeFlags);
7128 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7129 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7130 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7131 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7132 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7133 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7134 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7135 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7136 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7137 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7138 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7139 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7140
7141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7142 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7143 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7144 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7145 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7146 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7147 ASSERT_EQ(0, motionArgs.flags);
7148 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7149 ASSERT_EQ(0, motionArgs.buttonState);
7150 ASSERT_EQ(0, motionArgs.edgeFlags);
7151 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7152 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7153 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7154 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7155 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7156 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7157 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7158 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7159
7160 // Move.
7161 x2 += 20; y2 -= 25;
7162 processPosition(mapper, x2, y2);
7163 processMTSync(mapper);
7164 processSync(mapper);
7165
7166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7167 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7168 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7169 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7170 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7171 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7172 ASSERT_EQ(0, motionArgs.flags);
7173 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7174 ASSERT_EQ(0, motionArgs.buttonState);
7175 ASSERT_EQ(0, motionArgs.edgeFlags);
7176 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7177 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7178 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7180 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7181 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7182 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7183 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7184
7185 // New finger down.
7186 int32_t x3 = 700, y3 = 300;
7187 processPosition(mapper, x2, y2);
7188 processMTSync(mapper);
7189 processPosition(mapper, x3, y3);
7190 processMTSync(mapper);
7191 processSync(mapper);
7192
7193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7194 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7195 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7196 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7197 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007198 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007199 ASSERT_EQ(0, motionArgs.flags);
7200 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7201 ASSERT_EQ(0, motionArgs.buttonState);
7202 ASSERT_EQ(0, motionArgs.edgeFlags);
7203 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7204 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7205 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7206 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7207 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7208 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7209 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7210 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7211 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7212 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7213 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7214 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7215
7216 // Second finger up.
7217 x3 += 30; y3 -= 20;
7218 processPosition(mapper, x3, y3);
7219 processMTSync(mapper);
7220 processSync(mapper);
7221
7222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7223 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7224 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7225 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7226 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007227 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007228 ASSERT_EQ(0, motionArgs.flags);
7229 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7230 ASSERT_EQ(0, motionArgs.buttonState);
7231 ASSERT_EQ(0, motionArgs.edgeFlags);
7232 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7233 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7234 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7235 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7236 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7237 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7238 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7239 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7240 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7241 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7242 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7243 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7244
7245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7246 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7247 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7248 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7249 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7250 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7251 ASSERT_EQ(0, motionArgs.flags);
7252 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7253 ASSERT_EQ(0, motionArgs.buttonState);
7254 ASSERT_EQ(0, motionArgs.edgeFlags);
7255 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7256 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7257 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7258 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7259 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7260 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7261 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7262 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7263
7264 // Last finger up.
7265 processMTSync(mapper);
7266 processSync(mapper);
7267
7268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7269 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7270 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7271 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7272 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7273 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7274 ASSERT_EQ(0, motionArgs.flags);
7275 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7276 ASSERT_EQ(0, motionArgs.buttonState);
7277 ASSERT_EQ(0, motionArgs.edgeFlags);
7278 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7279 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7280 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7282 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7283 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7284 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7285 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7286
7287 // Should not have sent any more keys or motions.
7288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7290}
7291
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007292TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7293 addConfigurationProperty("touch.deviceType", "touchScreen");
7294 prepareDisplay(DISPLAY_ORIENTATION_0);
7295
7296 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7297 /*fuzz*/ 0, /*resolution*/ 10);
7298 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7299 /*fuzz*/ 0, /*resolution*/ 11);
7300 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7301 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7302 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7303 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7304 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7305 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7306 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7307 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7308
7309 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7310
7311 // X and Y axes
7312 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7313 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7314 // Touch major and minor
7315 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7316 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7317 // Tool major and minor
7318 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7319 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7320}
7321
7322TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7323 addConfigurationProperty("touch.deviceType", "touchScreen");
7324 prepareDisplay(DISPLAY_ORIENTATION_0);
7325
7326 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7327 /*fuzz*/ 0, /*resolution*/ 10);
7328 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7329 /*fuzz*/ 0, /*resolution*/ 11);
7330
7331 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7332
7333 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7334
7335 // Touch major and minor
7336 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7337 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7338 // Tool major and minor
7339 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7340 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7341}
7342
Michael Wrightd02c5b62014-02-10 15:10:22 -08007343TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007344 addConfigurationProperty("touch.deviceType", "touchScreen");
7345 prepareDisplay(DISPLAY_ORIENTATION_0);
7346 prepareAxes(POSITION | ID);
7347 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007348 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007349
arthurhungdcef2dc2020-08-11 14:47:50 +08007350 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007351
7352 NotifyMotionArgs motionArgs;
7353
7354 // Two fingers down at once.
7355 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7356 processPosition(mapper, x1, y1);
7357 processId(mapper, 1);
7358 processMTSync(mapper);
7359 processPosition(mapper, x2, y2);
7360 processId(mapper, 2);
7361 processMTSync(mapper);
7362 processSync(mapper);
7363
7364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7365 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7366 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7367 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7368 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7369 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7370 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7371
7372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007373 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007374 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7375 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7376 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7377 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7378 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7379 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7380 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7381 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7382 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7383
7384 // Move.
7385 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7386 processPosition(mapper, x1, y1);
7387 processId(mapper, 1);
7388 processMTSync(mapper);
7389 processPosition(mapper, x2, y2);
7390 processId(mapper, 2);
7391 processMTSync(mapper);
7392 processSync(mapper);
7393
7394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7395 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7396 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7397 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7398 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7399 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7400 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7401 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7402 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7403 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7404 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7405
7406 // First finger up.
7407 x2 += 15; y2 -= 20;
7408 processPosition(mapper, x2, y2);
7409 processId(mapper, 2);
7410 processMTSync(mapper);
7411 processSync(mapper);
7412
7413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007414 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007415 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7416 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7417 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7418 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7419 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7420 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7421 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7422 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7423 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7424
7425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7426 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7427 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7428 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7429 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7431 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7432
7433 // Move.
7434 x2 += 20; y2 -= 25;
7435 processPosition(mapper, x2, y2);
7436 processId(mapper, 2);
7437 processMTSync(mapper);
7438 processSync(mapper);
7439
7440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7441 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7442 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7443 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7444 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7445 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7446 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7447
7448 // New finger down.
7449 int32_t x3 = 700, y3 = 300;
7450 processPosition(mapper, x2, y2);
7451 processId(mapper, 2);
7452 processMTSync(mapper);
7453 processPosition(mapper, x3, y3);
7454 processId(mapper, 3);
7455 processMTSync(mapper);
7456 processSync(mapper);
7457
7458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007459 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007460 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(x3), toDisplayY(y3), 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 // Second finger up.
7471 x3 += 30; y3 -= 20;
7472 processPosition(mapper, x3, y3);
7473 processId(mapper, 3);
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_1_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(x3), toDisplayY(y3), 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(0, 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(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7496
7497 // Last finger up.
7498 processMTSync(mapper);
7499 processSync(mapper);
7500
7501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7502 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7503 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7504 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7505 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7506 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7507 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7508
7509 // Should not have sent any more keys or motions.
7510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7512}
7513
7514TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007515 addConfigurationProperty("touch.deviceType", "touchScreen");
7516 prepareDisplay(DISPLAY_ORIENTATION_0);
7517 prepareAxes(POSITION | ID | SLOT);
7518 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007519 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007520
arthurhungdcef2dc2020-08-11 14:47:50 +08007521 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007522
7523 NotifyMotionArgs motionArgs;
7524
7525 // Two fingers down at once.
7526 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7527 processPosition(mapper, x1, y1);
7528 processId(mapper, 1);
7529 processSlot(mapper, 1);
7530 processPosition(mapper, x2, y2);
7531 processId(mapper, 2);
7532 processSync(mapper);
7533
7534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7535 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7536 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7537 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7538 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7540 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7541
7542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007543 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007544 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7545 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7546 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7547 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7548 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7549 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7550 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7551 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7552 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7553
7554 // Move.
7555 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7556 processSlot(mapper, 0);
7557 processPosition(mapper, x1, y1);
7558 processSlot(mapper, 1);
7559 processPosition(mapper, x2, y2);
7560 processSync(mapper);
7561
7562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7563 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7564 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7565 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7566 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7567 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7568 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7569 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7570 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7572 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7573
7574 // First finger up.
7575 x2 += 15; y2 -= 20;
7576 processSlot(mapper, 0);
7577 processId(mapper, -1);
7578 processSlot(mapper, 1);
7579 processPosition(mapper, x2, y2);
7580 processSync(mapper);
7581
7582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007583 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007584 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7585 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7586 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7587 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7588 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7589 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7590 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7592 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7593
7594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7595 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7596 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7597 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7598 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7600 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7601
7602 // Move.
7603 x2 += 20; y2 -= 25;
7604 processPosition(mapper, x2, y2);
7605 processSync(mapper);
7606
7607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7608 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7609 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7610 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7611 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7613 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7614
7615 // New finger down.
7616 int32_t x3 = 700, y3 = 300;
7617 processPosition(mapper, x2, y2);
7618 processSlot(mapper, 0);
7619 processId(mapper, 3);
7620 processPosition(mapper, x3, y3);
7621 processSync(mapper);
7622
7623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007624 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007625 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7626 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7627 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7628 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7629 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7631 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7632 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7633 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7634
7635 // Second finger up.
7636 x3 += 30; y3 -= 20;
7637 processSlot(mapper, 1);
7638 processId(mapper, -1);
7639 processSlot(mapper, 0);
7640 processPosition(mapper, x3, y3);
7641 processSync(mapper);
7642
7643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007644 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007645 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7646 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7647 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7648 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7649 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7650 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7651 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7652 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7653 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7654
7655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7656 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7657 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7658 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7659 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7661 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7662
7663 // Last finger up.
7664 processId(mapper, -1);
7665 processSync(mapper);
7666
7667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7668 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7669 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7670 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7671 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7672 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7673 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7674
7675 // Should not have sent any more keys or motions.
7676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7678}
7679
7680TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007681 addConfigurationProperty("touch.deviceType", "touchScreen");
7682 prepareDisplay(DISPLAY_ORIENTATION_0);
7683 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007684 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007685
7686 // These calculations are based on the input device calibration documentation.
7687 int32_t rawX = 100;
7688 int32_t rawY = 200;
7689 int32_t rawTouchMajor = 7;
7690 int32_t rawTouchMinor = 6;
7691 int32_t rawToolMajor = 9;
7692 int32_t rawToolMinor = 8;
7693 int32_t rawPressure = 11;
7694 int32_t rawDistance = 0;
7695 int32_t rawOrientation = 3;
7696 int32_t id = 5;
7697
7698 float x = toDisplayX(rawX);
7699 float y = toDisplayY(rawY);
7700 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7701 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7702 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7703 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7704 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7705 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7706 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7707 float distance = float(rawDistance);
7708
7709 processPosition(mapper, rawX, rawY);
7710 processTouchMajor(mapper, rawTouchMajor);
7711 processTouchMinor(mapper, rawTouchMinor);
7712 processToolMajor(mapper, rawToolMajor);
7713 processToolMinor(mapper, rawToolMinor);
7714 processPressure(mapper, rawPressure);
7715 processOrientation(mapper, rawOrientation);
7716 processDistance(mapper, rawDistance);
7717 processId(mapper, id);
7718 processMTSync(mapper);
7719 processSync(mapper);
7720
7721 NotifyMotionArgs args;
7722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7723 ASSERT_EQ(0, args.pointerProperties[0].id);
7724 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7725 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7726 orientation, distance));
7727}
7728
7729TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007730 addConfigurationProperty("touch.deviceType", "touchScreen");
7731 prepareDisplay(DISPLAY_ORIENTATION_0);
7732 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7733 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007734 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007735
7736 // These calculations are based on the input device calibration documentation.
7737 int32_t rawX = 100;
7738 int32_t rawY = 200;
7739 int32_t rawTouchMajor = 140;
7740 int32_t rawTouchMinor = 120;
7741 int32_t rawToolMajor = 180;
7742 int32_t rawToolMinor = 160;
7743
7744 float x = toDisplayX(rawX);
7745 float y = toDisplayY(rawY);
7746 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7747 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7748 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7749 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7750 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7751
7752 processPosition(mapper, rawX, rawY);
7753 processTouchMajor(mapper, rawTouchMajor);
7754 processTouchMinor(mapper, rawTouchMinor);
7755 processToolMajor(mapper, rawToolMajor);
7756 processToolMinor(mapper, rawToolMinor);
7757 processMTSync(mapper);
7758 processSync(mapper);
7759
7760 NotifyMotionArgs args;
7761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7762 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7763 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7764}
7765
7766TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007767 addConfigurationProperty("touch.deviceType", "touchScreen");
7768 prepareDisplay(DISPLAY_ORIENTATION_0);
7769 prepareAxes(POSITION | TOUCH | TOOL);
7770 addConfigurationProperty("touch.size.calibration", "diameter");
7771 addConfigurationProperty("touch.size.scale", "10");
7772 addConfigurationProperty("touch.size.bias", "160");
7773 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007774 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007775
7776 // These calculations are based on the input device calibration documentation.
7777 // Note: We only provide a single common touch/tool value because the device is assumed
7778 // not to emit separate values for each pointer (isSummed = 1).
7779 int32_t rawX = 100;
7780 int32_t rawY = 200;
7781 int32_t rawX2 = 150;
7782 int32_t rawY2 = 250;
7783 int32_t rawTouchMajor = 5;
7784 int32_t rawToolMajor = 8;
7785
7786 float x = toDisplayX(rawX);
7787 float y = toDisplayY(rawY);
7788 float x2 = toDisplayX(rawX2);
7789 float y2 = toDisplayY(rawY2);
7790 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7791 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7792 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7793
7794 processPosition(mapper, rawX, rawY);
7795 processTouchMajor(mapper, rawTouchMajor);
7796 processToolMajor(mapper, rawToolMajor);
7797 processMTSync(mapper);
7798 processPosition(mapper, rawX2, rawY2);
7799 processTouchMajor(mapper, rawTouchMajor);
7800 processToolMajor(mapper, rawToolMajor);
7801 processMTSync(mapper);
7802 processSync(mapper);
7803
7804 NotifyMotionArgs args;
7805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7806 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7807
7808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007809 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007810 ASSERT_EQ(size_t(2), args.pointerCount);
7811 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7812 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7814 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7815}
7816
7817TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007818 addConfigurationProperty("touch.deviceType", "touchScreen");
7819 prepareDisplay(DISPLAY_ORIENTATION_0);
7820 prepareAxes(POSITION | TOUCH | TOOL);
7821 addConfigurationProperty("touch.size.calibration", "area");
7822 addConfigurationProperty("touch.size.scale", "43");
7823 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007824 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007825
7826 // These calculations are based on the input device calibration documentation.
7827 int32_t rawX = 100;
7828 int32_t rawY = 200;
7829 int32_t rawTouchMajor = 5;
7830 int32_t rawToolMajor = 8;
7831
7832 float x = toDisplayX(rawX);
7833 float y = toDisplayY(rawY);
7834 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7835 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7836 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7837
7838 processPosition(mapper, rawX, rawY);
7839 processTouchMajor(mapper, rawTouchMajor);
7840 processToolMajor(mapper, rawToolMajor);
7841 processMTSync(mapper);
7842 processSync(mapper);
7843
7844 NotifyMotionArgs args;
7845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7846 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7847 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7848}
7849
7850TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007851 addConfigurationProperty("touch.deviceType", "touchScreen");
7852 prepareDisplay(DISPLAY_ORIENTATION_0);
7853 prepareAxes(POSITION | PRESSURE);
7854 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7855 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007856 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007857
Michael Wrightaa449c92017-12-13 21:21:43 +00007858 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007859 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007860 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7861 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7862 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7863
Michael Wrightd02c5b62014-02-10 15:10:22 -08007864 // These calculations are based on the input device calibration documentation.
7865 int32_t rawX = 100;
7866 int32_t rawY = 200;
7867 int32_t rawPressure = 60;
7868
7869 float x = toDisplayX(rawX);
7870 float y = toDisplayY(rawY);
7871 float pressure = float(rawPressure) * 0.01f;
7872
7873 processPosition(mapper, rawX, rawY);
7874 processPressure(mapper, rawPressure);
7875 processMTSync(mapper);
7876 processSync(mapper);
7877
7878 NotifyMotionArgs args;
7879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7880 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7881 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7882}
7883
7884TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007885 addConfigurationProperty("touch.deviceType", "touchScreen");
7886 prepareDisplay(DISPLAY_ORIENTATION_0);
7887 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007888 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007889
7890 NotifyMotionArgs motionArgs;
7891 NotifyKeyArgs keyArgs;
7892
7893 processId(mapper, 1);
7894 processPosition(mapper, 100, 200);
7895 processSync(mapper);
7896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7897 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7898 ASSERT_EQ(0, motionArgs.buttonState);
7899
7900 // press BTN_LEFT, release BTN_LEFT
7901 processKey(mapper, BTN_LEFT, 1);
7902 processSync(mapper);
7903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7904 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7905 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7906
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7908 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7909 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7910
Michael Wrightd02c5b62014-02-10 15:10:22 -08007911 processKey(mapper, BTN_LEFT, 0);
7912 processSync(mapper);
7913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007914 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007915 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007916
7917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007918 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007919 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007920
7921 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7922 processKey(mapper, BTN_RIGHT, 1);
7923 processKey(mapper, BTN_MIDDLE, 1);
7924 processSync(mapper);
7925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7926 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7927 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7928 motionArgs.buttonState);
7929
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7931 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7932 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7933
7934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7935 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7936 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7937 motionArgs.buttonState);
7938
Michael Wrightd02c5b62014-02-10 15:10:22 -08007939 processKey(mapper, BTN_RIGHT, 0);
7940 processSync(mapper);
7941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007942 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007943 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007944
7945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007946 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007947 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007948
7949 processKey(mapper, BTN_MIDDLE, 0);
7950 processSync(mapper);
7951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007952 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007953 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007954
7955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007956 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007957 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007958
7959 // press BTN_BACK, release BTN_BACK
7960 processKey(mapper, BTN_BACK, 1);
7961 processSync(mapper);
7962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7963 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7964 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007965
Michael Wrightd02c5b62014-02-10 15:10:22 -08007966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007967 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007968 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7969
7970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7971 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7972 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007973
7974 processKey(mapper, BTN_BACK, 0);
7975 processSync(mapper);
7976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007977 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007978 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007979
7980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007981 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007982 ASSERT_EQ(0, motionArgs.buttonState);
7983
Michael Wrightd02c5b62014-02-10 15:10:22 -08007984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7985 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7986 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7987
7988 // press BTN_SIDE, release BTN_SIDE
7989 processKey(mapper, BTN_SIDE, 1);
7990 processSync(mapper);
7991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7992 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7993 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007994
Michael Wrightd02c5b62014-02-10 15:10:22 -08007995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007996 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007997 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7998
7999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8000 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8001 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008002
8003 processKey(mapper, BTN_SIDE, 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(0, 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(0, motionArgs.buttonState);
8012
Michael Wrightd02c5b62014-02-10 15:10:22 -08008013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8014 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8015 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8016
8017 // press BTN_FORWARD, release BTN_FORWARD
8018 processKey(mapper, BTN_FORWARD, 1);
8019 processSync(mapper);
8020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8021 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8022 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008023
Michael Wrightd02c5b62014-02-10 15:10:22 -08008024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008025 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008026 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8027
8028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8029 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8030 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008031
8032 processKey(mapper, BTN_FORWARD, 0);
8033 processSync(mapper);
8034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008035 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008036 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008037
8038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008039 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008040 ASSERT_EQ(0, motionArgs.buttonState);
8041
Michael Wrightd02c5b62014-02-10 15:10:22 -08008042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8043 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8044 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8045
8046 // press BTN_EXTRA, release BTN_EXTRA
8047 processKey(mapper, BTN_EXTRA, 1);
8048 processSync(mapper);
8049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8050 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8051 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008052
Michael Wrightd02c5b62014-02-10 15:10:22 -08008053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008054 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008055 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8056
8057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8058 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8059 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008060
8061 processKey(mapper, BTN_EXTRA, 0);
8062 processSync(mapper);
8063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008064 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008065 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008066
8067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008068 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008069 ASSERT_EQ(0, motionArgs.buttonState);
8070
Michael Wrightd02c5b62014-02-10 15:10:22 -08008071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8072 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8073 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8074
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8076
Michael Wrightd02c5b62014-02-10 15:10:22 -08008077 // press BTN_STYLUS, release BTN_STYLUS
8078 processKey(mapper, BTN_STYLUS, 1);
8079 processSync(mapper);
8080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8081 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008082 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8083
8084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8085 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8086 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008087
8088 processKey(mapper, BTN_STYLUS, 0);
8089 processSync(mapper);
8090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008091 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008092 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008093
8094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008095 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008096 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008097
8098 // press BTN_STYLUS2, release BTN_STYLUS2
8099 processKey(mapper, BTN_STYLUS2, 1);
8100 processSync(mapper);
8101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8102 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008103 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8104
8105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8106 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8107 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008108
8109 processKey(mapper, BTN_STYLUS2, 0);
8110 processSync(mapper);
8111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008112 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008113 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008114
8115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008116 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008117 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008118
8119 // release touch
8120 processId(mapper, -1);
8121 processSync(mapper);
8122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8123 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8124 ASSERT_EQ(0, motionArgs.buttonState);
8125}
8126
8127TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008128 addConfigurationProperty("touch.deviceType", "touchScreen");
8129 prepareDisplay(DISPLAY_ORIENTATION_0);
8130 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008131 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008132
8133 NotifyMotionArgs motionArgs;
8134
8135 // default tool type is finger
8136 processId(mapper, 1);
8137 processPosition(mapper, 100, 200);
8138 processSync(mapper);
8139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8140 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8141 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8142
8143 // eraser
8144 processKey(mapper, BTN_TOOL_RUBBER, 1);
8145 processSync(mapper);
8146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8147 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8148 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8149
8150 // stylus
8151 processKey(mapper, BTN_TOOL_RUBBER, 0);
8152 processKey(mapper, BTN_TOOL_PEN, 1);
8153 processSync(mapper);
8154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8155 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8156 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8157
8158 // brush
8159 processKey(mapper, BTN_TOOL_PEN, 0);
8160 processKey(mapper, BTN_TOOL_BRUSH, 1);
8161 processSync(mapper);
8162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8163 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8164 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8165
8166 // pencil
8167 processKey(mapper, BTN_TOOL_BRUSH, 0);
8168 processKey(mapper, BTN_TOOL_PENCIL, 1);
8169 processSync(mapper);
8170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8171 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8172 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8173
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008174 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008175 processKey(mapper, BTN_TOOL_PENCIL, 0);
8176 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8177 processSync(mapper);
8178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8179 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8180 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8181
8182 // mouse
8183 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8184 processKey(mapper, BTN_TOOL_MOUSE, 1);
8185 processSync(mapper);
8186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8187 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8188 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8189
8190 // lens
8191 processKey(mapper, BTN_TOOL_MOUSE, 0);
8192 processKey(mapper, BTN_TOOL_LENS, 1);
8193 processSync(mapper);
8194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8195 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8196 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8197
8198 // double-tap
8199 processKey(mapper, BTN_TOOL_LENS, 0);
8200 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8201 processSync(mapper);
8202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8203 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8204 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8205
8206 // triple-tap
8207 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8208 processKey(mapper, BTN_TOOL_TRIPLETAP, 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_FINGER, motionArgs.pointerProperties[0].toolType);
8213
8214 // quad-tap
8215 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8216 processKey(mapper, BTN_TOOL_QUADTAP, 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_FINGER, motionArgs.pointerProperties[0].toolType);
8221
8222 // finger
8223 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8224 processKey(mapper, BTN_TOOL_FINGER, 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_FINGER, motionArgs.pointerProperties[0].toolType);
8229
8230 // stylus trumps finger
8231 processKey(mapper, BTN_TOOL_PEN, 1);
8232 processSync(mapper);
8233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8234 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8235 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8236
8237 // eraser trumps stylus
8238 processKey(mapper, BTN_TOOL_RUBBER, 1);
8239 processSync(mapper);
8240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8241 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8242 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8243
8244 // mouse trumps eraser
8245 processKey(mapper, BTN_TOOL_MOUSE, 1);
8246 processSync(mapper);
8247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8248 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8249 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8250
8251 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8252 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8253 processSync(mapper);
8254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8255 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8256 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8257
8258 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8259 processToolType(mapper, MT_TOOL_PEN);
8260 processSync(mapper);
8261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8262 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8263 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8264
8265 // back to default tool type
8266 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8267 processKey(mapper, BTN_TOOL_MOUSE, 0);
8268 processKey(mapper, BTN_TOOL_RUBBER, 0);
8269 processKey(mapper, BTN_TOOL_PEN, 0);
8270 processKey(mapper, BTN_TOOL_FINGER, 0);
8271 processSync(mapper);
8272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8273 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8274 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8275}
8276
8277TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008278 addConfigurationProperty("touch.deviceType", "touchScreen");
8279 prepareDisplay(DISPLAY_ORIENTATION_0);
8280 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008281 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008282 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008283
8284 NotifyMotionArgs motionArgs;
8285
8286 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8287 processId(mapper, 1);
8288 processPosition(mapper, 100, 200);
8289 processSync(mapper);
8290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8291 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8292 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8293 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8294
8295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8296 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8297 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8298 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8299
8300 // move a little
8301 processPosition(mapper, 150, 250);
8302 processSync(mapper);
8303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8304 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8305 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8306 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8307
8308 // down when BTN_TOUCH is pressed, pressure defaults to 1
8309 processKey(mapper, BTN_TOUCH, 1);
8310 processSync(mapper);
8311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8312 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8313 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8314 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8315
8316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8317 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8318 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8319 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8320
8321 // up when BTN_TOUCH is released, hover restored
8322 processKey(mapper, BTN_TOUCH, 0);
8323 processSync(mapper);
8324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8325 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8326 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8327 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8328
8329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8330 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8332 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8333
8334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8335 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8337 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8338
8339 // exit hover when pointer goes away
8340 processId(mapper, -1);
8341 processSync(mapper);
8342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8343 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8345 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8346}
8347
8348TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008349 addConfigurationProperty("touch.deviceType", "touchScreen");
8350 prepareDisplay(DISPLAY_ORIENTATION_0);
8351 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008352 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008353
8354 NotifyMotionArgs motionArgs;
8355
8356 // initially hovering because pressure is 0
8357 processId(mapper, 1);
8358 processPosition(mapper, 100, 200);
8359 processPressure(mapper, 0);
8360 processSync(mapper);
8361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8362 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8363 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8364 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8365
8366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8367 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8368 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8369 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8370
8371 // move a little
8372 processPosition(mapper, 150, 250);
8373 processSync(mapper);
8374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8375 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8376 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8377 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8378
8379 // down when pressure becomes non-zero
8380 processPressure(mapper, RAW_PRESSURE_MAX);
8381 processSync(mapper);
8382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8383 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8384 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8385 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8386
8387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8388 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8390 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8391
8392 // up when pressure becomes 0, hover restored
8393 processPressure(mapper, 0);
8394 processSync(mapper);
8395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8396 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8397 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8398 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8399
8400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8401 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8402 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8403 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8404
8405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8406 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8407 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8408 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8409
8410 // exit hover when pointer goes away
8411 processId(mapper, -1);
8412 processSync(mapper);
8413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8414 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8416 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8417}
8418
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008419/**
8420 * Set the input device port <--> display port associations, and check that the
8421 * events are routed to the display that matches the display port.
8422 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8423 */
8424TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008425 const std::string usb2 = "USB2";
8426 const uint8_t hdmi1 = 0;
8427 const uint8_t hdmi2 = 1;
8428 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008429 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008430
8431 addConfigurationProperty("touch.deviceType", "touchScreen");
8432 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008433 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008434
8435 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8436 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8437
8438 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8439 // for this input device is specified, and the matching viewport is not present,
8440 // the input device should be disabled (at the mapper level).
8441
8442 // Add viewport for display 2 on hdmi2
8443 prepareSecondaryDisplay(type, hdmi2);
8444 // Send a touch event
8445 processPosition(mapper, 100, 100);
8446 processSync(mapper);
8447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8448
8449 // Add viewport for display 1 on hdmi1
8450 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8451 // Send a touch event again
8452 processPosition(mapper, 100, 100);
8453 processSync(mapper);
8454
8455 NotifyMotionArgs args;
8456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8457 ASSERT_EQ(DISPLAY_ID, args.displayId);
8458}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008459
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008460TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8461 addConfigurationProperty("touch.deviceType", "touchScreen");
8462 prepareAxes(POSITION);
8463 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8464
8465 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8466
8467 prepareDisplay(DISPLAY_ORIENTATION_0);
8468 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8469
8470 // Send a touch event
8471 processPosition(mapper, 100, 100);
8472 processSync(mapper);
8473
8474 NotifyMotionArgs args;
8475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8476 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8477}
8478
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008479TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008480 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008481 std::shared_ptr<FakePointerController> fakePointerController =
8482 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008483 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008484 fakePointerController->setPosition(100, 200);
8485 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008486 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008487
Garfield Tan888a6a42020-01-09 11:39:16 -08008488 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008489 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008490
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008491 prepareDisplay(DISPLAY_ORIENTATION_0);
8492 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008493 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008494
8495 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008496 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008497
8498 NotifyMotionArgs motionArgs;
8499 processPosition(mapper, 100, 100);
8500 processSync(mapper);
8501
8502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8503 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8504 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8505}
8506
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008507/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008508 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8509 */
8510TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8511 addConfigurationProperty("touch.deviceType", "touchScreen");
8512 prepareAxes(POSITION);
8513 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8514
8515 prepareDisplay(DISPLAY_ORIENTATION_0);
8516 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8517 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8518 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8519 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8520
8521 NotifyMotionArgs args;
8522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8523 ASSERT_EQ(26, args.readTime);
8524
8525 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8526 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8527 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8528
8529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8530 ASSERT_EQ(33, args.readTime);
8531}
8532
8533/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008534 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8535 * events should not be delivered to the listener.
8536 */
8537TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8538 addConfigurationProperty("touch.deviceType", "touchScreen");
8539 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8540 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8541 ViewportType::INTERNAL);
8542 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8543 prepareAxes(POSITION);
8544 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8545
8546 NotifyMotionArgs motionArgs;
8547 processPosition(mapper, 100, 100);
8548 processSync(mapper);
8549
8550 mFakeListener->assertNotifyMotionWasNotCalled();
8551}
8552
Garfield Tanc734e4f2021-01-15 20:01:39 -08008553TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8554 addConfigurationProperty("touch.deviceType", "touchScreen");
8555 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8556 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8557 ViewportType::INTERNAL);
8558 std::optional<DisplayViewport> optionalDisplayViewport =
8559 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8560 ASSERT_TRUE(optionalDisplayViewport.has_value());
8561 DisplayViewport displayViewport = *optionalDisplayViewport;
8562
8563 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8564 prepareAxes(POSITION);
8565 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8566
8567 // Finger down
8568 int32_t x = 100, y = 100;
8569 processPosition(mapper, x, y);
8570 processSync(mapper);
8571
8572 NotifyMotionArgs motionArgs;
8573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8574 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8575
8576 // Deactivate display viewport
8577 displayViewport.isActive = false;
8578 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8579 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8580
8581 // Finger move
8582 x += 10, y += 10;
8583 processPosition(mapper, x, y);
8584 processSync(mapper);
8585
8586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8587 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8588
8589 // Reactivate display viewport
8590 displayViewport.isActive = true;
8591 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8592 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8593
8594 // Finger move again
8595 x += 10, y += 10;
8596 processPosition(mapper, x, y);
8597 processSync(mapper);
8598
8599 // Gesture is aborted, so events after display is activated won't be dispatched until there is
8600 // no pointer on the touch device.
8601 mFakeListener->assertNotifyMotionWasNotCalled();
8602}
8603
Arthur Hung7c645402019-01-25 17:45:42 +08008604TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8605 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008606 prepareAxes(POSITION | ID | SLOT);
8607 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008608 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008609
8610 // Create the second touch screen device, and enable multi fingers.
8611 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008612 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008613 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008614 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008615 std::shared_ptr<InputDevice> device2 =
8616 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008617 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008618
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008619 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8620 0 /*flat*/, 0 /*fuzz*/);
8621 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8622 0 /*flat*/, 0 /*fuzz*/);
8623 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8624 0 /*flat*/, 0 /*fuzz*/);
8625 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8626 0 /*flat*/, 0 /*fuzz*/);
8627 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8628 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8629 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008630
8631 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008632 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008633 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8634 device2->reset(ARBITRARY_TIME);
8635
8636 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008637 std::shared_ptr<FakePointerController> fakePointerController =
8638 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008639 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008640
8641 // Setup policy for associated displays and show touches.
8642 const uint8_t hdmi1 = 0;
8643 const uint8_t hdmi2 = 1;
8644 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8645 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8646 mFakePolicy->setShowTouches(true);
8647
8648 // Create displays.
8649 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008650 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008651
8652 // Default device will reconfigure above, need additional reconfiguration for another device.
8653 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan197e0862022-07-01 14:28:00 +00008654 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8655 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008656
8657 // Two fingers down at default display.
8658 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8659 processPosition(mapper, x1, y1);
8660 processId(mapper, 1);
8661 processSlot(mapper, 1);
8662 processPosition(mapper, x2, y2);
8663 processId(mapper, 2);
8664 processSync(mapper);
8665
8666 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8667 fakePointerController->getSpots().find(DISPLAY_ID);
8668 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8669 ASSERT_EQ(size_t(2), iter->second.size());
8670
8671 // Two fingers down at second display.
8672 processPosition(mapper2, x1, y1);
8673 processId(mapper2, 1);
8674 processSlot(mapper2, 1);
8675 processPosition(mapper2, x2, y2);
8676 processId(mapper2, 2);
8677 processSync(mapper2);
8678
8679 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8680 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8681 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008682
8683 // Disable the show touches configuration and ensure the spots are cleared.
8684 mFakePolicy->setShowTouches(false);
8685 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8686 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
8687
8688 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008689}
8690
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008691TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008692 prepareAxes(POSITION);
8693 addConfigurationProperty("touch.deviceType", "touchScreen");
8694 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008695 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008696
8697 NotifyMotionArgs motionArgs;
8698 // Unrotated video frame
8699 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8700 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008701 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008702 processPosition(mapper, 100, 200);
8703 processSync(mapper);
8704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8705 ASSERT_EQ(frames, motionArgs.videoFrames);
8706
8707 // Subsequent touch events should not have any videoframes
8708 // This is implemented separately in FakeEventHub,
8709 // but that should match the behaviour of TouchVideoDevice.
8710 processPosition(mapper, 200, 200);
8711 processSync(mapper);
8712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8713 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8714}
8715
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008716TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008717 prepareAxes(POSITION);
8718 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008719 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008720 // Unrotated video frame
8721 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8722 NotifyMotionArgs motionArgs;
8723
8724 // Test all 4 orientations
8725 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008726 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8727 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8728 clearViewports();
8729 prepareDisplay(orientation);
8730 std::vector<TouchVideoFrame> frames{frame};
8731 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8732 processPosition(mapper, 100, 200);
8733 processSync(mapper);
8734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8735 ASSERT_EQ(frames, motionArgs.videoFrames);
8736 }
8737}
8738
8739TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8740 prepareAxes(POSITION);
8741 addConfigurationProperty("touch.deviceType", "touchScreen");
8742 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8743 // orientation-aware are affected by display rotation.
8744 addConfigurationProperty("touch.orientationAware", "0");
8745 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8746 // Unrotated video frame
8747 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8748 NotifyMotionArgs motionArgs;
8749
8750 // Test all 4 orientations
8751 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008752 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8753 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8754 clearViewports();
8755 prepareDisplay(orientation);
8756 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008757 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008758 processPosition(mapper, 100, 200);
8759 processSync(mapper);
8760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008761 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8762 // compared to the display. This is so that when the window transform (which contains the
8763 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8764 // window's coordinate space.
8765 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008766 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08008767
8768 // Release finger.
8769 processSync(mapper);
8770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008771 }
8772}
8773
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008774TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008775 prepareAxes(POSITION);
8776 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008777 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008778 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8779 // so mix these.
8780 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8781 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8782 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8783 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8784 NotifyMotionArgs motionArgs;
8785
8786 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008787 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008788 processPosition(mapper, 100, 200);
8789 processSync(mapper);
8790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008791 ASSERT_EQ(frames, motionArgs.videoFrames);
8792}
8793
8794TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8795 prepareAxes(POSITION);
8796 addConfigurationProperty("touch.deviceType", "touchScreen");
8797 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8798 // orientation-aware are affected by display rotation.
8799 addConfigurationProperty("touch.orientationAware", "0");
8800 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8801 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8802 // so mix these.
8803 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8804 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8805 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8806 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8807 NotifyMotionArgs motionArgs;
8808
8809 prepareDisplay(DISPLAY_ORIENTATION_90);
8810 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8811 processPosition(mapper, 100, 200);
8812 processSync(mapper);
8813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8814 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8815 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8816 // compared to the display. This is so that when the window transform (which contains the
8817 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8818 // window's coordinate space.
8819 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8820 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008821 ASSERT_EQ(frames, motionArgs.videoFrames);
8822}
8823
Arthur Hung9da14732019-09-02 16:16:58 +08008824/**
8825 * If we had defined port associations, but the viewport is not ready, the touch device would be
8826 * expected to be disabled, and it should be enabled after the viewport has found.
8827 */
8828TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008829 constexpr uint8_t hdmi2 = 1;
8830 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008831 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008832
8833 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8834
8835 addConfigurationProperty("touch.deviceType", "touchScreen");
8836 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008837 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008838
8839 ASSERT_EQ(mDevice->isEnabled(), false);
8840
8841 // Add display on hdmi2, the device should be enabled and can receive touch event.
8842 prepareSecondaryDisplay(type, hdmi2);
8843 ASSERT_EQ(mDevice->isEnabled(), true);
8844
8845 // Send a touch event.
8846 processPosition(mapper, 100, 100);
8847 processSync(mapper);
8848
8849 NotifyMotionArgs args;
8850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8851 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8852}
8853
Arthur Hung421eb1c2020-01-16 00:09:42 +08008854TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008855 addConfigurationProperty("touch.deviceType", "touchScreen");
8856 prepareDisplay(DISPLAY_ORIENTATION_0);
8857 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008858 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008859
8860 NotifyMotionArgs motionArgs;
8861
8862 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8863 // finger down
8864 processId(mapper, 1);
8865 processPosition(mapper, x1, y1);
8866 processSync(mapper);
8867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8868 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8869 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8870
8871 // finger move
8872 processId(mapper, 1);
8873 processPosition(mapper, x2, y2);
8874 processSync(mapper);
8875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8876 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8877 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8878
8879 // finger up.
8880 processId(mapper, -1);
8881 processSync(mapper);
8882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8883 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8884 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8885
8886 // new finger down
8887 processId(mapper, 1);
8888 processPosition(mapper, x3, y3);
8889 processSync(mapper);
8890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8891 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8892 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8893}
8894
8895/**
arthurhungcc7f9802020-04-30 17:55:40 +08008896 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8897 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008898 */
arthurhungcc7f9802020-04-30 17:55:40 +08008899TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008900 addConfigurationProperty("touch.deviceType", "touchScreen");
8901 prepareDisplay(DISPLAY_ORIENTATION_0);
8902 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008903 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008904
8905 NotifyMotionArgs motionArgs;
8906
8907 // default tool type is finger
8908 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008909 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008910 processPosition(mapper, x1, y1);
8911 processSync(mapper);
8912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8913 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8914 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8915
8916 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8917 processToolType(mapper, MT_TOOL_PALM);
8918 processSync(mapper);
8919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8920 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8921
8922 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008923 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008924 processPosition(mapper, x2, y2);
8925 processSync(mapper);
8926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8927
8928 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008929 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008930 processSync(mapper);
8931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8932
8933 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008934 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008935 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008936 processPosition(mapper, x3, y3);
8937 processSync(mapper);
8938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8939 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8940 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8941}
8942
arthurhungbf89a482020-04-17 17:37:55 +08008943/**
arthurhungcc7f9802020-04-30 17:55:40 +08008944 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8945 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008946 */
arthurhungcc7f9802020-04-30 17:55:40 +08008947TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008948 addConfigurationProperty("touch.deviceType", "touchScreen");
8949 prepareDisplay(DISPLAY_ORIENTATION_0);
8950 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8951 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8952
8953 NotifyMotionArgs motionArgs;
8954
8955 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008956 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8957 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008958 processPosition(mapper, x1, y1);
8959 processSync(mapper);
8960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8961 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8962 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8963
8964 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008965 processSlot(mapper, SECOND_SLOT);
8966 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008967 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008968 processSync(mapper);
8969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008970 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008971 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8972
8973 // If the tool type of the first finger changes to MT_TOOL_PALM,
8974 // we expect to receive ACTION_POINTER_UP with cancel flag.
8975 processSlot(mapper, FIRST_SLOT);
8976 processId(mapper, FIRST_TRACKING_ID);
8977 processToolType(mapper, MT_TOOL_PALM);
8978 processSync(mapper);
8979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008980 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008981 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8982
8983 // The following MOVE events of second finger should be processed.
8984 processSlot(mapper, SECOND_SLOT);
8985 processId(mapper, SECOND_TRACKING_ID);
8986 processPosition(mapper, x2 + 1, y2 + 1);
8987 processSync(mapper);
8988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8989 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8990 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8991
8992 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8993 // it. Second finger receive move.
8994 processSlot(mapper, FIRST_SLOT);
8995 processId(mapper, INVALID_TRACKING_ID);
8996 processSync(mapper);
8997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8998 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8999 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9000
9001 // Second finger keeps moving.
9002 processSlot(mapper, SECOND_SLOT);
9003 processId(mapper, SECOND_TRACKING_ID);
9004 processPosition(mapper, x2 + 2, y2 + 2);
9005 processSync(mapper);
9006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9007 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9008 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9009
9010 // Second finger up.
9011 processId(mapper, INVALID_TRACKING_ID);
9012 processSync(mapper);
9013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9014 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9015 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9016}
9017
9018/**
9019 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9020 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9021 */
9022TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9023 addConfigurationProperty("touch.deviceType", "touchScreen");
9024 prepareDisplay(DISPLAY_ORIENTATION_0);
9025 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9026 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9027
9028 NotifyMotionArgs motionArgs;
9029
9030 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9031 // First finger down.
9032 processId(mapper, FIRST_TRACKING_ID);
9033 processPosition(mapper, x1, y1);
9034 processSync(mapper);
9035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9036 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9037 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9038
9039 // Second finger down.
9040 processSlot(mapper, SECOND_SLOT);
9041 processId(mapper, SECOND_TRACKING_ID);
9042 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009043 processSync(mapper);
9044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009045 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009046 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9047
arthurhungcc7f9802020-04-30 17:55:40 +08009048 // If the tool type of the first finger changes to MT_TOOL_PALM,
9049 // we expect to receive ACTION_POINTER_UP with cancel flag.
9050 processSlot(mapper, FIRST_SLOT);
9051 processId(mapper, FIRST_TRACKING_ID);
9052 processToolType(mapper, MT_TOOL_PALM);
9053 processSync(mapper);
9054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009055 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009056 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9057
9058 // Second finger keeps moving.
9059 processSlot(mapper, SECOND_SLOT);
9060 processId(mapper, SECOND_TRACKING_ID);
9061 processPosition(mapper, x2 + 1, y2 + 1);
9062 processSync(mapper);
9063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9064 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9065
9066 // second finger becomes palm, receive cancel due to only 1 finger is active.
9067 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009068 processToolType(mapper, MT_TOOL_PALM);
9069 processSync(mapper);
9070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9071 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9072
arthurhungcc7f9802020-04-30 17:55:40 +08009073 // third finger down.
9074 processSlot(mapper, THIRD_SLOT);
9075 processId(mapper, THIRD_TRACKING_ID);
9076 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009077 processPosition(mapper, x3, y3);
9078 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9080 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9081 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009082 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9083
9084 // third finger move
9085 processId(mapper, THIRD_TRACKING_ID);
9086 processPosition(mapper, x3 + 1, y3 + 1);
9087 processSync(mapper);
9088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9089 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9090
9091 // first finger up, third finger receive move.
9092 processSlot(mapper, FIRST_SLOT);
9093 processId(mapper, INVALID_TRACKING_ID);
9094 processSync(mapper);
9095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9096 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9097 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9098
9099 // second finger up, third finger receive move.
9100 processSlot(mapper, SECOND_SLOT);
9101 processId(mapper, INVALID_TRACKING_ID);
9102 processSync(mapper);
9103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9104 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9105 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9106
9107 // third finger up.
9108 processSlot(mapper, THIRD_SLOT);
9109 processId(mapper, INVALID_TRACKING_ID);
9110 processSync(mapper);
9111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9112 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9113 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9114}
9115
9116/**
9117 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9118 * and the active finger could still be allowed to receive the events
9119 */
9120TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9121 addConfigurationProperty("touch.deviceType", "touchScreen");
9122 prepareDisplay(DISPLAY_ORIENTATION_0);
9123 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9124 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9125
9126 NotifyMotionArgs motionArgs;
9127
9128 // default tool type is finger
9129 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9130 processId(mapper, FIRST_TRACKING_ID);
9131 processPosition(mapper, x1, y1);
9132 processSync(mapper);
9133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9134 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9135 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9136
9137 // Second finger down.
9138 processSlot(mapper, SECOND_SLOT);
9139 processId(mapper, SECOND_TRACKING_ID);
9140 processPosition(mapper, x2, y2);
9141 processSync(mapper);
9142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009143 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009144 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9145
9146 // If the tool type of the second finger changes to MT_TOOL_PALM,
9147 // we expect to receive ACTION_POINTER_UP with cancel flag.
9148 processId(mapper, SECOND_TRACKING_ID);
9149 processToolType(mapper, MT_TOOL_PALM);
9150 processSync(mapper);
9151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009152 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009153 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9154
9155 // The following MOVE event should be processed.
9156 processSlot(mapper, FIRST_SLOT);
9157 processId(mapper, FIRST_TRACKING_ID);
9158 processPosition(mapper, x1 + 1, y1 + 1);
9159 processSync(mapper);
9160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9161 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9162 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9163
9164 // second finger up.
9165 processSlot(mapper, SECOND_SLOT);
9166 processId(mapper, INVALID_TRACKING_ID);
9167 processSync(mapper);
9168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9169 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9170
9171 // first finger keep moving
9172 processSlot(mapper, FIRST_SLOT);
9173 processId(mapper, FIRST_TRACKING_ID);
9174 processPosition(mapper, x1 + 2, y1 + 2);
9175 processSync(mapper);
9176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9177 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9178
9179 // first finger up.
9180 processId(mapper, INVALID_TRACKING_ID);
9181 processSync(mapper);
9182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9183 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9184 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009185}
9186
Arthur Hung9ad18942021-06-19 02:04:46 +00009187/**
9188 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9189 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9190 * cause slot be valid again.
9191 */
9192TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9193 addConfigurationProperty("touch.deviceType", "touchScreen");
9194 prepareDisplay(DISPLAY_ORIENTATION_0);
9195 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9196 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9197
9198 NotifyMotionArgs motionArgs;
9199
9200 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9201 // First finger down.
9202 processId(mapper, FIRST_TRACKING_ID);
9203 processPosition(mapper, x1, y1);
9204 processPressure(mapper, RAW_PRESSURE_MAX);
9205 processSync(mapper);
9206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9207 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9208 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9209
9210 // First finger move.
9211 processId(mapper, FIRST_TRACKING_ID);
9212 processPosition(mapper, x1 + 1, y1 + 1);
9213 processPressure(mapper, RAW_PRESSURE_MAX);
9214 processSync(mapper);
9215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9216 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9217 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9218
9219 // Second finger down.
9220 processSlot(mapper, SECOND_SLOT);
9221 processId(mapper, SECOND_TRACKING_ID);
9222 processPosition(mapper, x2, y2);
9223 processPressure(mapper, RAW_PRESSURE_MAX);
9224 processSync(mapper);
9225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009226 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009227 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9228
9229 // second finger up with some unexpected data.
9230 processSlot(mapper, SECOND_SLOT);
9231 processId(mapper, INVALID_TRACKING_ID);
9232 processPosition(mapper, x2, y2);
9233 processSync(mapper);
9234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009235 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009236 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9237
9238 // first finger up with some unexpected data.
9239 processSlot(mapper, FIRST_SLOT);
9240 processId(mapper, INVALID_TRACKING_ID);
9241 processPosition(mapper, x2, y2);
9242 processPressure(mapper, RAW_PRESSURE_MAX);
9243 processSync(mapper);
9244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9245 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9246 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9247}
9248
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009249// --- MultiTouchInputMapperTest_ExternalDevice ---
9250
9251class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9252protected:
Chris Yea52ade12020-08-27 16:49:20 -07009253 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009254};
9255
9256/**
9257 * Expect fallback to internal viewport if device is external and external viewport is not present.
9258 */
9259TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9260 prepareAxes(POSITION);
9261 addConfigurationProperty("touch.deviceType", "touchScreen");
9262 prepareDisplay(DISPLAY_ORIENTATION_0);
9263 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9264
9265 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9266
9267 NotifyMotionArgs motionArgs;
9268
9269 // Expect the event to be sent to the internal viewport,
9270 // because an external viewport is not present.
9271 processPosition(mapper, 100, 100);
9272 processSync(mapper);
9273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9274 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9275
9276 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009277 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009278 processPosition(mapper, 100, 100);
9279 processSync(mapper);
9280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9281 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9282}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009283
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009284TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9285 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9286 std::shared_ptr<FakePointerController> fakePointerController =
9287 std::make_shared<FakePointerController>();
9288 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9289 fakePointerController->setPosition(0, 0);
9290 fakePointerController->setButtonState(0);
9291
9292 // prepare device and capture
9293 prepareDisplay(DISPLAY_ORIENTATION_0);
9294 prepareAxes(POSITION | ID | SLOT);
9295 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9296 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9297 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009298 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009299 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9300
9301 // captured touchpad should be a touchpad source
9302 NotifyDeviceResetArgs resetArgs;
9303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9304 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9305
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009306 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009307
9308 const InputDeviceInfo::MotionRange* relRangeX =
9309 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9310 ASSERT_NE(relRangeX, nullptr);
9311 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9312 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9313 const InputDeviceInfo::MotionRange* relRangeY =
9314 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9315 ASSERT_NE(relRangeY, nullptr);
9316 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9317 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9318
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009319 // run captured pointer tests - note that this is unscaled, so input listener events should be
9320 // identical to what the hardware sends (accounting for any
9321 // calibration).
9322 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009323 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009324 processId(mapper, 1);
9325 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9326 processKey(mapper, BTN_TOUCH, 1);
9327 processSync(mapper);
9328
9329 // expect coord[0] to contain initial location of touch 0
9330 NotifyMotionArgs args;
9331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9332 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9333 ASSERT_EQ(1U, args.pointerCount);
9334 ASSERT_EQ(0, args.pointerProperties[0].id);
9335 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9336 ASSERT_NO_FATAL_FAILURE(
9337 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9338
9339 // FINGER 1 DOWN
9340 processSlot(mapper, 1);
9341 processId(mapper, 2);
9342 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9343 processSync(mapper);
9344
9345 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009347 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009348 ASSERT_EQ(2U, args.pointerCount);
9349 ASSERT_EQ(0, args.pointerProperties[0].id);
9350 ASSERT_EQ(1, args.pointerProperties[1].id);
9351 ASSERT_NO_FATAL_FAILURE(
9352 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9353 ASSERT_NO_FATAL_FAILURE(
9354 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9355
9356 // FINGER 1 MOVE
9357 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9358 processSync(mapper);
9359
9360 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9361 // from move
9362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9363 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9364 ASSERT_NO_FATAL_FAILURE(
9365 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9366 ASSERT_NO_FATAL_FAILURE(
9367 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9368
9369 // FINGER 0 MOVE
9370 processSlot(mapper, 0);
9371 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9372 processSync(mapper);
9373
9374 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9376 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9377 ASSERT_NO_FATAL_FAILURE(
9378 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9379 ASSERT_NO_FATAL_FAILURE(
9380 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9381
9382 // BUTTON DOWN
9383 processKey(mapper, BTN_LEFT, 1);
9384 processSync(mapper);
9385
9386 // touchinputmapper design sends a move before button press
9387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9388 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9390 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9391
9392 // BUTTON UP
9393 processKey(mapper, BTN_LEFT, 0);
9394 processSync(mapper);
9395
9396 // touchinputmapper design sends a move after button release
9397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9398 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9400 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9401
9402 // FINGER 0 UP
9403 processId(mapper, -1);
9404 processSync(mapper);
9405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9406 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9407
9408 // FINGER 1 MOVE
9409 processSlot(mapper, 1);
9410 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9411 processSync(mapper);
9412
9413 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9415 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9416 ASSERT_EQ(1U, args.pointerCount);
9417 ASSERT_EQ(1, args.pointerProperties[0].id);
9418 ASSERT_NO_FATAL_FAILURE(
9419 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9420
9421 // FINGER 1 UP
9422 processId(mapper, -1);
9423 processKey(mapper, BTN_TOUCH, 0);
9424 processSync(mapper);
9425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9426 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9427
9428 // non captured touchpad should be a mouse source
9429 mFakePolicy->setPointerCapture(false);
9430 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9432 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9433}
9434
9435TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9436 std::shared_ptr<FakePointerController> fakePointerController =
9437 std::make_shared<FakePointerController>();
9438 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9439 fakePointerController->setPosition(0, 0);
9440 fakePointerController->setButtonState(0);
9441
9442 // prepare device and capture
9443 prepareDisplay(DISPLAY_ORIENTATION_0);
9444 prepareAxes(POSITION | ID | SLOT);
9445 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9446 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009447 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009448 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9449 // run uncaptured pointer tests - pushes out generic events
9450 // FINGER 0 DOWN
9451 processId(mapper, 3);
9452 processPosition(mapper, 100, 100);
9453 processKey(mapper, BTN_TOUCH, 1);
9454 processSync(mapper);
9455
9456 // start at (100,100), cursor should be at (0,0) * scale
9457 NotifyMotionArgs args;
9458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9459 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9460 ASSERT_NO_FATAL_FAILURE(
9461 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9462
9463 // FINGER 0 MOVE
9464 processPosition(mapper, 200, 200);
9465 processSync(mapper);
9466
9467 // compute scaling to help with touch position checking
9468 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9469 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9470 float scale =
9471 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9472
9473 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9475 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9476 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9477 0, 0, 0, 0, 0, 0, 0));
9478}
9479
9480TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9481 std::shared_ptr<FakePointerController> fakePointerController =
9482 std::make_shared<FakePointerController>();
9483
9484 prepareDisplay(DISPLAY_ORIENTATION_0);
9485 prepareAxes(POSITION | ID | SLOT);
9486 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009487 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009488 mFakePolicy->setPointerCapture(false);
9489 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9490
9491 // uncaptured touchpad should be a pointer device
9492 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9493
9494 // captured touchpad should be a touchpad device
9495 mFakePolicy->setPointerCapture(true);
9496 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9497 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9498}
9499
HQ Liue6983c72022-04-19 22:14:56 +00009500class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9501protected:
9502 float mPointerMovementScale;
9503 float mPointerXZoomScale;
9504 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9505 addConfigurationProperty("touch.deviceType", "pointer");
9506 std::shared_ptr<FakePointerController> fakePointerController =
9507 std::make_shared<FakePointerController>();
9508 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9509 fakePointerController->setPosition(0, 0);
9510 fakePointerController->setButtonState(0);
9511 prepareDisplay(DISPLAY_ORIENTATION_0);
9512
9513 prepareAxes(POSITION);
9514 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9515 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9516 // needs to be disabled, and the pointer gesture needs to be enabled.
9517 mFakePolicy->setPointerCapture(false);
9518 mFakePolicy->setPointerGestureEnabled(true);
9519 mFakePolicy->setPointerController(fakePointerController);
9520
9521 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9522 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9523 mPointerMovementScale =
9524 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9525 mPointerXZoomScale =
9526 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9527 }
9528
9529 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9530 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9531 /*flat*/ 0,
9532 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9533 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9534 /*flat*/ 0,
9535 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9536 }
9537};
9538
9539/**
9540 * Two fingers down on a pointer mode touch pad. The width
9541 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9542 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9543 * be greater than the both value to be freeform gesture, so that after two
9544 * fingers start to move downwards, the gesture should be swipe.
9545 */
9546TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9547 // The min freeform gesture width is 25units/mm x 30mm = 750
9548 // which is greater than fraction of the diagnal length of the touchpad (349).
9549 // Thus, MaxSwipWidth is 750.
9550 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9551 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9552 NotifyMotionArgs motionArgs;
9553
9554 // Two fingers down at once.
9555 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9556 // Pointer's initial position is used the [0,0] coordinate.
9557 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9558
9559 processId(mapper, FIRST_TRACKING_ID);
9560 processPosition(mapper, x1, y1);
9561 processMTSync(mapper);
9562 processId(mapper, SECOND_TRACKING_ID);
9563 processPosition(mapper, x2, y2);
9564 processMTSync(mapper);
9565 processSync(mapper);
9566
9567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9568 ASSERT_EQ(1U, motionArgs.pointerCount);
9569 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9570 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9571 ASSERT_NO_FATAL_FAILURE(
9572 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9573
9574 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9575 // that there should be 1 pointer.
9576 int32_t movingDistance = 200;
9577 y1 += movingDistance;
9578 y2 += movingDistance;
9579
9580 processId(mapper, FIRST_TRACKING_ID);
9581 processPosition(mapper, x1, y1);
9582 processMTSync(mapper);
9583 processId(mapper, SECOND_TRACKING_ID);
9584 processPosition(mapper, x2, y2);
9585 processMTSync(mapper);
9586 processSync(mapper);
9587
9588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9589 ASSERT_EQ(1U, motionArgs.pointerCount);
9590 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9591 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9593 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9594 0, 0, 0, 0));
9595}
9596
9597/**
9598 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9599 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9600 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9601 * value to be freeform gesture, so that after two fingers start to move downwards,
9602 * the gesture should be swipe.
9603 */
9604TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9605 // The min freeform gesture width is 5units/mm x 30mm = 150
9606 // which is greater than fraction of the diagnal length of the touchpad (349).
9607 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
9608 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
9609 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9610 NotifyMotionArgs motionArgs;
9611
9612 // Two fingers down at once.
9613 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9614 // Pointer's initial position is used the [0,0] coordinate.
9615 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9616
9617 processId(mapper, FIRST_TRACKING_ID);
9618 processPosition(mapper, x1, y1);
9619 processMTSync(mapper);
9620 processId(mapper, SECOND_TRACKING_ID);
9621 processPosition(mapper, x2, y2);
9622 processMTSync(mapper);
9623 processSync(mapper);
9624
9625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9626 ASSERT_EQ(1U, motionArgs.pointerCount);
9627 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9628 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9629 ASSERT_NO_FATAL_FAILURE(
9630 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9631
9632 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9633 // and there should be 1 pointer.
9634 int32_t movingDistance = 200;
9635 y1 += movingDistance;
9636 y2 += movingDistance;
9637
9638 processId(mapper, FIRST_TRACKING_ID);
9639 processPosition(mapper, x1, y1);
9640 processMTSync(mapper);
9641 processId(mapper, SECOND_TRACKING_ID);
9642 processPosition(mapper, x2, y2);
9643 processMTSync(mapper);
9644 processSync(mapper);
9645
9646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9647 ASSERT_EQ(1U, motionArgs.pointerCount);
9648 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9649 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9650 // New coordinate is the scaled relative coordinate from the initial coordinate.
9651 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9652 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9653 0, 0, 0, 0));
9654}
9655
9656/**
9657 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
9658 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
9659 * freeform gestures after two fingers start to move downwards.
9660 */
9661TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
9662 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9663 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9664
9665 NotifyMotionArgs motionArgs;
9666
9667 // Two fingers down at once. Wider than the max swipe width.
9668 // The gesture is expected to be PRESS, then transformed to FREEFORM
9669 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
9670
9671 processId(mapper, FIRST_TRACKING_ID);
9672 processPosition(mapper, x1, y1);
9673 processMTSync(mapper);
9674 processId(mapper, SECOND_TRACKING_ID);
9675 processPosition(mapper, x2, y2);
9676 processMTSync(mapper);
9677 processSync(mapper);
9678
9679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9680 ASSERT_EQ(1U, motionArgs.pointerCount);
9681 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9682 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9683 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
9684 ASSERT_NO_FATAL_FAILURE(
9685 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9686
9687 int32_t movingDistance = 200;
9688
9689 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
9690 // then two down events for two pointers.
9691 y1 += movingDistance;
9692 y2 += movingDistance;
9693
9694 processId(mapper, FIRST_TRACKING_ID);
9695 processPosition(mapper, x1, y1);
9696 processMTSync(mapper);
9697 processId(mapper, SECOND_TRACKING_ID);
9698 processPosition(mapper, x2, y2);
9699 processMTSync(mapper);
9700 processSync(mapper);
9701
9702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9703 // The previous PRESS gesture is cancelled, because it is transformed to freeform
9704 ASSERT_EQ(1U, motionArgs.pointerCount);
9705 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9707 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9708 ASSERT_EQ(1U, motionArgs.pointerCount);
9709 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9711 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9712 ASSERT_EQ(2U, motionArgs.pointerCount);
9713 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
9714 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9715 // Two pointers' scaled relative coordinates from their initial centroid.
9716 // Initial y coordinates are 0 as y1 and y2 have the same value.
9717 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
9718 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
9719 // When pointers move, the new coordinates equal to the initial coordinates plus
9720 // scaled moving distance.
9721 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9722 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9723 0, 0, 0, 0));
9724 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9725 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9726 0, 0, 0, 0));
9727
9728 // Move two fingers down again, expect one MOVE motion event.
9729 y1 += movingDistance;
9730 y2 += movingDistance;
9731
9732 processId(mapper, FIRST_TRACKING_ID);
9733 processPosition(mapper, x1, y1);
9734 processMTSync(mapper);
9735 processId(mapper, SECOND_TRACKING_ID);
9736 processPosition(mapper, x2, y2);
9737 processMTSync(mapper);
9738 processSync(mapper);
9739
9740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9741 ASSERT_EQ(2U, motionArgs.pointerCount);
9742 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9743 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9744 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9745 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9746 0, 0, 0, 0, 0));
9747 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9748 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9749 0, 0, 0, 0, 0));
9750}
9751
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009752// --- JoystickInputMapperTest ---
9753
9754class JoystickInputMapperTest : public InputMapperTest {
9755protected:
9756 static const int32_t RAW_X_MIN;
9757 static const int32_t RAW_X_MAX;
9758 static const int32_t RAW_Y_MIN;
9759 static const int32_t RAW_Y_MAX;
9760
9761 void SetUp() override {
9762 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9763 }
9764 void prepareAxes() {
9765 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9766 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9767 }
9768
9769 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9770 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9771 }
9772
9773 void processSync(JoystickInputMapper& mapper) {
9774 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9775 }
9776
9777 void prepareVirtualDisplay(int32_t orientation) {
9778 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9779 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9780 NO_PORT, ViewportType::VIRTUAL);
9781 }
9782};
9783
9784const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9785const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9786const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9787const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9788
9789TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9790 prepareAxes();
9791 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9792
9793 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9794
9795 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9796
9797 // Send an axis event
9798 processAxis(mapper, ABS_X, 100);
9799 processSync(mapper);
9800
9801 NotifyMotionArgs args;
9802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9803 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9804
9805 // Send another axis event
9806 processAxis(mapper, ABS_Y, 100);
9807 processSync(mapper);
9808
9809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9810 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9811}
9812
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009813// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009814
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009815class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009816protected:
9817 static const char* DEVICE_NAME;
9818 static const char* DEVICE_LOCATION;
9819 static const int32_t DEVICE_ID;
9820 static const int32_t DEVICE_GENERATION;
9821 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009822 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009823 static const int32_t EVENTHUB_ID;
9824
9825 std::shared_ptr<FakeEventHub> mFakeEventHub;
9826 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009827 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009828 std::unique_ptr<InstrumentedInputReader> mReader;
9829 std::shared_ptr<InputDevice> mDevice;
9830
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009831 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009832 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009833 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009834 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009835 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009836 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009837 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9838 }
9839
9840 void SetUp() override { SetUp(DEVICE_CLASSES); }
9841
9842 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009843 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009844 mFakePolicy.clear();
9845 }
9846
9847 void configureDevice(uint32_t changes) {
9848 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9849 mReader->requestRefreshConfiguration(changes);
9850 mReader->loopOnce();
9851 }
9852 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9853 }
9854
9855 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9856 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009857 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009858 InputDeviceIdentifier identifier;
9859 identifier.name = name;
9860 identifier.location = location;
9861 std::shared_ptr<InputDevice> device =
9862 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9863 identifier);
9864 mReader->pushNextDevice(device);
9865 mFakeEventHub->addDevice(eventHubId, name, classes);
9866 mReader->loopOnce();
9867 return device;
9868 }
9869
9870 template <class T, typename... Args>
9871 T& addControllerAndConfigure(Args... args) {
9872 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9873
9874 return controller;
9875 }
9876};
9877
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009878const char* PeripheralControllerTest::DEVICE_NAME = "device";
9879const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9880const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9881const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9882const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009883const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
9884 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009885const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009886
9887// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009888class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009889protected:
9890 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009891 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009892 }
9893};
9894
9895TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009896 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009897
9898 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9899 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9900}
9901
9902TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009903 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009904
9905 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9906 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9907}
9908
9909// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009910class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009911protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009912 void SetUp() override {
9913 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9914 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009915};
9916
Chris Ye85758332021-05-16 23:05:17 -07009917TEST_F(LightControllerTest, MonoLight) {
9918 RawLightInfo infoMono = {.id = 1,
9919 .name = "Mono",
9920 .maxBrightness = 255,
9921 .flags = InputLightClass::BRIGHTNESS,
9922 .path = ""};
9923 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009924
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009925 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009926 InputDeviceInfo info;
9927 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009928 std::vector<InputDeviceLightInfo> lights = info.getLights();
9929 ASSERT_EQ(1U, lights.size());
9930 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009931
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009932 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9933 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009934}
9935
9936TEST_F(LightControllerTest, RGBLight) {
9937 RawLightInfo infoRed = {.id = 1,
9938 .name = "red",
9939 .maxBrightness = 255,
9940 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9941 .path = ""};
9942 RawLightInfo infoGreen = {.id = 2,
9943 .name = "green",
9944 .maxBrightness = 255,
9945 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9946 .path = ""};
9947 RawLightInfo infoBlue = {.id = 3,
9948 .name = "blue",
9949 .maxBrightness = 255,
9950 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9951 .path = ""};
9952 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9953 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9954 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9955
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009956 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009957 InputDeviceInfo info;
9958 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009959 std::vector<InputDeviceLightInfo> lights = info.getLights();
9960 ASSERT_EQ(1U, lights.size());
9961 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009962
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009963 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9964 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009965}
9966
9967TEST_F(LightControllerTest, MultiColorRGBLight) {
9968 RawLightInfo infoColor = {.id = 1,
9969 .name = "red",
9970 .maxBrightness = 255,
9971 .flags = InputLightClass::BRIGHTNESS |
9972 InputLightClass::MULTI_INTENSITY |
9973 InputLightClass::MULTI_INDEX,
9974 .path = ""};
9975
9976 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9977
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009978 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009979 InputDeviceInfo info;
9980 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009981 std::vector<InputDeviceLightInfo> lights = info.getLights();
9982 ASSERT_EQ(1U, lights.size());
9983 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009984
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009985 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9986 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009987}
9988
9989TEST_F(LightControllerTest, PlayerIdLight) {
9990 RawLightInfo info1 = {.id = 1,
9991 .name = "player1",
9992 .maxBrightness = 255,
9993 .flags = InputLightClass::BRIGHTNESS,
9994 .path = ""};
9995 RawLightInfo info2 = {.id = 2,
9996 .name = "player2",
9997 .maxBrightness = 255,
9998 .flags = InputLightClass::BRIGHTNESS,
9999 .path = ""};
10000 RawLightInfo info3 = {.id = 3,
10001 .name = "player3",
10002 .maxBrightness = 255,
10003 .flags = InputLightClass::BRIGHTNESS,
10004 .path = ""};
10005 RawLightInfo info4 = {.id = 4,
10006 .name = "player4",
10007 .maxBrightness = 255,
10008 .flags = InputLightClass::BRIGHTNESS,
10009 .path = ""};
10010 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10011 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10012 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10013 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10014
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010015 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010016 InputDeviceInfo info;
10017 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010018 std::vector<InputDeviceLightInfo> lights = info.getLights();
10019 ASSERT_EQ(1U, lights.size());
10020 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010021
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010022 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10023 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10024 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010025}
10026
Michael Wrightd02c5b62014-02-10 15:10:22 -080010027} // namespace android